@@ -39,10 +39,17 @@ async function main() {
3939 IOTA_PUBLIC_KEY ,
4040 } = process . env ;
4141
42+ // *** ENVIRONMENT CHECKING *** //
43+
4244 if ( IOTA_ADDRESS === undefined || IOTA_PUBLIC_KEY === undefined ) {
4345 throw new Error ( 'IOTA_ADDRESS or IOTA_PUBLIC_KEY not set in .env.local' ) ;
4446 }
4547
48+ const publicKey = new Ed25519PublicKey ( Buffer . from ( IOTA_PUBLIC_KEY ! , 'hex' ) ) ;
49+ if ( publicKey . toIotaAddress ( ) !== IOTA_ADDRESS ) {
50+ throw new Error ( 'IOTA_PUBLIC_KEY does not match IOTA_ADDRESS' ) ;
51+ }
52+
4653 // sending to the same address
4754 const recipient = IOTA_ADDRESS ;
4855 const amount = 1_000_000n ; // 0.001 IOTA
@@ -54,12 +61,9 @@ async function main() {
5461 defaultOrganizationId : ORGANIZATION_ID ! ,
5562 } ) ;
5663
57- const provider = new IotaClient ( { url : getFullnodeUrl ( 'testnet' ) } ) ;
58- const publicKey = new Ed25519PublicKey ( Buffer . from ( IOTA_PUBLIC_KEY ! , 'hex' ) ) ;
64+ // *** TRANSACTION BUILDING *** //
5965
60- if ( publicKey . toIotaAddress ( ) !== IOTA_ADDRESS ) {
61- throw new Error ( 'IOTA_PUBLIC_KEY does not match IOTA_ADDRESS' ) ;
62- }
66+ const provider = new IotaClient ( { url : getFullnodeUrl ( 'testnet' ) } ) ;
6367
6468 // fetch the user's IOTA coin objects
6569 const coins = await provider . getCoins ( {
@@ -78,7 +82,7 @@ async function main() {
7882 version : coins . data [ 0 ] ! . version ,
7983 digest : coins . data [ 0 ] ! . digest ,
8084 } ,
81- ] ) ;
85+ ] ) ; // separate intended send amount from gas payment
8286 const coin = tx . splitCoins ( tx . gas , [ tx . pure ( 'u64' , amount ) ] ) ;
8387 tx . transferObjects ( [ coin ] , tx . pure . address ( recipient ) ) ;
8488
@@ -97,6 +101,8 @@ async function main() {
97101 const signature = Buffer . from ( r + s , 'hex' ) ;
98102 const serialized = toSerializedSignature ( { signature, pubKey : publicKey } ) ;
99103
104+ // *** EXECUTION *** //
105+
100106 const result = await provider . executeTransactionBlock ( {
101107 transactionBlock : Buffer . from ( txBytes ) . toString ( 'base64' ) ,
102108 signature : serialized ,
0 commit comments