1- import * as dotenv from ' dotenv' ;
2- import * as path from ' path' ;
3- import { IotaClient , getFullnodeUrl } from ' @iota/iota-sdk/client' ;
4- import { Transaction } from ' @iota/iota-sdk/transactions' ;
5- import { Ed25519PublicKey } from ' @iota/iota-sdk/keypairs/ed25519' ;
6- import { messageWithIntent } from ' @iota/iota-sdk/cryptography' ;
7- import { Turnkey } from ' @turnkey/sdk-server' ;
8- import { blake2b } from ' @noble/hashes/blake2b' ;
9- import { bytesToHex } from ' @noble/hashes/utils' ;
10-
11- dotenv . config ( { path : path . resolve ( process . cwd ( ) , ' .env.local' ) } ) ;
1+ import * as dotenv from " dotenv" ;
2+ import * as path from " path" ;
3+ import { IotaClient , getFullnodeUrl } from " @iota/iota-sdk/client" ;
4+ import { Transaction } from " @iota/iota-sdk/transactions" ;
5+ import { Ed25519PublicKey } from " @iota/iota-sdk/keypairs/ed25519" ;
6+ import { messageWithIntent } from " @iota/iota-sdk/cryptography" ;
7+ import { Turnkey } from " @turnkey/sdk-server" ;
8+ import { blake2b } from " @noble/hashes/blake2b" ;
9+ import { bytesToHex } from " @noble/hashes/utils" ;
10+
11+ dotenv . config ( { path : path . resolve ( process . cwd ( ) , " .env.local" ) } ) ;
1212
1313function toSerializedSignature ( {
1414 signature,
@@ -20,12 +20,12 @@ function toSerializedSignature({
2020 const scheme = new Uint8Array ( [ 0x00 ] ) ; // ED25519 flag
2121 const pubKeyBytes = pubKey . toRawBytes ( ) ;
2222 const serialized = new Uint8Array (
23- scheme . length + signature . length + pubKeyBytes . length
23+ scheme . length + signature . length + pubKeyBytes . length ,
2424 ) ;
2525 serialized . set ( scheme , 0 ) ;
2626 serialized . set ( signature , scheme . length ) ;
2727 serialized . set ( pubKeyBytes , scheme . length + signature . length ) ;
28- return Buffer . from ( serialized ) . toString ( ' base64' ) ;
28+ return Buffer . from ( serialized ) . toString ( " base64" ) ;
2929}
3030
3131async function main ( ) {
@@ -42,35 +42,35 @@ async function main() {
4242 // *** ENVIRONMENT CHECKING *** //
4343
4444 if ( IOTA_ADDRESS === undefined || IOTA_PUBLIC_KEY === undefined ) {
45- throw new Error ( ' IOTA_ADDRESS or IOTA_PUBLIC_KEY not set in .env.local' ) ;
45+ throw new Error ( " IOTA_ADDRESS or IOTA_PUBLIC_KEY not set in .env.local" ) ;
4646 }
4747
48- const publicKey = new Ed25519PublicKey ( Buffer . from ( IOTA_PUBLIC_KEY ! , ' hex' ) ) ;
48+ const publicKey = new Ed25519PublicKey ( Buffer . from ( IOTA_PUBLIC_KEY ! , " hex" ) ) ;
4949 if ( publicKey . toIotaAddress ( ) !== IOTA_ADDRESS ) {
50- throw new Error ( ' IOTA_PUBLIC_KEY does not match IOTA_ADDRESS' ) ;
50+ throw new Error ( " IOTA_PUBLIC_KEY does not match IOTA_ADDRESS" ) ;
5151 }
5252
5353 // sending to the same address
5454 const recipient = IOTA_ADDRESS ;
5555 const amount = 1_000_000n ; // 0.001 IOTA
5656
5757 const turnkeyClient = new Turnkey ( {
58- apiBaseUrl : ' https://api.turnkey.com' ,
58+ apiBaseUrl : " https://api.turnkey.com" ,
5959 apiPrivateKey : API_PRIVATE_KEY ! ,
6060 apiPublicKey : API_PUBLIC_KEY ! ,
6161 defaultOrganizationId : ORGANIZATION_ID ! ,
6262 } ) ;
6363
6464 // *** TRANSACTION BUILDING *** //
6565
66- const provider = new IotaClient ( { url : getFullnodeUrl ( ' testnet' ) } ) ;
66+ const provider = new IotaClient ( { url : getFullnodeUrl ( " testnet" ) } ) ;
6767
6868 // fetch the user's IOTA coin objects
6969 const coins = await provider . getCoins ( {
7070 owner : IOTA_ADDRESS ! ,
71- coinType : ' 0x2::iota::IOTA' ,
71+ coinType : " 0x2::iota::IOTA" ,
7272 } ) ;
73- if ( ! coins . data . length ) throw new Error ( ' No IOTA coins' ) ;
73+ if ( ! coins . data . length ) throw new Error ( " No IOTA coins" ) ;
7474
7575 const tx = new Transaction ( ) ;
7676 tx . setSender ( IOTA_ADDRESS ! ) ;
@@ -83,37 +83,37 @@ async function main() {
8383 digest : coins . data [ 0 ] ! . digest ,
8484 } ,
8585 ] ) ; // separate intended send amount from gas payment
86- const coin = tx . splitCoins ( tx . gas , [ tx . pure ( ' u64' , amount ) ] ) ;
86+ const coin = tx . splitCoins ( tx . gas , [ tx . pure ( " u64" , amount ) ] ) ;
8787 tx . transferObjects ( [ coin ] , tx . pure . address ( recipient ) ) ;
8888
8989 const txBytes = await tx . build ( ) ;
9090
91- const intentMsg = messageWithIntent ( ' TransactionData' , txBytes ) ;
91+ const intentMsg = messageWithIntent ( " TransactionData" , txBytes ) ;
9292 const digest = blake2b ( intentMsg , { dkLen : 32 } ) ;
9393
9494 const { r, s } = await turnkeyClient . apiClient ( ) . signRawPayload ( {
9595 signWith : IOTA_ADDRESS ! ,
9696 payload : bytesToHex ( digest ) ,
97- encoding : ' PAYLOAD_ENCODING_HEXADECIMAL' ,
98- hashFunction : ' HASH_FUNCTION_NOT_APPLICABLE' ,
97+ encoding : " PAYLOAD_ENCODING_HEXADECIMAL" ,
98+ hashFunction : " HASH_FUNCTION_NOT_APPLICABLE" ,
9999 } ) ;
100100
101- const signature = Buffer . from ( r + s , ' hex' ) ;
101+ const signature = Buffer . from ( r + s , " hex" ) ;
102102 const serialized = toSerializedSignature ( { signature, pubKey : publicKey } ) ;
103103
104104 // *** EXECUTION *** //
105105
106106 const result = await provider . executeTransactionBlock ( {
107- transactionBlock : Buffer . from ( txBytes ) . toString ( ' base64' ) ,
107+ transactionBlock : Buffer . from ( txBytes ) . toString ( " base64" ) ,
108108 signature : serialized ,
109- requestType : ' WaitForEffectsCert' ,
109+ requestType : " WaitForEffectsCert" ,
110110 options : { showEffects : true } ,
111111 } ) ;
112112
113- console . log ( ' Transaction digest:' , result . digest ) ;
113+ console . log ( " Transaction digest:" , result . digest ) ;
114114}
115115
116116main ( ) . catch ( ( err ) => {
117- console . error ( ' Error:' , err ) ;
117+ console . error ( " Error:" , err ) ;
118118 process . exit ( 1 ) ;
119- } ) ;
119+ } ) ;
0 commit comments