@@ -3,8 +3,14 @@ import { TurnkeySigner } from "../";
3
3
import { TurnkeyClient } from "@turnkey/http" ;
4
4
import { ApiKeyStamper } from "@turnkey/api-key-stamper" ;
5
5
import { PublicKey , SystemProgram , Transaction } from "@solana/web3.js" ;
6
+ import nacl from "tweetnacl" ;
7
+ import bs58 from "bs58" ;
6
8
7
9
describe ( "TurnkeySigner" , ( ) => {
10
+ const organizationId = "4456e4c2-e8b5-4b93-a0cf-1dfae265c12c" ;
11
+ const apiPublicKey =
12
+ "025d374c674fc389c761462f3c59c0acabdcb3a17c599d9e62e5fe78fe984cfbeb" ;
13
+ const turnkeySolAddress = "D8P541wwnertZTgDT14kYJPoFT2eHUFqjTgPxMK5qatM" ;
8
14
test ( "can sign a Solana transfer against production" , async ( ) => {
9
15
if ( ! process . env . SOLANA_TEST_ORG_API_PRIVATE_KEY ) {
10
16
// This test requires an env var to be set
@@ -16,19 +22,16 @@ describe("TurnkeySigner", () => {
16
22
const client = new TurnkeyClient (
17
23
{ baseUrl : "https://api.turnkey.com" } ,
18
24
new ApiKeyStamper ( {
19
- apiPublicKey :
20
- "025d374c674fc389c761462f3c59c0acabdcb3a17c599d9e62e5fe78fe984cfbeb" ,
25
+ apiPublicKey,
21
26
apiPrivateKey : process . env . SOLANA_TEST_ORG_API_PRIVATE_KEY ,
22
27
} )
23
28
) ;
24
29
25
30
const signer = new TurnkeySigner ( {
26
- organizationId : "4456e4c2-e8b5-4b93-a0cf-1dfae265c12c" ,
31
+ organizationId,
27
32
client,
28
33
} ) ;
29
34
30
- const turnkeySolAddress = "D8P541wwnertZTgDT14kYJPoFT2eHUFqjTgPxMK5qatM" ;
31
-
32
35
const transferTransaction = new Transaction ( ) . add (
33
36
SystemProgram . transfer ( {
34
37
fromPubkey : new PublicKey ( turnkeySolAddress ) ,
@@ -48,4 +51,41 @@ describe("TurnkeySigner", () => {
48
51
await signer . addSignature ( transferTransaction , turnkeySolAddress ) ;
49
52
expect ( transferTransaction . signatures . length ) . toBe ( 1 ) ;
50
53
} ) ;
54
+
55
+ test ( "can sign a message with a Solana account" , async ( ) => {
56
+ if ( ! process . env . SOLANA_TEST_ORG_API_PRIVATE_KEY ) {
57
+ // This test requires an env var to be set
58
+ throw new Error (
59
+ "This test requires SOLANA_TEST_ORG_API_PRIVATE_KEY to be set"
60
+ ) ;
61
+ }
62
+
63
+ const client = new TurnkeyClient (
64
+ { baseUrl : "https://api.turnkey.com" } ,
65
+ new ApiKeyStamper ( {
66
+ apiPublicKey,
67
+ apiPrivateKey : process . env . SOLANA_TEST_ORG_API_PRIVATE_KEY ,
68
+ } )
69
+ ) ;
70
+
71
+ const signer = new TurnkeySigner ( {
72
+ organizationId,
73
+ client,
74
+ } ) ;
75
+
76
+ const message = "Hello world!" ;
77
+ const messageAsUint8Array = Buffer . from ( message ) ;
78
+
79
+ const signature = await signer . signMessage (
80
+ messageAsUint8Array ,
81
+ turnkeySolAddress
82
+ ) ;
83
+
84
+ const isValidSignature = nacl . sign . detached . verify (
85
+ messageAsUint8Array ,
86
+ signature ,
87
+ bs58 . decode ( turnkeySolAddress )
88
+ ) ;
89
+ expect ( isValidSignature ) . toBeTruthy ( ) ;
90
+ } ) ;
51
91
} ) ;
0 commit comments