@solana/web3.js 1.57.0 → 1.59.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,138 @@
1
+ import {AccountKeysFromLookups} from '../message/account-keys';
2
+ import assert from '../utils/assert';
3
+ import {toBuffer} from '../utils/to-buffer';
4
+ import {Blockhash} from '../blockhash';
5
+ import {Message, MessageV0, VersionedMessage} from '../message';
6
+ import {PublicKey} from '../publickey';
7
+ import {AddressLookupTableAccount} from '../programs';
8
+ import {AccountMeta, TransactionInstruction} from './legacy';
9
+
10
+ export type TransactionMessageArgs = {
11
+ payerKey: PublicKey;
12
+ instructions: Array<TransactionInstruction>;
13
+ recentBlockhash: Blockhash;
14
+ };
15
+
16
+ export type DecompileArgs =
17
+ | {
18
+ accountKeysFromLookups: AccountKeysFromLookups;
19
+ }
20
+ | {
21
+ addressLookupTableAccounts: AddressLookupTableAccount[];
22
+ };
23
+
24
+ export class TransactionMessage {
25
+ payerKey: PublicKey;
26
+ instructions: Array<TransactionInstruction>;
27
+ recentBlockhash: Blockhash;
28
+
29
+ constructor(args: TransactionMessageArgs) {
30
+ this.payerKey = args.payerKey;
31
+ this.instructions = args.instructions;
32
+ this.recentBlockhash = args.recentBlockhash;
33
+ }
34
+
35
+ static decompile(
36
+ message: VersionedMessage,
37
+ args?: DecompileArgs,
38
+ ): TransactionMessage {
39
+ const {header, compiledInstructions, recentBlockhash} = message;
40
+
41
+ const {
42
+ numRequiredSignatures,
43
+ numReadonlySignedAccounts,
44
+ numReadonlyUnsignedAccounts,
45
+ } = header;
46
+
47
+ const numWritableSignedAccounts =
48
+ numRequiredSignatures - numReadonlySignedAccounts;
49
+ assert(numWritableSignedAccounts > 0, 'Message header is invalid');
50
+
51
+ const numWritableUnsignedAccounts =
52
+ message.staticAccountKeys.length - numReadonlyUnsignedAccounts;
53
+ assert(numWritableUnsignedAccounts >= 0, 'Message header is invalid');
54
+
55
+ const accountKeys = message.getAccountKeys(args);
56
+ const payerKey = accountKeys.get(0);
57
+ if (payerKey === undefined) {
58
+ throw new Error(
59
+ 'Failed to decompile message because no account keys were found',
60
+ );
61
+ }
62
+
63
+ const instructions: TransactionInstruction[] = [];
64
+ for (const compiledIx of compiledInstructions) {
65
+ const keys: AccountMeta[] = [];
66
+
67
+ for (const keyIndex of compiledIx.accountKeyIndexes) {
68
+ const pubkey = accountKeys.get(keyIndex);
69
+ if (pubkey === undefined) {
70
+ throw new Error(
71
+ `Failed to find key for account key index ${keyIndex}`,
72
+ );
73
+ }
74
+
75
+ const isSigner = keyIndex < numRequiredSignatures;
76
+
77
+ let isWritable;
78
+ if (isSigner) {
79
+ isWritable = keyIndex < numWritableSignedAccounts;
80
+ } else if (keyIndex < accountKeys.staticAccountKeys.length) {
81
+ isWritable =
82
+ keyIndex - numRequiredSignatures < numWritableUnsignedAccounts;
83
+ } else {
84
+ isWritable =
85
+ keyIndex - accountKeys.staticAccountKeys.length <
86
+ // accountKeysFromLookups cannot be undefined because we already found a pubkey for this index above
87
+ accountKeys.accountKeysFromLookups!.writable.length;
88
+ }
89
+
90
+ keys.push({
91
+ pubkey,
92
+ isSigner: keyIndex < header.numRequiredSignatures,
93
+ isWritable,
94
+ });
95
+ }
96
+
97
+ const programId = accountKeys.get(compiledIx.programIdIndex);
98
+ if (programId === undefined) {
99
+ throw new Error(
100
+ `Failed to find program id for program id index ${compiledIx.programIdIndex}`,
101
+ );
102
+ }
103
+
104
+ instructions.push(
105
+ new TransactionInstruction({
106
+ programId,
107
+ data: toBuffer(compiledIx.data),
108
+ keys,
109
+ }),
110
+ );
111
+ }
112
+
113
+ return new TransactionMessage({
114
+ payerKey,
115
+ instructions,
116
+ recentBlockhash,
117
+ });
118
+ }
119
+
120
+ compileToLegacyMessage(): Message {
121
+ return Message.compile({
122
+ payerKey: this.payerKey,
123
+ recentBlockhash: this.recentBlockhash,
124
+ instructions: this.instructions,
125
+ });
126
+ }
127
+
128
+ compileToV0Message(
129
+ addressLookupTableAccounts?: AddressLookupTableAccount[],
130
+ ): MessageV0 {
131
+ return MessageV0.compile({
132
+ payerKey: this.payerKey,
133
+ recentBlockhash: this.recentBlockhash,
134
+ instructions: this.instructions,
135
+ addressLookupTableAccounts,
136
+ });
137
+ }
138
+ }