Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (56 loc) · 1.65 KB
/
Copy pathmain.go
File metadata and controls
68 lines (56 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Package main demonstrates exporting a wallet mnemonic via the Turnkey enclave export flow.
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"github.com/tkhq/go-sdk/crypto"
turnkey "github.com/tkhq/go-sdk/v2"
)
func main() {
apiPrivateKey := mustEnv("TURNKEY_API_PRIVATE_KEY")
organizationID := mustEnv("TURNKEY_ORGANIZATION_ID")
walletID := mustEnv("TURNKEY_WALLET_ID")
stamper, err := turnkey.NewAPIKeyStamper(apiPrivateKey)
if err != nil {
log.Fatal("failed to create stamper:", err)
}
client, err := turnkey.NewClient(stamper, organizationID)
if err != nil {
log.Fatal("failed to create Turnkey client:", err)
}
ctx := context.Background()
targetPublicKey, kemPrivateKey, err := crypto.GenerateEncryptionKeyPair()
if err != nil {
log.Fatal("failed to generate encryption key:", err)
}
result, err := client.ExportWallet(ctx, turnkey.ExportWalletRequest{
WalletID: walletID,
TargetPublicKey: targetPublicKey,
})
if err != nil {
fatalRequestError(err, "export wallet")
}
mnemonic, err := crypto.DecryptExportBundle([]byte(result.ExportBundle), organizationID, kemPrivateKey)
if err != nil {
log.Fatal("failed to decrypt export bundle:", err)
}
fmt.Printf("Wallet ID: %s\n", result.WalletID)
fmt.Printf("Mnemonic: %s\n", string(mnemonic))
}
func mustEnv(key string) string {
v := os.Getenv(key)
if v == "" {
log.Fatalf("%s is required", key)
}
return v
}
func fatalRequestError(err error, action string) {
var reqErr *turnkey.RequestError
if errors.As(err, &reqErr) {
log.Fatalf("failed to %s (status=%d): %s", action, reqErr.StatusCode, reqErr.Body)
}
log.Fatalf("failed to %s: %v", action, err)
}