forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransaction.test.ts
965 lines (871 loc) · 33.5 KB
/
transaction.test.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
import bs58 from 'bs58';
import {Buffer} from 'buffer';
import {expect} from 'chai';
import {Connection} from '../src/connection';
import {Keypair} from '../src/keypair';
import {PublicKey} from '../src/publickey';
import {
Transaction,
TransactionInstruction,
VersionedTransaction,
} from '../src/transaction';
import {StakeProgram, SystemProgram} from '../src/programs';
import {Message} from '../src/message';
import invariant from '../src/utils/assert';
import {toBuffer} from '../src/utils/to-buffer';
import {helpers} from './mocks/rpc-http';
import {url} from './url';
import {sign} from '../src/utils/ed25519';
describe('Transaction', () => {
describe('compileMessage', () => {
it('accountKeys are ordered', () => {
// These pubkeys are chosen specially to be in sort order.
const payer = new PublicKey(
'3qMLYYyNvaxNZP7nW8u5abHMoJthYqQehRLbFVPNNcvQ',
);
const accountWritableSigner2 = new PublicKey(
'3XLtLo5Z4DG8b6PteJidF6kFPNDfxWjxv4vTLrjaHTvd',
);
const accountWritableSigner3 = new PublicKey(
'4rvqGPb4sXgyUKQcvmPxnWEZTTiTqNUZ2jjnw7atKVxa',
);
const accountSigner4 = new PublicKey(
'5oGjWjyoKDoXGpboGBfqm9a5ZscyAjRi3xuGYYu1ayQg',
);
const accountSigner5 = new PublicKey(
'65Rkc3VmDEV6zTRGtgdwkTcQUxDJnJszj2s4WoXazYpC',
);
const accountWritable6 = new PublicKey(
'72BxBZ9eD9Ue6zoJ9bzfit7MuaDAnq1qhirgAoFUXz9q',
);
const accountWritable7 = new PublicKey(
'BtYrPUeVphVgRHJkf2bKz8DLRxJdQmZyANrTM12xFqZL',
);
const accountRegular8 = new PublicKey(
'Di1MbqFwpodKzNrkjGaUHhXC4TJ1SHUAxo9agPZphNH1',
);
const accountRegular9 = new PublicKey(
'DYzzsfHTgaNhCgn7wMaciAYuwYsGqtVNg9PeFZhH93Pc',
);
const programId = new PublicKey(
'Fx9svCTdxnACvmEmx672v2kP1or4G1zC73tH7XsXbKkP',
);
const recentBlockhash = Keypair.generate().publicKey.toBase58();
const transaction = new Transaction({
blockhash: recentBlockhash,
lastValidBlockHeight: 9999,
}).add({
keys: [
// Regular accounts
{pubkey: accountRegular9, isSigner: false, isWritable: false},
{pubkey: accountRegular8, isSigner: false, isWritable: false},
// Writable accounts
{pubkey: accountWritable7, isSigner: false, isWritable: true},
{pubkey: accountWritable6, isSigner: false, isWritable: true},
// Signers
{pubkey: accountSigner5, isSigner: true, isWritable: false},
{pubkey: accountSigner4, isSigner: true, isWritable: false},
// Writable Signers
{pubkey: accountWritableSigner3, isSigner: true, isWritable: true},
{pubkey: accountWritableSigner2, isSigner: true, isWritable: true},
// Payer.
{pubkey: payer, isSigner: true, isWritable: true},
],
programId,
});
transaction.feePayer = payer;
const message = transaction.compileMessage();
// Payer comes first.
expect(message.accountKeys[0].equals(payer)).to.be.true;
// Writable signers come next, in pubkey order.
expect(message.accountKeys[1].equals(accountWritableSigner2)).to.be.true;
expect(message.accountKeys[2].equals(accountWritableSigner3)).to.be.true;
// Signers come next, in pubkey order.
expect(message.accountKeys[3].equals(accountSigner4)).to.be.true;
expect(message.accountKeys[4].equals(accountSigner5)).to.be.true;
// Writable accounts come next, in pubkey order.
expect(message.accountKeys[5].equals(accountWritable6)).to.be.true;
expect(message.accountKeys[6].equals(accountWritable7)).to.be.true;
// Everything else afterward, in pubkey order.
expect(message.accountKeys[7].equals(accountRegular8)).to.be.true;
expect(message.accountKeys[8].equals(accountRegular9)).to.be.true;
expect(message.accountKeys[9].equals(programId)).to.be.true;
});
it('accountKeys collapses signedness and writability of duplicate accounts', () => {
// These pubkeys are chosen specially to be in sort order.
const payer = new PublicKey(
'2eBgaMN8dCnCjx8B8Wrwk974v5WHwA6Vvj4N2mW9KDyt',
);
const account2 = new PublicKey(
'DL8FErokCN7rerLdmJ7tQvsL1FsqDu1sTKLLooWmChiW',
);
const account3 = new PublicKey(
'EdPiTYbXFxNrn1vqD7ZdDyauRKG4hMR6wY54RU1YFP2e',
);
const account4 = new PublicKey(
'FThXbyKK4kYJBngSSuvo9e6kc7mwPHEgw4V8qdmz1h3k',
);
const programId = new PublicKey(
'Gcatgv533efD1z2knsH9UKtkrjRWCZGi12f8MjNaDzmN',
);
const account5 = new PublicKey(
'rBtwG4bx85Exjr9cgoupvP1c7VTe7u5B36rzCg1HYgi',
);
const recentBlockhash = Keypair.generate().publicKey.toBase58();
const transaction = new Transaction({
blockhash: recentBlockhash,
lastValidBlockHeight: 9999,
}).add({
keys: [
// Should sort last.
{pubkey: account5, isSigner: false, isWritable: false},
{pubkey: account5, isSigner: false, isWritable: false},
// Should be considered writeable.
{pubkey: account4, isSigner: false, isWritable: false},
{pubkey: account4, isSigner: false, isWritable: true},
// Should be considered a signer.
{pubkey: account3, isSigner: false, isWritable: false},
{pubkey: account3, isSigner: true, isWritable: false},
// Should be considered a writable signer.
{pubkey: account2, isSigner: false, isWritable: true},
{pubkey: account2, isSigner: true, isWritable: false},
// Payer.
{pubkey: payer, isSigner: true, isWritable: true},
],
programId,
});
transaction.feePayer = payer;
const message = transaction.compileMessage();
// Payer comes first.
expect(message.accountKeys[0].equals(payer)).to.be.true;
// Writable signer comes first.
expect(message.accountKeys[1].equals(account2)).to.be.true;
// Signer comes next.
expect(message.accountKeys[2].equals(account3)).to.be.true;
// Writable account comes next.
expect(message.accountKeys[3].equals(account4)).to.be.true;
// Regular accounts come last.
expect(message.accountKeys[4].equals(programId)).to.be.true;
expect(message.accountKeys[5].equals(account5)).to.be.true;
});
it('payer is first account meta', () => {
const payer = Keypair.generate();
const other = Keypair.generate();
const recentBlockhash = Keypair.generate().publicKey.toBase58();
const programId = Keypair.generate().publicKey;
const transaction = new Transaction({
blockhash: recentBlockhash,
lastValidBlockHeight: 9999,
}).add({
keys: [
{pubkey: other.publicKey, isSigner: true, isWritable: true},
{pubkey: payer.publicKey, isSigner: true, isWritable: true},
],
programId,
});
transaction.sign(payer, other);
const message = transaction.compileMessage();
expect(message.accountKeys[0]).to.eql(payer.publicKey);
expect(message.accountKeys[1]).to.eql(other.publicKey);
expect(message.header.numRequiredSignatures).to.eq(2);
expect(message.header.numReadonlySignedAccounts).to.eq(0);
expect(message.header.numReadonlyUnsignedAccounts).to.eq(1);
});
it('validation', () => {
const payer = Keypair.generate();
const recentBlockhash = Keypair.generate().publicKey.toBase58();
const transaction = new Transaction();
expect(() => {
transaction.compileMessage();
}).to.throw('Transaction recentBlockhash required');
transaction.recentBlockhash = recentBlockhash;
expect(() => {
transaction.compileMessage();
}).to.throw('Transaction fee payer required');
transaction.setSigners(payer.publicKey, Keypair.generate().publicKey);
expect(() => {
transaction.compileMessage();
}).to.throw('unknown signer');
// Expect compile to succeed with implicit fee payer from signers
transaction.setSigners(payer.publicKey);
transaction.compileMessage();
// Expect compile to succeed with fee payer and no signers
transaction.signatures = [];
transaction.feePayer = payer.publicKey;
transaction.compileMessage();
});
it('payer is writable', () => {
const payer = Keypair.generate();
const recentBlockhash = Keypair.generate().publicKey.toBase58();
const programId = Keypair.generate().publicKey;
const transaction = new Transaction({
blockhash: recentBlockhash,
lastValidBlockHeight: 9999,
}).add({
keys: [{pubkey: payer.publicKey, isSigner: true, isWritable: false}],
programId,
});
transaction.sign(payer);
const message = transaction.compileMessage();
expect(message.accountKeys[0]).to.eql(payer.publicKey);
expect(message.header.numRequiredSignatures).to.eq(1);
expect(message.header.numReadonlySignedAccounts).to.eq(0);
expect(message.header.numReadonlyUnsignedAccounts).to.eq(1);
});
it('uses the nonce as the recent blockhash when compiling nonce-based transactions', () => {
const nonce = new PublicKey(1);
const nonceAuthority = new PublicKey(2);
const nonceInfo = {
nonce: nonce.toBase58(),
nonceInstruction: SystemProgram.nonceAdvance({
noncePubkey: nonce,
authorizedPubkey: nonceAuthority,
}),
};
const transaction = new Transaction({
feePayer: nonceAuthority,
nonceInfo,
});
const message = transaction.compileMessage();
expect(message.recentBlockhash).to.equal(nonce.toBase58());
});
it('prepends the nonce advance instruction when compiling nonce-based transactions', () => {
const nonce = new PublicKey(1);
const nonceAuthority = new PublicKey(2);
const nonceInfo = {
nonce: nonce.toBase58(),
nonceInstruction: SystemProgram.nonceAdvance({
noncePubkey: nonce,
authorizedPubkey: nonceAuthority,
}),
};
const transaction = new Transaction({
feePayer: nonceAuthority,
nonceInfo,
}).add(
SystemProgram.transfer({
fromPubkey: nonceAuthority,
lamports: 1,
toPubkey: new PublicKey(3),
}),
);
const message = transaction.compileMessage();
expect(message.instructions).to.have.length(2);
const expectedNonceAdvanceCompiledInstruction = {
accounts: [1, 4, 0],
data: (() => {
const expectedData = Buffer.alloc(4);
expectedData.writeInt32LE(
4 /* SystemInstruction::AdvanceNonceAccount */,
0,
);
return bs58.encode(expectedData);
})(),
programIdIndex: (() => {
let foundIndex = -1;
message.accountKeys.find((publicKey, ii) => {
if (publicKey.equals(SystemProgram.programId)) {
foundIndex = ii;
return true;
}
});
return foundIndex;
})(),
};
expect(message.instructions[0]).to.deep.equal(
expectedNonceAdvanceCompiledInstruction,
);
});
it('does not prepend the nonce advance instruction when compiling nonce-based transactions if it is already there', () => {
const nonce = new PublicKey(1);
const nonceAuthority = new PublicKey(2);
const nonceInfo = {
nonce: nonce.toBase58(),
nonceInstruction: SystemProgram.nonceAdvance({
noncePubkey: nonce,
authorizedPubkey: nonceAuthority,
}),
};
const transaction = new Transaction({
feePayer: nonceAuthority,
nonceInfo,
})
.add(nonceInfo.nonceInstruction)
.add(
SystemProgram.transfer({
fromPubkey: nonceAuthority,
lamports: 1,
toPubkey: new PublicKey(3),
}),
);
const message = transaction.compileMessage();
expect(message.instructions).to.have.length(2);
const expectedNonceAdvanceCompiledInstruction = {
accounts: [1, 4, 0],
data: (() => {
const expectedData = Buffer.alloc(4);
expectedData.writeInt32LE(
4 /* SystemInstruction::AdvanceNonceAccount */,
0,
);
return bs58.encode(expectedData);
})(),
programIdIndex: (() => {
let foundIndex = -1;
message.accountKeys.find((publicKey, ii) => {
if (publicKey.equals(SystemProgram.programId)) {
foundIndex = ii;
return true;
}
});
return foundIndex;
})(),
};
expect(message.instructions[0]).to.deep.equal(
expectedNonceAdvanceCompiledInstruction,
);
});
});
if (process.env.TEST_LIVE) {
it('getEstimatedFee', async () => {
const connection = new Connection(url);
const accountFrom = Keypair.generate();
const accountTo = Keypair.generate();
const latestBlockhash = await helpers.latestBlockhash({connection});
const transaction = new Transaction({
feePayer: accountFrom.publicKey,
...latestBlockhash,
}).add(
SystemProgram.transfer({
fromPubkey: accountFrom.publicKey,
toPubkey: accountTo.publicKey,
lamports: 10,
}),
);
const fee = await transaction.getEstimatedFee(connection);
expect(fee).to.eq(5000);
});
}
it('partialSign', () => {
const account1 = Keypair.generate();
const account2 = Keypair.generate();
const recentBlockhash = account1.publicKey.toBase58(); // Fake recentBlockhash
const transfer = SystemProgram.transfer({
fromPubkey: account1.publicKey,
toPubkey: account2.publicKey,
lamports: 123,
});
const transaction = new Transaction({
blockhash: recentBlockhash,
lastValidBlockHeight: 9999,
}).add(transfer);
transaction.sign(account1, account2);
const partialTransaction = new Transaction({
blockhash: recentBlockhash,
lastValidBlockHeight: 9999,
}).add(transfer);
partialTransaction.setSigners(account1.publicKey, account2.publicKey);
expect(partialTransaction.signatures[0].signature).to.be.null;
expect(partialTransaction.signatures[1].signature).to.be.null;
partialTransaction.partialSign(account1);
expect(partialTransaction.signatures[0].signature).not.to.be.null;
expect(partialTransaction.signatures[1].signature).to.be.null;
expect(() => partialTransaction.serialize()).to.throw();
expect(() =>
partialTransaction.serialize({requireAllSignatures: false}),
).not.to.throw();
partialTransaction.partialSign(account2);
expect(partialTransaction.signatures[0].signature).not.to.be.null;
expect(partialTransaction.signatures[1].signature).not.to.be.null;
expect(() => partialTransaction.serialize()).not.to.throw();
expect(partialTransaction).to.eql(transaction);
invariant(partialTransaction.signatures[0].signature);
partialTransaction.signatures[0].signature.fill(1);
expect(() =>
partialTransaction.serialize({requireAllSignatures: false}),
).to.throw();
expect(() =>
partialTransaction.serialize({
verifySignatures: false,
requireAllSignatures: false,
}),
).not.to.throw();
});
describe('dedupe', () => {
const payer = Keypair.generate();
const duplicate1 = payer;
const duplicate2 = payer;
const recentBlockhash = Keypair.generate().publicKey.toBase58();
const programId = Keypair.generate().publicKey;
it('setSigners', () => {
const transaction = new Transaction({
blockhash: recentBlockhash,
lastValidBlockHeight: 9999,
}).add({
keys: [
{pubkey: duplicate1.publicKey, isSigner: true, isWritable: true},
{pubkey: payer.publicKey, isSigner: false, isWritable: true},
{pubkey: duplicate2.publicKey, isSigner: true, isWritable: false},
],
programId,
});
transaction.setSigners(
payer.publicKey,
duplicate1.publicKey,
duplicate2.publicKey,
);
expect(transaction.signatures).to.have.length(1);
expect(transaction.signatures[0].publicKey).to.eql(payer.publicKey);
const message = transaction.compileMessage();
expect(message.accountKeys[0]).to.eql(payer.publicKey);
expect(message.header.numRequiredSignatures).to.eq(1);
expect(message.header.numReadonlySignedAccounts).to.eq(0);
expect(message.header.numReadonlyUnsignedAccounts).to.eq(1);
transaction.signatures;
});
it('sign', () => {
const transaction = new Transaction({
blockhash: recentBlockhash,
lastValidBlockHeight: 9999,
}).add({
keys: [
{pubkey: duplicate1.publicKey, isSigner: true, isWritable: true},
{pubkey: payer.publicKey, isSigner: false, isWritable: true},
{pubkey: duplicate2.publicKey, isSigner: true, isWritable: false},
],
programId,
});
transaction.sign(payer, duplicate1, duplicate2);
expect(transaction.signatures).to.have.length(1);
expect(transaction.signatures[0].publicKey).to.eql(payer.publicKey);
const message = transaction.compileMessage();
expect(message.accountKeys[0]).to.eql(payer.publicKey);
expect(message.header.numRequiredSignatures).to.eq(1);
expect(message.header.numReadonlySignedAccounts).to.eq(0);
expect(message.header.numReadonlyUnsignedAccounts).to.eq(1);
transaction.signatures;
});
});
it('transfer signatures', () => {
const account1 = Keypair.generate();
const account2 = Keypair.generate();
const recentBlockhash = account1.publicKey.toBase58(); // Fake recentBlockhash
const transfer1 = SystemProgram.transfer({
fromPubkey: account1.publicKey,
toPubkey: account2.publicKey,
lamports: 123,
});
const transfer2 = SystemProgram.transfer({
fromPubkey: account2.publicKey,
toPubkey: account1.publicKey,
lamports: 123,
});
const latestBlockhash = {
blockhash: recentBlockhash,
lastValidBlockHeight: 9999,
};
const orgTransaction = new Transaction({
...latestBlockhash,
}).add(transfer1, transfer2);
orgTransaction.sign(account1, account2);
const newTransaction = new Transaction({
...latestBlockhash,
signatures: orgTransaction.signatures,
}).add(transfer1, transfer2);
expect(newTransaction).to.eql(orgTransaction);
});
it('dedup signatures', () => {
const account1 = Keypair.generate();
const account2 = Keypair.generate();
const recentBlockhash = account1.publicKey.toBase58(); // Fake recentBlockhash
const transfer1 = SystemProgram.transfer({
fromPubkey: account1.publicKey,
toPubkey: account2.publicKey,
lamports: 123,
});
const transfer2 = SystemProgram.transfer({
fromPubkey: account1.publicKey,
toPubkey: account2.publicKey,
lamports: 123,
});
const orgTransaction = new Transaction({
blockhash: recentBlockhash,
lastValidBlockHeight: 9999,
}).add(transfer1, transfer2);
orgTransaction.sign(account1);
});
it('use nonce', () => {
const account1 = Keypair.generate();
const account2 = Keypair.generate();
const nonceAccount = Keypair.generate();
const nonce = account2.publicKey.toBase58(); // Fake Nonce hash
const nonceInfo = {
nonce,
nonceInstruction: SystemProgram.nonceAdvance({
noncePubkey: nonceAccount.publicKey,
authorizedPubkey: account1.publicKey,
}),
};
const transferTransaction = new Transaction({nonceInfo}).add(
SystemProgram.transfer({
fromPubkey: account1.publicKey,
toPubkey: account2.publicKey,
lamports: 123,
}),
);
transferTransaction.sign(account1);
expect(transferTransaction.instructions).to.have.length(1);
expect(transferTransaction.recentBlockhash).to.be.undefined;
const stakeAccount = Keypair.generate();
const voteAccount = Keypair.generate();
const stakeTransaction = new Transaction({nonceInfo}).add(
StakeProgram.delegate({
stakePubkey: stakeAccount.publicKey,
authorizedPubkey: account1.publicKey,
votePubkey: voteAccount.publicKey,
}),
);
stakeTransaction.sign(account1);
expect(stakeTransaction.instructions).to.have.length(1);
expect(stakeTransaction.recentBlockhash).to.be.undefined;
});
it('parse wire format and serialize', () => {
const sender = Keypair.fromSeed(Uint8Array.from(Array(32).fill(8))); // Arbitrary known account
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
const recipient = new PublicKey(
'J3dxNj7nDRRqRRXuEMynDG57DkZK4jYRuv3Garmb1i99',
); // Arbitrary known public key
const transfer = SystemProgram.transfer({
fromPubkey: sender.publicKey,
toPubkey: recipient,
lamports: 49,
});
const expectedTransaction = new Transaction({
blockhash: recentBlockhash,
feePayer: sender.publicKey,
lastValidBlockHeight: 9999,
}).add(transfer);
expectedTransaction.sign(sender);
const serializedTransaction = Buffer.from(
'AVuErQHaXv0SG0/PchunfxHKt8wMRfMZzqV0tkC5qO6owYxWU2v871AoWywGoFQr4z+q/7mE8lIufNl/kxj+nQ0BAAEDE5j2LG0aRXxRumpLXz29L2n8qTIWIY3ImX5Ba9F9k8r9Q5/Mtmcn8onFxt47xKj+XdXXd3C8j/FcPu7csUrz/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAxJrndgN4IFTxep3s6kO0ROug7bEsbx0xxuDkqEvwUusBAgIAAQwCAAAAMQAAAAAAAAA=',
'base64',
);
const deserializedTransaction = Transaction.from(serializedTransaction);
expect(expectedTransaction.serialize()).to.eql(serializedTransaction);
expect(deserializedTransaction.serialize()).to.eql(serializedTransaction);
});
it('populate transaction', () => {
const recentBlockhash = new PublicKey(1).toString();
const message = {
accountKeys: [
new PublicKey(1).toString(),
new PublicKey(2).toString(),
new PublicKey(3).toString(),
new PublicKey(4).toString(),
new PublicKey(5).toString(),
],
header: {
numReadonlySignedAccounts: 0,
numReadonlyUnsignedAccounts: 3,
numRequiredSignatures: 2,
},
instructions: [
{
accounts: [1, 2, 3],
data: bs58.encode(Buffer.alloc(5).fill(9)),
programIdIndex: 4,
},
],
recentBlockhash,
};
const signatures = [
bs58.encode(Buffer.alloc(64).fill(1)),
bs58.encode(Buffer.alloc(64).fill(2)),
];
const transaction = Transaction.populate(new Message(message), signatures);
expect(transaction.instructions).to.have.length(1);
expect(transaction.signatures).to.have.length(2);
expect(transaction.recentBlockhash).to.eq(recentBlockhash);
});
it('populate then compile transaction', () => {
const recentBlockhash = new PublicKey(1).toString();
const message = new Message({
accountKeys: [
new PublicKey(1).toString(),
new PublicKey(2).toString(),
new PublicKey(3).toString(),
new PublicKey(4).toString(),
new PublicKey(5).toString(),
],
header: {
numReadonlySignedAccounts: 0,
numReadonlyUnsignedAccounts: 3,
numRequiredSignatures: 2,
},
instructions: [
{
accounts: [1, 2, 3],
data: bs58.encode(Buffer.alloc(5).fill(9)),
programIdIndex: 2,
},
],
recentBlockhash,
});
const signatures = [
bs58.encode(Buffer.alloc(64).fill(1)),
bs58.encode(Buffer.alloc(64).fill(2)),
];
const transaction = Transaction.populate(message, signatures);
const compiledMessage = transaction.compileMessage();
expect(compiledMessage).to.eql(message);
// show that without caching the message, the populated message
// might not be the same when re-compiled
transaction._message = undefined;
const compiledMessage2 = transaction.compileMessage();
expect(compiledMessage2).not.to.eql(message);
// show that even if message is cached, transaction may still
// be modified
transaction._message = message;
transaction.recentBlockhash = new PublicKey(100).toString();
const compiledMessage3 = transaction.compileMessage();
expect(compiledMessage3).not.to.eql(message);
});
it('constructs a transaction with nonce info', () => {
const nonce = new PublicKey(1);
const nonceAuthority = new PublicKey(2);
const nonceInfo = {
nonce: nonce.toBase58(),
nonceInstruction: SystemProgram.nonceAdvance({
noncePubkey: nonce,
authorizedPubkey: nonceAuthority,
}),
};
const transaction = new Transaction({nonceInfo});
expect(transaction.recentBlockhash).to.be.undefined;
expect(transaction.lastValidBlockHeight).to.be.undefined;
expect(transaction.nonceInfo).to.equal(nonceInfo);
});
it('constructs a transaction with last valid block height', () => {
const blockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k';
const lastValidBlockHeight = 1234;
const transaction = new Transaction({
blockhash,
lastValidBlockHeight,
});
expect(transaction.recentBlockhash).to.eq(blockhash);
expect(transaction.lastValidBlockHeight).to.eq(lastValidBlockHeight);
});
it('constructs a transaction with only a recent blockhash', () => {
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k';
const transaction = new Transaction({
recentBlockhash,
});
expect(transaction.recentBlockhash).to.eq(recentBlockhash);
expect(transaction.lastValidBlockHeight).to.be.undefined;
});
it('serialize unsigned transaction', () => {
const sender = Keypair.fromSeed(Uint8Array.from(Array(32).fill(8))); // Arbitrary known account
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
const recipient = new PublicKey(
'J3dxNj7nDRRqRRXuEMynDG57DkZK4jYRuv3Garmb1i99',
); // Arbitrary known public key
const transfer = SystemProgram.transfer({
fromPubkey: sender.publicKey,
toPubkey: recipient,
lamports: 49,
});
const expectedTransaction = new Transaction({
blockhash: recentBlockhash,
lastValidBlockHeight: 9999,
}).add(transfer);
// Empty signature array fails.
expect(expectedTransaction.signatures).to.have.length(0);
expect(() => {
expectedTransaction.serialize();
}).to.throw('Transaction fee payer required');
expect(() => {
expectedTransaction.serialize({verifySignatures: false});
}).to.throw('Transaction fee payer required');
expect(() => {
expectedTransaction.serializeMessage();
}).to.throw('Transaction fee payer required');
expectedTransaction.feePayer = sender.publicKey;
// Transactions with missing signatures will fail sigverify.
expect(() => {
expectedTransaction.serialize();
}).to.throw('Signature verification failed');
// Serializing without signatures is allowed if sigverify disabled.
expectedTransaction.serialize({verifySignatures: false});
// Serializing the message is allowed when signature array has null signatures
expectedTransaction.serializeMessage();
expectedTransaction.feePayer = undefined;
expectedTransaction.setSigners(sender.publicKey);
expect(expectedTransaction.signatures).to.have.length(1);
// Transactions with missing signatures will fail sigverify.
expect(() => {
expectedTransaction.serialize();
}).to.throw('Signature verification failed');
// Serializing without signatures is allowed if sigverify disabled.
expectedTransaction.serialize({verifySignatures: false});
// Serializing the message is allowed when signature array has null signatures
expectedTransaction.serializeMessage();
const expectedSerializationWithNoSignatures = Buffer.from(
'AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' +
'AAAAAAAAAAAAAAAAAAABAAEDE5j2LG0aRXxRumpLXz29L2n8qTIWIY3ImX5Ba9F9k8r9' +
'Q5/Mtmcn8onFxt47xKj+XdXXd3C8j/FcPu7csUrz/AAAAAAAAAAAAAAAAAAAAAAAAAAA' +
'AAAAAAAAAAAAAAAAxJrndgN4IFTxep3s6kO0ROug7bEsbx0xxuDkqEvwUusBAgIAAQwC' +
'AAAAMQAAAAAAAAA=',
'base64',
);
expect(expectedTransaction.serialize({requireAllSignatures: false})).to.eql(
expectedSerializationWithNoSignatures,
);
// Properly signed transaction succeeds
expectedTransaction.partialSign(sender);
expect(expectedTransaction.signatures).to.have.length(1);
const expectedSerialization = Buffer.from(
'AVuErQHaXv0SG0/PchunfxHKt8wMRfMZzqV0tkC5qO6owYxWU2v871AoWywGoFQr4z+q/7mE8lIufNl/' +
'kxj+nQ0BAAEDE5j2LG0aRXxRumpLXz29L2n8qTIWIY3ImX5Ba9F9k8r9Q5/Mtmcn8onFxt47xKj+XdXX' +
'd3C8j/FcPu7csUrz/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAxJrndgN4IFTxep3s6kO0' +
'ROug7bEsbx0xxuDkqEvwUusBAgIAAQwCAAAAMQAAAAAAAAA=',
'base64',
);
expect(expectedTransaction.serialize()).to.eql(expectedSerialization);
expect(expectedTransaction.signatures).to.have.length(1);
});
it('deprecated - externally signed stake delegate', () => {
const authority = Keypair.fromSeed(Uint8Array.from(Array(32).fill(1)));
const stake = new PublicKey(2);
const recentBlockhash = new PublicKey(3).toBuffer();
const vote = new PublicKey(4);
var tx = StakeProgram.delegate({
stakePubkey: stake,
authorizedPubkey: authority.publicKey,
votePubkey: vote,
});
const from = authority;
tx.recentBlockhash = bs58.encode(recentBlockhash);
tx.setSigners(from.publicKey);
const tx_bytes = tx.serializeMessage();
const signature = sign(tx_bytes, from.secretKey);
tx.addSignature(from.publicKey, toBuffer(signature));
expect(tx.verifySignatures()).to.be.true;
});
it('externally signed stake delegate', () => {
const authority = Keypair.fromSeed(Uint8Array.from(Array(32).fill(1)));
const stake = new PublicKey(2);
const recentBlockhash = new PublicKey(3).toBuffer();
const vote = new PublicKey(4);
var tx = StakeProgram.delegate({
stakePubkey: stake,
authorizedPubkey: authority.publicKey,
votePubkey: vote,
});
const from = authority;
tx.recentBlockhash = bs58.encode(recentBlockhash);
tx.feePayer = from.publicKey;
const tx_bytes = tx.serializeMessage();
const signature = sign(tx_bytes, from.secretKey);
tx.addSignature(from.publicKey, toBuffer(signature));
expect(tx.verifySignatures()).to.be.true;
});
it('deserializes versioned transactions', () => {
const serializedVersionedTx = Buffer.from(
'AdTIDASR42TgVuXKkd7mJKk373J3LPVp85eyKMVcrboo9KTY8/vm6N/Cv0NiHqk2I8iYw6VX5ZaBKG8z' +
'9l1XjwiAAQACA+6qNbqfjaIENwt9GzEK/ENiB/ijGwluzBUmQ9xlTAMcCaS0ctnyxTcXXlJr7u2qtnaM' +
'gIAO2/c7RBD0ipHWUcEDBkZv5SEXMv/srbpyw5vnvIzlu8X3EmssQ5s6QAAAAJbI7VNs6MzREUlnzRaJ' +
'pBKP8QQoDn2dWQvD0KIgHFDiAwIACQAgoQcAAAAAAAIABQEAAAQAATYPBwAKBDIBAyQWIw0oCxIdCA4i' +
'JzQRKwUZHxceHCohMBUJJiwpMxAaGC0TLhQxGyAMBiU2NS8VDgAAAADuAgAAAAAAAAIAAAAAAAAAAdGCT' +
'Qiq5yw3+3m1sPoRNj0GtUNNs0FIMocxzt3zuoSZHQABAwQFBwgLDA8RFBcYGhwdHh8iIyUnKiwtLi8yF' +
'wIGCQoNDhASExUWGRsgISQmKCkrMDEz',
'base64',
);
expect(() => Transaction.from(serializedVersionedTx)).to.throw(
'Versioned messages must be deserialized with VersionedMessage.deserialize()',
);
const versionedTx = VersionedTransaction.deserialize(serializedVersionedTx);
expect(versionedTx.message.version).to.eq(0);
});
it('can serialize, deserialize, and reserialize with a partial signer', () => {
const signer = Keypair.generate();
const acc0Writable = Keypair.generate();
const acc1Writable = Keypair.generate();
const acc2Writable = Keypair.generate();
const t0 = new Transaction({
blockhash: 'HZaTsZuhN1aaz9WuuimCFMyH7wJ5xiyMUHFCnZSMyguH',
feePayer: signer.publicKey,
lastValidBlockHeight: 9999,
});
t0.add(
new TransactionInstruction({
keys: [
{
pubkey: signer.publicKey,
isWritable: true,
isSigner: true,
},
{
pubkey: acc0Writable.publicKey,
isWritable: true,
isSigner: false,
},
],
programId: Keypair.generate().publicKey,
}),
);
t0.add(
new TransactionInstruction({
keys: [
{
pubkey: acc1Writable.publicKey,
isWritable: false,
isSigner: false,
},
],
programId: Keypair.generate().publicKey,
}),
);
t0.add(
new TransactionInstruction({
keys: [
{
pubkey: acc2Writable.publicKey,
isWritable: true,
isSigner: false,
},
],
programId: Keypair.generate().publicKey,
}),
);
t0.add(
new TransactionInstruction({
keys: [
{
pubkey: signer.publicKey,
isWritable: true,
isSigner: true,
},
{
pubkey: acc0Writable.publicKey,
isWritable: false,
isSigner: false,
},
{
pubkey: acc2Writable.publicKey,
isWritable: false,
isSigner: false,
},
{
pubkey: acc1Writable.publicKey,
isWritable: true,
isSigner: false,
},
],
programId: Keypair.generate().publicKey,
}),
);
const t1 = Transaction.from(t0.serialize({requireAllSignatures: false}));
t1.partialSign(signer);
t1.serialize();
});
});