Server-Side
If your server application (most likely on Linux) language supports FFI to C, it is possible to call wallet core.

Golang

Here is a step by step example of cgo, using Wallet Core docker image.
  1. 1.
    Run docker run -it trustwallet/wallet-core The librabry is already built in this image (Build instructions here) Note: may not be the most recent version.
  2. 2.
    Install go: apt-get update && apt-get install golang (or download from here go1.13.3, configure GOROOT and append GOROOT/bin to PATH).
  3. 3.
    Save the sample code below to file called main.go
1
package main
2
3
// #cgo CFLAGS: -I/wallet-core/include
4
// #cgo LDFLAGS: -L/wallet-core/build -L/wallet-core/build/trezor-crypto -lTrustWalletCore -lprotobuf -lTrezorCrypto -lc++ -lm
5
// #include <TrustWalletCore/TWHDWallet.h>
6
// #include <TrustWalletCore/TWString.h>
7
// #include <TrustWalletCore/TWData.h>
8
// #include <TrustWalletCore/TWPrivateKey.h>
9
// #include <TrustWalletCore/TWPublicKey.h>
10
// #include <TrustWalletCore/TWMnemonic.h>
11
import "C"
12
13
import "fmt"
14
import "unsafe"
15
import "encoding/hex"
16
17
// C.TWString -> Go string
18
func TWStringGoString(s unsafe.Pointer) string {
19
return C.GoString(C.TWStringUTF8Bytes(s))
20
}
21
22
// Go string -> C.TWString
23
func TWStringCreateWithGoString(s string) unsafe.Pointer {
24
cStr := C.CString(s)
25
defer C.free(unsafe.Pointer(cStr))
26
str := C.TWStringCreateWithUTF8Bytes(cStr)
27
return str
28
}
29
30
// C.TWData -> Go byte[]
31
func TWDataGoBytes(d unsafe.Pointer) []byte {
32
cBytes := C.TWDataBytes(d)
33
cSize := C.TWDataSize(d)
34
return C.GoBytes(unsafe.Pointer(cBytes), C.int(cSize))
35
}
36
37
// Go byte[] -> C.TWData
38
func TWDataCreateWithGoBytes(d []byte) unsafe.Pointer {
39
cBytes := C.CBytes(d)
40
defer C.free(unsafe.Pointer(cBytes))
41
data := C.TWDataCreateWithBytes((*C.uchar)(cBytes), C.ulong(len(d)))
42
return data
43
}
44
45
func main() {
46
fmt.Println("==> calling wallet core from go")
47
str := TWStringCreateWithGoString("confirm bleak useless tail chalk destroy horn step bulb genuine attract split")
48
emtpy := TWStringCreateWithGoString("")
49
defer C.TWStringDelete(str)
50
defer C.TWStringDelete(emtpy)
51
52
fmt.Println("<== mnemonic is valid: ",C.TWMnemonicIsValid(str))
53
54
wallet := C.TWHDWalletCreateWithMnemonic(str, emtpy)
55
defer C.TWHDWalletDelete(wallet)
56
57
key := C.TWHDWalletGetKeyForCoin(wallet, C.TWCoinTypeBitcoin)
58
keyData := C.TWPrivateKeyData(key)
59
keyHex := hex.EncodeToString(TWDataGoBytes(keyData))
60
fmt.Println("<== bitcoin private key: ", keyHex)
61
62
pubKey, _ := hex.DecodeString("0288be7586c41a0498c1f931a0aaf08c15811ee2651a5fe0fa213167dcaba59ae8")
63
pubKeyData := TWDataCreateWithGoBytes(pubKey)
64
defer C.TWDataDelete(pubKeyData)
65
66
fmt.Println("<== bitcoin public key is valid: ", C.TWPublicKeyIsValid(pubKeyData, C.TWPublicKeyTypeSECP256k1))
67
68
address := C.TWHDWalletGetAddressForCoin(wallet, C.TWCoinTypeBitcoin)
69
fmt.Println("<== bitcoin address: ", TWStringGoString(address))
70
}
Copied!
  1. 1.
    Compile it by go build -o main
  2. 2.
    Run ./main and you will see the output below:
1
==> calling wallet core from go
2
<== mnemonic is valid: true
3
<== bitcoin private key: 945f423798858e24aa1ab490648013db63ad1f539ebbb8cb1399edd1d0b59716
4
<== bitcoin public key is valid: true
5
<== bitcoin address: bc1qw29x4hrt6tahz4jvuhzrq6y5el3spqt499zuay
Copied!
  1. 1.
    You might want to copy and run main outside of the docker container, make sure you have libc++1 and libc++abi1 installed in your host Ubuntu.

Node.js

  1. 1.
    Install from NPM or GPR
1
npm install @trustwallet/wallet-core
Copied!
  1. 1.
    Check tests for API usage
Copy link