1+ #!/usr/bin/env node
2+
3+ const { DstackClient, TappdClient, getComposeHash, verifyEnvEncryptPublicKey } = require ( './dist/node/index.js' ) ;
4+ const { toViemAccount, toViemAccountSecure } = require ( './dist/node/viem.js' ) ;
5+ const { toKeypair, toKeypairSecure } = require ( './dist/node/solana.js' ) ;
6+
7+ async function main ( ) {
8+ console . log ( "=== JS SDK Output Test ===" ) ;
9+
10+ try {
11+ // Test client get_key
12+ const client = new DstackClient ( ) ;
13+ console . log ( "\n1. Testing DstackClient.getKey()" ) ;
14+
15+ const testPaths = [
16+ { path : "test/wallet" , purpose : "ethereum" } ,
17+ { path : "test/signing" , purpose : "solana" } ,
18+ { path : "user/alice" , purpose : "mainnet" }
19+ ] ;
20+
21+ for ( const { path, purpose } of testPaths ) {
22+ const keyResult = await client . getKey ( path , purpose ) ;
23+ console . log ( `getKey('${ path } ', '${ purpose } '):` ) ;
24+ console . log ( ` key: ${ Buffer . from ( keyResult . key ) . toString ( 'hex' ) } ` ) ;
25+ console . log ( ` signature_chain length: ${ keyResult . signature_chain . length } ` ) ;
26+ console . log ( ` signature_chain[0]: ${ Buffer . from ( keyResult . signature_chain [ 0 ] ) . toString ( 'hex' ) } ` ) ;
27+ }
28+
29+ // Test viem integration
30+ console . log ( "\n2. Testing Viem Integration" ) ;
31+ const ethKey = await client . getKey ( "eth/test" , "wallet" ) ;
32+
33+ console . log ( "\n2.1 toViemAccount (legacy):" ) ;
34+ try {
35+ const account = toViemAccount ( ethKey ) ;
36+ console . log ( ` address: ${ account . address } ` ) ;
37+ console . log ( ` type: ${ account . type } ` ) ;
38+ } catch ( error ) {
39+ console . log ( ` error: ${ error . message } ` ) ;
40+ }
41+
42+ console . log ( "\n2.2 toViemAccountSecure:" ) ;
43+ try {
44+ const accountSecure = toViemAccountSecure ( ethKey ) ;
45+ console . log ( ` address: ${ accountSecure . address } ` ) ;
46+ console . log ( ` type: ${ accountSecure . type } ` ) ;
47+ } catch ( error ) {
48+ console . log ( ` error: ${ error . message } ` ) ;
49+ }
50+
51+ // Test solana integration
52+ console . log ( "\n3. Testing Solana Integration" ) ;
53+ const solKey = await client . getKey ( "sol/test" , "wallet" ) ;
54+
55+ console . log ( "\n3.1 toKeypair (legacy):" ) ;
56+ try {
57+ const keypair = toKeypair ( solKey ) ;
58+ console . log ( ` publicKey: ${ keypair . publicKey . toString ( ) } ` ) ;
59+ console . log ( ` secretKey length: ${ keypair . secretKey . length } ` ) ;
60+ console . log ( ` secretKey (first 32 bytes): ${ Buffer . from ( keypair . secretKey . slice ( 0 , 32 ) ) . toString ( 'hex' ) } ` ) ;
61+ } catch ( error ) {
62+ console . log ( ` error: ${ error . message } ` ) ;
63+ }
64+
65+ console . log ( "\n3.2 toKeypairSecure:" ) ;
66+ try {
67+ const keypairSecure = toKeypairSecure ( solKey ) ;
68+ console . log ( ` publicKey: ${ keypairSecure . publicKey . toString ( ) } ` ) ;
69+ console . log ( ` secretKey length: ${ keypairSecure . secretKey . length } ` ) ;
70+ console . log ( ` secretKey (first 32 bytes): ${ Buffer . from ( keypairSecure . secretKey . slice ( 0 , 32 ) ) . toString ( 'hex' ) } ` ) ;
71+ } catch ( error ) {
72+ console . log ( ` error: ${ error . message } ` ) ;
73+ }
74+
75+ // Test TappdClient (deprecated)
76+ console . log ( "\n4. Testing TappdClient (deprecated)" ) ;
77+ try {
78+ const tappdClient = new TappdClient ( ) ;
79+ console . log ( "\n4.1 TappdClient.getKey():" ) ;
80+ const tappdKey = await tappdClient . getKey ( "test/wallet" , "ethereum" ) ;
81+ console . log ( ` key: ${ Buffer . from ( tappdKey . key ) . toString ( 'hex' ) } ` ) ;
82+ console . log ( ` signature_chain length: ${ tappdKey . signature_chain . length } ` ) ;
83+
84+ console . log ( "\n4.2 TappdClient.tdxQuote():" ) ;
85+ const tappdQuote = await tappdClient . tdxQuote ( "test-data" , "raw" ) ;
86+ console . log ( ` quote length: ${ tappdQuote . quote . length } ` ) ;
87+ console . log ( ` event_log length: ${ tappdQuote . event_log . length } ` ) ;
88+ console . log ( ` rtmrs count: ${ tappdQuote . replayRtmrs ( ) . length } ` ) ;
89+ } catch ( error ) {
90+ console . log ( ` error: ${ error . message } ` ) ;
91+ }
92+
93+ // Test quotes
94+ console . log ( "\n5. Testing Quote Methods" ) ;
95+ console . log ( "\n5.1 DstackClient.getQuote():" ) ;
96+ const dstackQuote = await client . getQuote ( "test-data-for-quote" ) ;
97+ console . log ( ` quote length: ${ dstackQuote . quote . length } ` ) ;
98+ console . log ( ` event_log length: ${ dstackQuote . event_log . length } ` ) ;
99+ console . log ( ` rtmrs count: ${ dstackQuote . replayRtmrs ( ) . length } ` ) ;
100+
101+ // Test getComposeHash
102+ console . log ( "\n6. Testing getComposeHash" ) ;
103+ const testComposes = [
104+ {
105+ manifest_version : 1 ,
106+ name : "test-app" ,
107+ runner : "docker-compose" ,
108+ docker_compose_file : "services:\\n app:\\n image: test\\n ports:\\n - 8080:8080"
109+ } ,
110+ {
111+ manifest_version : 1 ,
112+ name : "another-app" ,
113+ runner : "docker-compose" ,
114+ docker_compose_file : "services:\\n web:\\n build: .\\n environment:\\n - NODE_ENV=production"
115+ }
116+ ] ;
117+
118+ testComposes . forEach ( ( compose , index ) => {
119+ const hash = getComposeHash ( compose ) ;
120+ console . log ( `compose ${ index + 1 } : ${ hash } ` ) ;
121+ console . log ( ` name: ${ compose . name } ` ) ;
122+ console . log ( ` runner: ${ compose . runner } ` ) ;
123+ } ) ;
124+
125+ // Test verifyEnvEncryptPublicKey
126+ console . log ( "\n7. Testing verifyEnvEncryptPublicKey" ) ;
127+ const testCases = [
128+ {
129+ publicKey : Buffer . from ( 'e33a1832c6562067ff8f844a61e51ad051f1180b66ec2551fb0251735f3ee90a' , 'hex' ) ,
130+ signature : Buffer . from ( '8542c49081fbf4e03f62034f13fbf70630bdf256a53032e38465a27c36fd6bed7a5e7111652004aef37f7fd92fbfc1285212c4ae6a6154203a48f5e16cad2cef00' , 'hex' ) ,
131+ appId : '0000000000000000000000000000000000000000'
132+ } ,
133+ {
134+ publicKey : Buffer . from ( 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef' , 'hex' ) ,
135+ signature : Buffer . from ( '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' , 'hex' ) ,
136+ appId : 'invalid-app-id'
137+ }
138+ ] ;
139+
140+ testCases . forEach ( ( testCase , index ) => {
141+ try {
142+ const result = verifyEnvEncryptPublicKey ( testCase . publicKey , testCase . signature , testCase . appId ) ;
143+ console . log ( `test case ${ index + 1 } : ${ result ? Buffer . from ( result ) . toString ( 'hex' ) : 'null' } ` ) ;
144+ } catch ( error ) {
145+ console . log ( `test case ${ index + 1 } : error - ${ error . message } ` ) ;
146+ }
147+ } ) ;
148+
149+ } catch ( error ) {
150+ console . error ( "Error:" , error . message ) ;
151+ process . exit ( 1 ) ;
152+ }
153+ }
154+
155+ main ( ) . catch ( console . error ) ;
0 commit comments