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.
    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.
    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.
    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
import "C"
11
12
import "fmt"
13
import "unsafe"
14
import "encoding/hex"
15
16
// C.TWString -> Go string
17
func TWStringGoString(s unsafe.Pointer) string {
18
return C.GoString(C.TWStringUTF8Bytes(s))
19
}
20
21
// Go string -> C.TWString
22
func TWStringCreateWithGoString(s string) unsafe.Pointer {
23
cStr := C.CString(s)
24
defer C.free(unsafe.Pointer(cStr))
25
str := C.TWStringCreateWithUTF8Bytes(cStr)
26
return str
27
}
28
29
// C.TWData -> Go byte[]
30
func TWDataGoBytes(d unsafe.Pointer) []byte {
31
cBytes := C.TWDataBytes(d)
32
cSize := C.TWDataSize(d)
33
return C.GoBytes(unsafe.Pointer(cBytes), C.int(cSize))
34
}
35
36
// Go byte[] -> C.TWData
37
func TWDataCreateWithGoBytes(d []byte) unsafe.Pointer {
38
cBytes := C.CBytes(d)
39
defer C.free(unsafe.Pointer(cBytes))
40
data := C.TWDataCreateWithBytes((*C.uchar)(cBytes), C.ulong(len(d)))
41
return data
42
}
43
44
func main() {
45
fmt.Println("==> calling wallet core from go")
46
str := TWStringCreateWithGoString("confirm bleak useless tail chalk destroy horn step bulb genuine attract split")
47
emtpy := TWStringCreateWithGoString("")
48
defer C.TWStringDelete(str)
49
defer C.TWStringDelete(emtpy)
50
51
fmt.Println("<== mnemonic is valid: ", C.TWHDWalletIsValid(str))
52
53
wallet := C.TWHDWalletCreateWithMnemonic(str, emtpy)
54
defer C.TWHDWalletDelete(wallet)
55
56
key := C.TWHDWalletGetKeyForCoin(wallet, C.TWCoinTypeBitcoin)
57
keyData := C.TWPrivateKeyData(key)
58
keyHex := hex.EncodeToString(TWDataGoBytes(keyData))
59
fmt.Println("<== bitcoin private key: ", keyHex)
60
61
pubKey, _ := hex.DecodeString("0288be7586c41a0498c1f931a0aaf08c15811ee2651a5fe0fa213167dcaba59ae8")
62
pubKeyData := TWDataCreateWithGoBytes(pubKey)
63
defer C.TWDataDelete(pubKeyData)
64
65
fmt.Println("<== bitcoin public key is valid: ", C.TWPublicKeyIsValid(pubKeyData, C.TWPublicKeyTypeSECP256k1))
66
67
address := C.TWHDWalletGetAddressForCoin(wallet, C.TWCoinTypeBitcoin)
68
fmt.Println("<== bitcoin address: ", TWStringGoString(address))
69
}
Copied!
    1.
    Compile it by go build -o main
    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.
    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.
Last modified 1yr ago
Copy link
Contents
Golang