@solana/transactions 2.0.0-experimental.2bae733 → 2.0.0-experimental.2e0ae95
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.
- package/LICENSE +1 -1
- package/README.md +276 -5
- package/dist/index.browser.cjs +644 -285
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +623 -287
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +623 -287
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +644 -285
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +623 -287
- package/dist/index.node.js.map +1 -1
- package/dist/types/accounts.d.ts +3 -3
- package/dist/types/accounts.d.ts.map +1 -0
- package/dist/types/blockhash.d.ts +7 -1
- package/dist/types/blockhash.d.ts.map +1 -0
- package/dist/types/compilable-transaction.d.ts +7 -0
- package/dist/types/compilable-transaction.d.ts.map +1 -0
- package/dist/types/compile-address-table-lookups.d.ts +3 -3
- package/dist/types/compile-address-table-lookups.d.ts.map +1 -0
- package/dist/types/compile-header.d.ts +1 -1
- package/dist/types/compile-header.d.ts.map +1 -0
- package/dist/types/compile-instructions.d.ts +2 -2
- package/dist/types/compile-instructions.d.ts.map +1 -0
- package/dist/types/compile-lifetime-token.d.ts +1 -1
- package/dist/types/compile-lifetime-token.d.ts.map +1 -0
- package/dist/types/compile-static-accounts.d.ts +3 -3
- package/dist/types/compile-static-accounts.d.ts.map +1 -0
- package/dist/types/compile-transaction.d.ts +10 -0
- package/dist/types/compile-transaction.d.ts.map +1 -0
- package/dist/types/create-transaction.d.ts +1 -1
- package/dist/types/create-transaction.d.ts.map +1 -0
- package/dist/types/decompile-transaction.d.ts +13 -0
- package/dist/types/decompile-transaction.d.ts.map +1 -0
- package/dist/types/durable-nonce.d.ts +23 -10
- package/dist/types/durable-nonce.d.ts.map +1 -0
- package/dist/types/fee-payer.d.ts +6 -5
- package/dist/types/fee-payer.d.ts.map +1 -0
- package/dist/types/index.d.ts +13 -7
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/instructions.d.ts +4 -2
- package/dist/types/instructions.d.ts.map +1 -0
- package/dist/types/message.d.ts +6 -10
- package/dist/types/message.d.ts.map +1 -0
- package/dist/types/serializers/address-table-lookup.d.ts +5 -3
- package/dist/types/serializers/address-table-lookup.d.ts.map +1 -0
- package/dist/types/serializers/header.d.ts +5 -3
- package/dist/types/serializers/header.d.ts.map +1 -0
- package/dist/types/serializers/index.d.ts +3 -0
- package/dist/types/serializers/index.d.ts.map +1 -0
- package/dist/types/serializers/instruction.d.ts +5 -3
- package/dist/types/serializers/instruction.d.ts.map +1 -0
- package/dist/types/serializers/message.d.ts +5 -5
- package/dist/types/serializers/message.d.ts.map +1 -0
- package/dist/types/serializers/transaction-version.d.ts +5 -5
- package/dist/types/serializers/transaction-version.d.ts.map +1 -0
- package/dist/types/serializers/transaction.d.ts +10 -0
- package/dist/types/serializers/transaction.d.ts.map +1 -0
- package/dist/types/signatures.d.ts +10 -7
- package/dist/types/signatures.d.ts.map +1 -0
- package/dist/types/types.d.ts +13 -13
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/unsigned-transaction.d.ts +4 -0
- package/dist/types/unsigned-transaction.d.ts.map +1 -0
- package/dist/types/wire-transaction.d.ts +6 -0
- package/dist/types/wire-transaction.d.ts.map +1 -0
- package/package.json +21 -40
- package/dist/index.development.js +0 -1188
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -15
- package/dist/types/serializers/unimplemented.d.ts +0 -3
package/dist/index.node.cjs
CHANGED
|
@@ -1,79 +1,90 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var errors = require('@solana/errors');
|
|
4
|
+
var codecsStrings = require('@solana/codecs-strings');
|
|
5
|
+
var addresses = require('@solana/addresses');
|
|
6
|
+
var functional = require('@solana/functional');
|
|
7
|
+
var codecsCore = require('@solana/codecs-core');
|
|
8
|
+
var codecsDataStructures = require('@solana/codecs-data-structures');
|
|
9
|
+
var codecsNumbers = require('@solana/codecs-numbers');
|
|
3
10
|
var keys = require('@solana/keys');
|
|
4
|
-
var umiSerializers = require('@metaplex-foundation/umi-serializers');
|
|
5
11
|
|
|
6
|
-
//
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
// src/blockhash.ts
|
|
13
|
+
var base58Encoder;
|
|
14
|
+
function assertIsBlockhash(putativeBlockhash) {
|
|
15
|
+
if (!base58Encoder)
|
|
16
|
+
base58Encoder = codecsStrings.getBase58Encoder();
|
|
17
|
+
try {
|
|
18
|
+
if (
|
|
19
|
+
// Lowest value (32 bytes of zeroes)
|
|
20
|
+
putativeBlockhash.length < 32 || // Highest value (32 bytes of 255)
|
|
21
|
+
putativeBlockhash.length > 44
|
|
22
|
+
) {
|
|
23
|
+
throw new Error("Expected input string to decode to a byte array of length 32.");
|
|
24
|
+
}
|
|
25
|
+
const bytes = base58Encoder.encode(putativeBlockhash);
|
|
26
|
+
const numBytes = bytes.byteLength;
|
|
27
|
+
if (numBytes !== 32) {
|
|
28
|
+
throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
|
|
29
|
+
}
|
|
30
|
+
} catch (e) {
|
|
31
|
+
throw new Error(`\`${putativeBlockhash}\` is not a blockhash`, {
|
|
32
|
+
cause: e
|
|
33
|
+
});
|
|
34
|
+
}
|
|
19
35
|
}
|
|
20
36
|
|
|
21
|
-
// src/
|
|
22
|
-
function
|
|
23
|
-
if ("feePayer" in transaction && feePayer === transaction.feePayer) {
|
|
24
|
-
return transaction;
|
|
25
|
-
}
|
|
26
|
-
let out;
|
|
37
|
+
// src/unsigned-transaction.ts
|
|
38
|
+
function getUnsignedTransaction(transaction) {
|
|
27
39
|
if ("signatures" in transaction) {
|
|
28
40
|
const {
|
|
29
41
|
signatures: _,
|
|
30
42
|
// eslint-disable-line @typescript-eslint/no-unused-vars
|
|
31
43
|
...unsignedTransaction
|
|
32
44
|
} = transaction;
|
|
33
|
-
|
|
34
|
-
...unsignedTransaction,
|
|
35
|
-
feePayer
|
|
36
|
-
};
|
|
45
|
+
return unsignedTransaction;
|
|
37
46
|
} else {
|
|
38
|
-
|
|
39
|
-
...transaction,
|
|
40
|
-
feePayer
|
|
41
|
-
};
|
|
47
|
+
return transaction;
|
|
42
48
|
}
|
|
43
|
-
Object.freeze(out);
|
|
44
|
-
return out;
|
|
45
49
|
}
|
|
46
50
|
|
|
47
|
-
// src/
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
...unsignedTransaction,
|
|
58
|
-
instructions: nextInstructions
|
|
59
|
-
};
|
|
60
|
-
} else {
|
|
61
|
-
out = {
|
|
62
|
-
...transaction,
|
|
63
|
-
instructions: nextInstructions
|
|
64
|
-
};
|
|
51
|
+
// src/blockhash.ts
|
|
52
|
+
function isTransactionWithBlockhashLifetime(transaction) {
|
|
53
|
+
const lifetimeConstraintShapeMatches = "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.blockhash === "string" && typeof transaction.lifetimeConstraint.lastValidBlockHeight === "bigint";
|
|
54
|
+
if (!lifetimeConstraintShapeMatches)
|
|
55
|
+
return false;
|
|
56
|
+
try {
|
|
57
|
+
assertIsBlockhash(transaction.lifetimeConstraint.blockhash);
|
|
58
|
+
return true;
|
|
59
|
+
} catch {
|
|
60
|
+
return false;
|
|
65
61
|
}
|
|
66
|
-
return out;
|
|
67
62
|
}
|
|
68
|
-
function
|
|
69
|
-
|
|
70
|
-
|
|
63
|
+
function assertIsTransactionWithBlockhashLifetime(transaction) {
|
|
64
|
+
if (!isTransactionWithBlockhashLifetime(transaction)) {
|
|
65
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_EXPECTED_BLOCKHASH_LIFETIME);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function setTransactionLifetimeUsingBlockhash(blockhashLifetimeConstraint, transaction) {
|
|
69
|
+
if ("lifetimeConstraint" in transaction && transaction.lifetimeConstraint.blockhash === blockhashLifetimeConstraint.blockhash && transaction.lifetimeConstraint.lastValidBlockHeight === blockhashLifetimeConstraint.lastValidBlockHeight) {
|
|
70
|
+
return transaction;
|
|
71
|
+
}
|
|
72
|
+
const out = {
|
|
73
|
+
...getUnsignedTransaction(transaction),
|
|
74
|
+
lifetimeConstraint: blockhashLifetimeConstraint
|
|
75
|
+
};
|
|
71
76
|
Object.freeze(out);
|
|
72
77
|
return out;
|
|
73
78
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
79
|
+
|
|
80
|
+
// src/create-transaction.ts
|
|
81
|
+
function createTransaction({
|
|
82
|
+
version
|
|
83
|
+
}) {
|
|
84
|
+
const out = {
|
|
85
|
+
instructions: [],
|
|
86
|
+
version
|
|
87
|
+
};
|
|
77
88
|
Object.freeze(out);
|
|
78
89
|
return out;
|
|
79
90
|
}
|
|
@@ -100,6 +111,271 @@ function isWritableRole(role) {
|
|
|
100
111
|
function mergeRoles(roleA, roleB) {
|
|
101
112
|
return roleA | roleB;
|
|
102
113
|
}
|
|
114
|
+
var RECENT_BLOCKHASHES_SYSVAR_ADDRESS = "SysvarRecentB1ockHashes11111111111111111111";
|
|
115
|
+
var SYSTEM_PROGRAM_ADDRESS = "11111111111111111111111111111111";
|
|
116
|
+
function assertIsDurableNonceTransaction(transaction) {
|
|
117
|
+
if (!isDurableNonceTransaction(transaction)) {
|
|
118
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_EXPECTED_NONCE_LIFETIME);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress) {
|
|
122
|
+
return {
|
|
123
|
+
accounts: [
|
|
124
|
+
{ address: nonceAccountAddress, role: AccountRole.WRITABLE },
|
|
125
|
+
{
|
|
126
|
+
address: RECENT_BLOCKHASHES_SYSVAR_ADDRESS,
|
|
127
|
+
role: AccountRole.READONLY
|
|
128
|
+
},
|
|
129
|
+
{ address: nonceAuthorityAddress, role: AccountRole.READONLY_SIGNER }
|
|
130
|
+
],
|
|
131
|
+
data: new Uint8Array([4, 0, 0, 0]),
|
|
132
|
+
programAddress: SYSTEM_PROGRAM_ADDRESS
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function isAdvanceNonceAccountInstruction(instruction) {
|
|
136
|
+
return instruction.programAddress === SYSTEM_PROGRAM_ADDRESS && // Test for `AdvanceNonceAccount` instruction data
|
|
137
|
+
instruction.data != null && isAdvanceNonceAccountInstructionData(instruction.data) && // Test for exactly 3 accounts
|
|
138
|
+
instruction.accounts?.length === 3 && // First account is nonce account address
|
|
139
|
+
instruction.accounts[0].address != null && instruction.accounts[0].role === AccountRole.WRITABLE && // Second account is recent blockhashes sysvar
|
|
140
|
+
instruction.accounts[1].address === RECENT_BLOCKHASHES_SYSVAR_ADDRESS && instruction.accounts[1].role === AccountRole.READONLY && // Third account is nonce authority account
|
|
141
|
+
instruction.accounts[2].address != null && isSignerRole(instruction.accounts[2].role);
|
|
142
|
+
}
|
|
143
|
+
function isAdvanceNonceAccountInstructionData(data) {
|
|
144
|
+
return data.byteLength === 4 && data[0] === 4 && data[1] === 0 && data[2] === 0 && data[3] === 0;
|
|
145
|
+
}
|
|
146
|
+
function isDurableNonceTransaction(transaction) {
|
|
147
|
+
return "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.nonce === "string" && transaction.instructions[0] != null && isAdvanceNonceAccountInstruction(transaction.instructions[0]);
|
|
148
|
+
}
|
|
149
|
+
function isAdvanceNonceAccountInstructionForNonce(instruction, nonceAccountAddress, nonceAuthorityAddress) {
|
|
150
|
+
return instruction.accounts[0].address === nonceAccountAddress && instruction.accounts[2].address === nonceAuthorityAddress;
|
|
151
|
+
}
|
|
152
|
+
function setTransactionLifetimeUsingDurableNonce({
|
|
153
|
+
nonce,
|
|
154
|
+
nonceAccountAddress,
|
|
155
|
+
nonceAuthorityAddress
|
|
156
|
+
}, transaction) {
|
|
157
|
+
let newInstructions;
|
|
158
|
+
const firstInstruction = transaction.instructions[0];
|
|
159
|
+
if (firstInstruction && isAdvanceNonceAccountInstruction(firstInstruction)) {
|
|
160
|
+
if (isAdvanceNonceAccountInstructionForNonce(firstInstruction, nonceAccountAddress, nonceAuthorityAddress)) {
|
|
161
|
+
if (isDurableNonceTransaction(transaction) && transaction.lifetimeConstraint.nonce === nonce) {
|
|
162
|
+
return transaction;
|
|
163
|
+
} else {
|
|
164
|
+
newInstructions = [firstInstruction, ...transaction.instructions.slice(1)];
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
newInstructions = [
|
|
168
|
+
createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
|
|
169
|
+
...transaction.instructions.slice(1)
|
|
170
|
+
];
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
newInstructions = [
|
|
174
|
+
createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
|
|
175
|
+
...transaction.instructions
|
|
176
|
+
];
|
|
177
|
+
}
|
|
178
|
+
const out = {
|
|
179
|
+
...getUnsignedTransaction(transaction),
|
|
180
|
+
instructions: newInstructions,
|
|
181
|
+
lifetimeConstraint: {
|
|
182
|
+
nonce
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
Object.freeze(out);
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// src/fee-payer.ts
|
|
190
|
+
function setTransactionFeePayer(feePayer, transaction) {
|
|
191
|
+
if ("feePayer" in transaction && feePayer === transaction.feePayer) {
|
|
192
|
+
return transaction;
|
|
193
|
+
}
|
|
194
|
+
const out = {
|
|
195
|
+
...getUnsignedTransaction(transaction),
|
|
196
|
+
feePayer
|
|
197
|
+
};
|
|
198
|
+
Object.freeze(out);
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// src/instructions.ts
|
|
203
|
+
function appendTransactionInstruction(instruction, transaction) {
|
|
204
|
+
return appendTransactionInstructions([instruction], transaction);
|
|
205
|
+
}
|
|
206
|
+
function appendTransactionInstructions(instructions, transaction) {
|
|
207
|
+
const out = {
|
|
208
|
+
...getUnsignedTransaction(transaction),
|
|
209
|
+
instructions: [...transaction.instructions, ...instructions]
|
|
210
|
+
};
|
|
211
|
+
Object.freeze(out);
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
function prependTransactionInstruction(instruction, transaction) {
|
|
215
|
+
return prependTransactionInstructions([instruction], transaction);
|
|
216
|
+
}
|
|
217
|
+
function prependTransactionInstructions(instructions, transaction) {
|
|
218
|
+
const out = {
|
|
219
|
+
...getUnsignedTransaction(transaction),
|
|
220
|
+
instructions: [...instructions, ...transaction.instructions]
|
|
221
|
+
};
|
|
222
|
+
Object.freeze(out);
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// src/decompile-transaction.ts
|
|
227
|
+
function getAccountMetas(message) {
|
|
228
|
+
const { header } = message;
|
|
229
|
+
const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
|
|
230
|
+
const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
|
|
231
|
+
const accountMetas = [];
|
|
232
|
+
let accountIndex = 0;
|
|
233
|
+
for (let i = 0; i < numWritableSignerAccounts; i++) {
|
|
234
|
+
accountMetas.push({
|
|
235
|
+
address: message.staticAccounts[accountIndex],
|
|
236
|
+
role: AccountRole.WRITABLE_SIGNER
|
|
237
|
+
});
|
|
238
|
+
accountIndex++;
|
|
239
|
+
}
|
|
240
|
+
for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
|
|
241
|
+
accountMetas.push({
|
|
242
|
+
address: message.staticAccounts[accountIndex],
|
|
243
|
+
role: AccountRole.READONLY_SIGNER
|
|
244
|
+
});
|
|
245
|
+
accountIndex++;
|
|
246
|
+
}
|
|
247
|
+
for (let i = 0; i < numWritableNonSignerAccounts; i++) {
|
|
248
|
+
accountMetas.push({
|
|
249
|
+
address: message.staticAccounts[accountIndex],
|
|
250
|
+
role: AccountRole.WRITABLE
|
|
251
|
+
});
|
|
252
|
+
accountIndex++;
|
|
253
|
+
}
|
|
254
|
+
for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
|
|
255
|
+
accountMetas.push({
|
|
256
|
+
address: message.staticAccounts[accountIndex],
|
|
257
|
+
role: AccountRole.READONLY
|
|
258
|
+
});
|
|
259
|
+
accountIndex++;
|
|
260
|
+
}
|
|
261
|
+
return accountMetas;
|
|
262
|
+
}
|
|
263
|
+
function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTableAddress) {
|
|
264
|
+
const compiledAddressTableLookupAddresses = compiledAddressTableLookups.map((l) => l.lookupTableAddress);
|
|
265
|
+
const missing = compiledAddressTableLookupAddresses.filter((a) => addressesByLookupTableAddress[a] === void 0);
|
|
266
|
+
if (missing.length > 0) {
|
|
267
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_FAILED_TO_DECOMPILE_ADDRESS_LOOKUP_TABLE_CONTENTS_MISSING, {
|
|
268
|
+
lookupTableAddresses: missing
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
const readOnlyMetas = [];
|
|
272
|
+
const writableMetas = [];
|
|
273
|
+
for (const lookup of compiledAddressTableLookups) {
|
|
274
|
+
const addresses = addressesByLookupTableAddress[lookup.lookupTableAddress];
|
|
275
|
+
const highestIndex = Math.max(...lookup.readableIndices, ...lookup.writableIndices);
|
|
276
|
+
if (highestIndex >= addresses.length) {
|
|
277
|
+
throw new errors.SolanaError(
|
|
278
|
+
errors.SOLANA_ERROR__TRANSACTION_FAILED_TO_DECOMPILE_ADDRESS_LOOKUP_TABLE_INDEX_OUT_OF_RANGE,
|
|
279
|
+
{
|
|
280
|
+
highestKnownIndex: addresses.length - 1,
|
|
281
|
+
highestRequestedIndex: highestIndex,
|
|
282
|
+
lookupTableAddress: lookup.lookupTableAddress
|
|
283
|
+
}
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
const readOnlyForLookup = lookup.readableIndices.map((r) => ({
|
|
287
|
+
address: addresses[r],
|
|
288
|
+
addressIndex: r,
|
|
289
|
+
lookupTableAddress: lookup.lookupTableAddress,
|
|
290
|
+
role: AccountRole.READONLY
|
|
291
|
+
}));
|
|
292
|
+
readOnlyMetas.push(...readOnlyForLookup);
|
|
293
|
+
const writableForLookup = lookup.writableIndices.map((w) => ({
|
|
294
|
+
address: addresses[w],
|
|
295
|
+
addressIndex: w,
|
|
296
|
+
lookupTableAddress: lookup.lookupTableAddress,
|
|
297
|
+
role: AccountRole.WRITABLE
|
|
298
|
+
}));
|
|
299
|
+
writableMetas.push(...writableForLookup);
|
|
300
|
+
}
|
|
301
|
+
return [...writableMetas, ...readOnlyMetas];
|
|
302
|
+
}
|
|
303
|
+
function convertInstruction(instruction, accountMetas) {
|
|
304
|
+
const programAddress = accountMetas[instruction.programAddressIndex]?.address;
|
|
305
|
+
if (!programAddress) {
|
|
306
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_FAILED_TO_DECOMPILE_INSTRUCTION_PROGRAM_ADDRESS_NOT_FOUND, {
|
|
307
|
+
index: instruction.programAddressIndex
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
|
|
311
|
+
const { data } = instruction;
|
|
312
|
+
return {
|
|
313
|
+
programAddress,
|
|
314
|
+
...accounts && accounts.length ? { accounts } : {},
|
|
315
|
+
...data && data.length ? { data } : {}
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
|
|
319
|
+
if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
|
|
320
|
+
return {
|
|
321
|
+
blockhash: messageLifetimeToken,
|
|
322
|
+
lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
|
|
323
|
+
// U64 MAX
|
|
324
|
+
};
|
|
325
|
+
} else {
|
|
326
|
+
const nonceAccountAddress = firstInstruction.accounts[0].address;
|
|
327
|
+
addresses.assertIsAddress(nonceAccountAddress);
|
|
328
|
+
const nonceAuthorityAddress = firstInstruction.accounts[2].address;
|
|
329
|
+
addresses.assertIsAddress(nonceAuthorityAddress);
|
|
330
|
+
return {
|
|
331
|
+
nonce: messageLifetimeToken,
|
|
332
|
+
nonceAccountAddress,
|
|
333
|
+
nonceAuthorityAddress
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
function convertSignatures(compiledTransaction) {
|
|
338
|
+
const {
|
|
339
|
+
compiledMessage: { staticAccounts },
|
|
340
|
+
signatures
|
|
341
|
+
} = compiledTransaction;
|
|
342
|
+
return signatures.reduce((acc, sig, index) => {
|
|
343
|
+
const allZeros = sig.every((byte) => byte === 0);
|
|
344
|
+
if (allZeros)
|
|
345
|
+
return acc;
|
|
346
|
+
const address = staticAccounts[index];
|
|
347
|
+
return { ...acc, [address]: sig };
|
|
348
|
+
}, {});
|
|
349
|
+
}
|
|
350
|
+
function decompileTransaction(compiledTransaction, config) {
|
|
351
|
+
const { compiledMessage } = compiledTransaction;
|
|
352
|
+
const feePayer = compiledMessage.staticAccounts[0];
|
|
353
|
+
if (!feePayer) {
|
|
354
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_FAILED_TO_DECOMPILE_FEE_PAYER_MISSING);
|
|
355
|
+
}
|
|
356
|
+
const accountMetas = getAccountMetas(compiledMessage);
|
|
357
|
+
const accountLookupMetas = "addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups !== void 0 && compiledMessage.addressTableLookups.length > 0 ? getAddressLookupMetas(compiledMessage.addressTableLookups, config?.addressesByLookupTableAddress ?? {}) : [];
|
|
358
|
+
const transactionMetas = [...accountMetas, ...accountLookupMetas];
|
|
359
|
+
const instructions = compiledMessage.instructions.map(
|
|
360
|
+
(compiledInstruction) => convertInstruction(compiledInstruction, transactionMetas)
|
|
361
|
+
);
|
|
362
|
+
const firstInstruction = instructions[0];
|
|
363
|
+
const lifetimeConstraint = getLifetimeConstraint(
|
|
364
|
+
compiledMessage.lifetimeToken,
|
|
365
|
+
firstInstruction,
|
|
366
|
+
config?.lastValidBlockHeight
|
|
367
|
+
);
|
|
368
|
+
const signatures = convertSignatures(compiledTransaction);
|
|
369
|
+
return functional.pipe(
|
|
370
|
+
createTransaction({ version: compiledMessage.version }),
|
|
371
|
+
(tx) => setTransactionFeePayer(feePayer, tx),
|
|
372
|
+
(tx) => instructions.reduce((acc, instruction) => {
|
|
373
|
+
return appendTransactionInstruction(instruction, acc);
|
|
374
|
+
}, tx),
|
|
375
|
+
(tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
|
|
376
|
+
(tx) => Object.keys(signatures).length > 0 ? { ...tx, signatures } : tx
|
|
377
|
+
);
|
|
378
|
+
}
|
|
103
379
|
function upsert(addressMap, address, update) {
|
|
104
380
|
addressMap[address] = update(addressMap[address] ?? { role: AccountRole.READONLY });
|
|
105
381
|
}
|
|
@@ -116,13 +392,13 @@ function getAddressMapFromInstructions(feePayer, instructions) {
|
|
|
116
392
|
if (isWritableRole(entry.role)) {
|
|
117
393
|
switch (entry[TYPE]) {
|
|
118
394
|
case 0 /* FEE_PAYER */:
|
|
119
|
-
throw new
|
|
120
|
-
|
|
121
|
-
);
|
|
395
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_INVOKED_PROGRAMS_CANNOT_PAY_FEES, {
|
|
396
|
+
programAddress: instruction.programAddress
|
|
397
|
+
});
|
|
122
398
|
default:
|
|
123
|
-
throw new
|
|
124
|
-
|
|
125
|
-
);
|
|
399
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE, {
|
|
400
|
+
programAddress: instruction.programAddress
|
|
401
|
+
});
|
|
126
402
|
}
|
|
127
403
|
}
|
|
128
404
|
if (entry[TYPE] === 2 /* STATIC */) {
|
|
@@ -152,7 +428,7 @@ function getAddressMapFromInstructions(feePayer, instructions) {
|
|
|
152
428
|
const shouldReplaceEntry = (
|
|
153
429
|
// Consider using the new LOOKUP_TABLE if its address is different...
|
|
154
430
|
entry.lookupTableAddress !== accountMeta.lookupTableAddress && // ...and sorts before the existing one.
|
|
155
|
-
(addressComparator
|
|
431
|
+
(addressComparator ||= addresses.getAddressComparator())(
|
|
156
432
|
accountMeta.lookupTableAddress,
|
|
157
433
|
entry.lookupTableAddress
|
|
158
434
|
) < 0
|
|
@@ -187,8 +463,11 @@ function getAddressMapFromInstructions(feePayer, instructions) {
|
|
|
187
463
|
addressesOfInvokedPrograms.has(account.address)
|
|
188
464
|
) {
|
|
189
465
|
if (isWritableRole(accountMeta.role)) {
|
|
190
|
-
throw new
|
|
191
|
-
|
|
466
|
+
throw new errors.SolanaError(
|
|
467
|
+
errors.SOLANA_ERROR__TRANSACTION_INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE,
|
|
468
|
+
{
|
|
469
|
+
programAddress: account.address
|
|
470
|
+
}
|
|
192
471
|
);
|
|
193
472
|
}
|
|
194
473
|
if (entry.role !== nextRole) {
|
|
@@ -258,7 +537,7 @@ function getOrderedAccountsFromAddressMap(addressMap) {
|
|
|
258
537
|
if (leftIsWritable !== isWritableRole(rightEntry.role)) {
|
|
259
538
|
return leftIsWritable ? -1 : 1;
|
|
260
539
|
}
|
|
261
|
-
addressComparator
|
|
540
|
+
addressComparator ||= addresses.getAddressComparator();
|
|
262
541
|
if (leftEntry[TYPE] === 1 /* LOOKUP_TABLE */ && rightEntry[TYPE] === 1 /* LOOKUP_TABLE */ && leftEntry.lookupTableAddress !== rightEntry.lookupTableAddress) {
|
|
263
542
|
return addressComparator(leftEntry.lookupTableAddress, rightEntry.lookupTableAddress);
|
|
264
543
|
} else {
|
|
@@ -271,23 +550,22 @@ function getOrderedAccountsFromAddressMap(addressMap) {
|
|
|
271
550
|
return orderedAccounts;
|
|
272
551
|
}
|
|
273
552
|
function getCompiledAddressTableLookups(orderedAccounts) {
|
|
274
|
-
var _a;
|
|
275
553
|
const index = {};
|
|
276
554
|
for (const account of orderedAccounts) {
|
|
277
555
|
if (!("lookupTableAddress" in account)) {
|
|
278
556
|
continue;
|
|
279
557
|
}
|
|
280
|
-
const entry = index[
|
|
558
|
+
const entry = index[account.lookupTableAddress] ||= {
|
|
281
559
|
readableIndices: [],
|
|
282
560
|
writableIndices: []
|
|
283
|
-
}
|
|
561
|
+
};
|
|
284
562
|
if (account.role === AccountRole.WRITABLE) {
|
|
285
563
|
entry.writableIndices.push(account.addressIndex);
|
|
286
564
|
} else {
|
|
287
565
|
entry.readableIndices.push(account.addressIndex);
|
|
288
566
|
}
|
|
289
567
|
}
|
|
290
|
-
return Object.keys(index).sort(
|
|
568
|
+
return Object.keys(index).sort(addresses.getAddressComparator()).map((lookupTableAddress) => ({
|
|
291
569
|
lookupTableAddress,
|
|
292
570
|
...index[lookupTableAddress]
|
|
293
571
|
}));
|
|
@@ -366,260 +644,293 @@ function compileMessage(transaction) {
|
|
|
366
644
|
version: transaction.version
|
|
367
645
|
};
|
|
368
646
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
keys.getBase58EncodedAddressCodec(
|
|
375
|
-
__DEV__ ? {
|
|
376
|
-
description: "The address of the address lookup table account from which instruction addresses should be looked up"
|
|
377
|
-
} : void 0
|
|
378
|
-
)
|
|
379
|
-
],
|
|
647
|
+
var memoizedAddressTableLookupEncoder;
|
|
648
|
+
function getAddressTableLookupEncoder() {
|
|
649
|
+
if (!memoizedAddressTableLookupEncoder) {
|
|
650
|
+
memoizedAddressTableLookupEncoder = codecsDataStructures.getStructEncoder([
|
|
651
|
+
["lookupTableAddress", addresses.getAddressEncoder()],
|
|
380
652
|
[
|
|
381
653
|
"writableIndices",
|
|
382
|
-
|
|
383
|
-
...__DEV__ ? {
|
|
384
|
-
description: "The indices of the accounts in the lookup table that should be loaded as writeable"
|
|
385
|
-
} : null,
|
|
386
|
-
size: umiSerializers.shortU16()
|
|
387
|
-
})
|
|
654
|
+
codecsDataStructures.getArrayEncoder(codecsNumbers.getU8Encoder(), { size: codecsNumbers.getShortU16Encoder() })
|
|
388
655
|
],
|
|
389
656
|
[
|
|
390
657
|
"readableIndices",
|
|
391
|
-
|
|
392
|
-
...__DEV__ ? {
|
|
393
|
-
description: "The indices of the accounts in the lookup table that should be loaded as read-only"
|
|
394
|
-
} : void 0,
|
|
395
|
-
size: umiSerializers.shortU16()
|
|
396
|
-
})
|
|
658
|
+
codecsDataStructures.getArrayEncoder(codecsNumbers.getU8Encoder(), { size: codecsNumbers.getShortU16Encoder() })
|
|
397
659
|
]
|
|
398
|
-
]
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
660
|
+
]);
|
|
661
|
+
}
|
|
662
|
+
return memoizedAddressTableLookupEncoder;
|
|
663
|
+
}
|
|
664
|
+
var memoizedAddressTableLookupDecoder;
|
|
665
|
+
function getAddressTableLookupDecoder() {
|
|
666
|
+
if (!memoizedAddressTableLookupDecoder) {
|
|
667
|
+
memoizedAddressTableLookupDecoder = codecsDataStructures.getStructDecoder([
|
|
668
|
+
["lookupTableAddress", addresses.getAddressDecoder()],
|
|
669
|
+
["writableIndices", codecsDataStructures.getArrayDecoder(codecsNumbers.getU8Decoder(), { size: codecsNumbers.getShortU16Decoder() })],
|
|
670
|
+
["readableIndices", codecsDataStructures.getArrayDecoder(codecsNumbers.getU8Decoder(), { size: codecsNumbers.getShortU16Decoder() })]
|
|
671
|
+
]);
|
|
672
|
+
}
|
|
673
|
+
return memoizedAddressTableLookupDecoder;
|
|
674
|
+
}
|
|
675
|
+
var memoizedU8Encoder;
|
|
676
|
+
function getMemoizedU8Encoder() {
|
|
677
|
+
if (!memoizedU8Encoder)
|
|
678
|
+
memoizedU8Encoder = codecsNumbers.getU8Encoder();
|
|
679
|
+
return memoizedU8Encoder;
|
|
680
|
+
}
|
|
681
|
+
var memoizedU8Decoder;
|
|
682
|
+
function getMemoizedU8Decoder() {
|
|
683
|
+
if (!memoizedU8Decoder)
|
|
684
|
+
memoizedU8Decoder = codecsNumbers.getU8Decoder();
|
|
685
|
+
return memoizedU8Decoder;
|
|
686
|
+
}
|
|
687
|
+
function getMessageHeaderEncoder() {
|
|
688
|
+
return codecsDataStructures.getStructEncoder([
|
|
689
|
+
["numSignerAccounts", getMemoizedU8Encoder()],
|
|
690
|
+
["numReadonlySignerAccounts", getMemoizedU8Encoder()],
|
|
691
|
+
["numReadonlyNonSignerAccounts", getMemoizedU8Encoder()]
|
|
692
|
+
]);
|
|
403
693
|
}
|
|
404
|
-
function
|
|
405
|
-
return
|
|
406
|
-
[
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
__DEV__ ? {
|
|
411
|
-
description: "The expected number of addresses in the static address list belonging to accounts that are required to sign this transaction"
|
|
412
|
-
} : void 0
|
|
413
|
-
)
|
|
414
|
-
],
|
|
415
|
-
[
|
|
416
|
-
"numReadonlySignerAccounts",
|
|
417
|
-
umiSerializers.u8(
|
|
418
|
-
__DEV__ ? {
|
|
419
|
-
description: "The expected number of addresses in the static address list belonging to accounts that are required to sign this transaction, but may not be writable"
|
|
420
|
-
} : void 0
|
|
421
|
-
)
|
|
422
|
-
],
|
|
423
|
-
[
|
|
424
|
-
"numReadonlyNonSignerAccounts",
|
|
425
|
-
umiSerializers.u8(
|
|
426
|
-
__DEV__ ? {
|
|
427
|
-
description: "The expected number of addresses in the static address list belonging to accounts that are neither signers, nor writable"
|
|
428
|
-
} : void 0
|
|
429
|
-
)
|
|
430
|
-
]
|
|
431
|
-
],
|
|
432
|
-
__DEV__ ? {
|
|
433
|
-
description: "The transaction message header containing counts of the signer, readonly-signer, and readonly-nonsigner account addresses"
|
|
434
|
-
} : void 0
|
|
435
|
-
);
|
|
694
|
+
function getMessageHeaderDecoder() {
|
|
695
|
+
return codecsDataStructures.getStructDecoder([
|
|
696
|
+
["numSignerAccounts", getMemoizedU8Decoder()],
|
|
697
|
+
["numReadonlySignerAccounts", getMemoizedU8Decoder()],
|
|
698
|
+
["numReadonlyNonSignerAccounts", getMemoizedU8Decoder()]
|
|
699
|
+
]);
|
|
436
700
|
}
|
|
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
|
-
|
|
701
|
+
var memoizedGetInstructionEncoder;
|
|
702
|
+
function getInstructionEncoder() {
|
|
703
|
+
if (!memoizedGetInstructionEncoder) {
|
|
704
|
+
memoizedGetInstructionEncoder = codecsCore.mapEncoder(
|
|
705
|
+
codecsDataStructures.getStructEncoder([
|
|
706
|
+
["programAddressIndex", codecsNumbers.getU8Encoder()],
|
|
707
|
+
["accountIndices", codecsDataStructures.getArrayEncoder(codecsNumbers.getU8Encoder(), { size: codecsNumbers.getShortU16Encoder() })],
|
|
708
|
+
["data", codecsDataStructures.getBytesEncoder({ size: codecsNumbers.getShortU16Encoder() })]
|
|
709
|
+
]),
|
|
710
|
+
// Convert an instruction to have all fields defined
|
|
711
|
+
(instruction) => {
|
|
712
|
+
if (instruction.accountIndices !== void 0 && instruction.data !== void 0) {
|
|
713
|
+
return instruction;
|
|
714
|
+
}
|
|
715
|
+
return {
|
|
716
|
+
...instruction,
|
|
717
|
+
accountIndices: instruction.accountIndices ?? [],
|
|
718
|
+
data: instruction.data ?? new Uint8Array(0)
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
);
|
|
722
|
+
}
|
|
723
|
+
return memoizedGetInstructionEncoder;
|
|
724
|
+
}
|
|
725
|
+
var memoizedGetInstructionDecoder;
|
|
726
|
+
function getInstructionDecoder() {
|
|
727
|
+
if (!memoizedGetInstructionDecoder) {
|
|
728
|
+
memoizedGetInstructionDecoder = codecsCore.mapDecoder(
|
|
729
|
+
codecsDataStructures.getStructDecoder([
|
|
730
|
+
["programAddressIndex", codecsNumbers.getU8Decoder()],
|
|
731
|
+
["accountIndices", codecsDataStructures.getArrayDecoder(codecsNumbers.getU8Decoder(), { size: codecsNumbers.getShortU16Decoder() })],
|
|
732
|
+
["data", codecsDataStructures.getBytesDecoder({ size: codecsNumbers.getShortU16Decoder() })]
|
|
733
|
+
]),
|
|
734
|
+
// Convert an instruction to exclude optional fields if they are empty
|
|
735
|
+
(instruction) => {
|
|
736
|
+
if (instruction.accountIndices.length && instruction.data.byteLength) {
|
|
737
|
+
return instruction;
|
|
738
|
+
}
|
|
739
|
+
const { accountIndices, data, ...rest } = instruction;
|
|
740
|
+
return {
|
|
741
|
+
...rest,
|
|
742
|
+
...accountIndices.length ? { accountIndices } : null,
|
|
743
|
+
...data.byteLength ? { data } : null
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
return memoizedGetInstructionDecoder;
|
|
749
|
+
}
|
|
750
|
+
var VERSION_FLAG_MASK = 128;
|
|
751
|
+
function getTransactionVersionEncoder() {
|
|
752
|
+
return codecsCore.createEncoder({
|
|
753
|
+
getSizeFromValue: (value) => value === "legacy" ? 0 : 1,
|
|
754
|
+
maxSize: 1,
|
|
755
|
+
write: (value, bytes, offset) => {
|
|
756
|
+
if (value === "legacy") {
|
|
757
|
+
return offset;
|
|
758
|
+
}
|
|
759
|
+
if (value < 0 || value > 127) {
|
|
760
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_VERSION_NUMBER_OUT_OF_RANGE, {
|
|
761
|
+
actualVersion: value
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
bytes.set([value | VERSION_FLAG_MASK], offset);
|
|
765
|
+
return offset + 1;
|
|
766
|
+
}
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
function getTransactionVersionDecoder() {
|
|
770
|
+
return codecsCore.createDecoder({
|
|
771
|
+
maxSize: 1,
|
|
772
|
+
read: (bytes, offset) => {
|
|
773
|
+
const firstByte = bytes[offset];
|
|
774
|
+
if ((firstByte & VERSION_FLAG_MASK) === 0) {
|
|
775
|
+
return ["legacy", offset];
|
|
776
|
+
} else {
|
|
777
|
+
const version = firstByte ^ VERSION_FLAG_MASK;
|
|
778
|
+
return [version, offset + 1];
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
// src/serializers/message.ts
|
|
785
|
+
function getCompiledMessageLegacyEncoder() {
|
|
786
|
+
return codecsDataStructures.getStructEncoder(getPreludeStructEncoderTuple());
|
|
787
|
+
}
|
|
788
|
+
function getCompiledMessageVersionedEncoder() {
|
|
789
|
+
return codecsCore.mapEncoder(
|
|
790
|
+
codecsDataStructures.getStructEncoder([
|
|
791
|
+
...getPreludeStructEncoderTuple(),
|
|
792
|
+
["addressTableLookups", getAddressTableLookupArrayEncoder()]
|
|
467
793
|
]),
|
|
468
794
|
(value) => {
|
|
469
|
-
if (value.
|
|
795
|
+
if (value.version === "legacy") {
|
|
470
796
|
return value;
|
|
471
797
|
}
|
|
472
798
|
return {
|
|
473
799
|
...value,
|
|
474
|
-
|
|
475
|
-
data: value.data ?? new Uint8Array(0)
|
|
800
|
+
addressTableLookups: value.addressTableLookups ?? []
|
|
476
801
|
};
|
|
802
|
+
}
|
|
803
|
+
);
|
|
804
|
+
}
|
|
805
|
+
function getPreludeStructEncoderTuple() {
|
|
806
|
+
return [
|
|
807
|
+
["version", getTransactionVersionEncoder()],
|
|
808
|
+
["header", getMessageHeaderEncoder()],
|
|
809
|
+
["staticAccounts", codecsDataStructures.getArrayEncoder(addresses.getAddressEncoder(), { size: codecsNumbers.getShortU16Encoder() })],
|
|
810
|
+
["lifetimeToken", codecsStrings.getStringEncoder({ encoding: codecsStrings.getBase58Encoder(), size: 32 })],
|
|
811
|
+
["instructions", codecsDataStructures.getArrayEncoder(getInstructionEncoder(), { size: codecsNumbers.getShortU16Encoder() })]
|
|
812
|
+
];
|
|
813
|
+
}
|
|
814
|
+
function getPreludeStructDecoderTuple() {
|
|
815
|
+
return [
|
|
816
|
+
["version", getTransactionVersionDecoder()],
|
|
817
|
+
["header", getMessageHeaderDecoder()],
|
|
818
|
+
["staticAccounts", codecsDataStructures.getArrayDecoder(addresses.getAddressDecoder(), { size: codecsNumbers.getShortU16Decoder() })],
|
|
819
|
+
["lifetimeToken", codecsStrings.getStringDecoder({ encoding: codecsStrings.getBase58Decoder(), size: 32 })],
|
|
820
|
+
["instructions", codecsDataStructures.getArrayDecoder(getInstructionDecoder(), { size: codecsNumbers.getShortU16Decoder() })],
|
|
821
|
+
["addressTableLookups", getAddressTableLookupArrayDecoder()]
|
|
822
|
+
];
|
|
823
|
+
}
|
|
824
|
+
function getAddressTableLookupArrayEncoder() {
|
|
825
|
+
return codecsDataStructures.getArrayEncoder(getAddressTableLookupEncoder(), { size: codecsNumbers.getShortU16Encoder() });
|
|
826
|
+
}
|
|
827
|
+
function getAddressTableLookupArrayDecoder() {
|
|
828
|
+
return codecsDataStructures.getArrayDecoder(getAddressTableLookupDecoder(), { size: codecsNumbers.getShortU16Decoder() });
|
|
829
|
+
}
|
|
830
|
+
function getCompiledMessageEncoder() {
|
|
831
|
+
return codecsCore.createEncoder({
|
|
832
|
+
getSizeFromValue: (compiledMessage) => {
|
|
833
|
+
if (compiledMessage.version === "legacy") {
|
|
834
|
+
return getCompiledMessageLegacyEncoder().getSizeFromValue(compiledMessage);
|
|
835
|
+
} else {
|
|
836
|
+
return getCompiledMessageVersionedEncoder().getSizeFromValue(compiledMessage);
|
|
837
|
+
}
|
|
477
838
|
},
|
|
478
|
-
(
|
|
479
|
-
if (
|
|
480
|
-
return
|
|
839
|
+
write: (compiledMessage, bytes, offset) => {
|
|
840
|
+
if (compiledMessage.version === "legacy") {
|
|
841
|
+
return getCompiledMessageLegacyEncoder().write(compiledMessage, bytes, offset);
|
|
842
|
+
} else {
|
|
843
|
+
return getCompiledMessageVersionedEncoder().write(compiledMessage, bytes, offset);
|
|
481
844
|
}
|
|
482
|
-
const { addressIndices, data, ...rest } = value;
|
|
483
|
-
return {
|
|
484
|
-
...rest,
|
|
485
|
-
...addressIndices.length ? { addressIndices } : null,
|
|
486
|
-
...data.byteLength ? { data } : null
|
|
487
|
-
};
|
|
488
845
|
}
|
|
489
|
-
);
|
|
846
|
+
});
|
|
490
847
|
}
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
848
|
+
function getCompiledMessageDecoder() {
|
|
849
|
+
return codecsCore.mapDecoder(
|
|
850
|
+
codecsDataStructures.getStructDecoder(getPreludeStructDecoderTuple()),
|
|
851
|
+
({ addressTableLookups, ...restOfMessage }) => {
|
|
852
|
+
if (restOfMessage.version === "legacy" || !addressTableLookups?.length) {
|
|
853
|
+
return restOfMessage;
|
|
854
|
+
}
|
|
855
|
+
return { ...restOfMessage, addressTableLookups };
|
|
856
|
+
}
|
|
497
857
|
);
|
|
498
858
|
}
|
|
499
|
-
function
|
|
500
|
-
return ()
|
|
501
|
-
throw getError("decoder", name);
|
|
502
|
-
};
|
|
859
|
+
function getCompiledMessageCodec() {
|
|
860
|
+
return codecsCore.combineCodec(getCompiledMessageEncoder(), getCompiledMessageDecoder());
|
|
503
861
|
}
|
|
504
862
|
|
|
505
|
-
// src/
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
if ((firstByte & VERSION_FLAG_MASK) === 0) {
|
|
515
|
-
return ["legacy", offset];
|
|
863
|
+
// src/compile-transaction.ts
|
|
864
|
+
function getCompiledTransaction(transaction) {
|
|
865
|
+
const compiledMessage = compileMessage(transaction);
|
|
866
|
+
let signatures;
|
|
867
|
+
if ("signatures" in transaction) {
|
|
868
|
+
signatures = [];
|
|
869
|
+
for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
|
|
870
|
+
signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
|
|
871
|
+
}
|
|
516
872
|
} else {
|
|
517
|
-
|
|
518
|
-
return [version, offset + 1];
|
|
873
|
+
signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
|
|
519
874
|
}
|
|
520
|
-
}
|
|
521
|
-
function serialize(value) {
|
|
522
|
-
if (value === "legacy") {
|
|
523
|
-
return new Uint8Array();
|
|
524
|
-
}
|
|
525
|
-
if (value < 0 || value > 127) {
|
|
526
|
-
throw new Error(`Transaction version must be in the range [0, 127]. \`${value}\` given.`);
|
|
527
|
-
}
|
|
528
|
-
return new Uint8Array([value | VERSION_FLAG_MASK]);
|
|
529
|
-
}
|
|
530
|
-
function getTransactionVersionCodec() {
|
|
531
875
|
return {
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
serialize
|
|
876
|
+
compiledMessage,
|
|
877
|
+
signatures
|
|
535
878
|
};
|
|
536
879
|
}
|
|
537
880
|
|
|
538
|
-
// src/serializers/
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
function serialize2(compiledMessage) {
|
|
545
|
-
if (compiledMessage.version === "legacy") {
|
|
546
|
-
return umiSerializers.struct(getPreludeStructSerializerTuple()).serialize(compiledMessage);
|
|
547
|
-
} else {
|
|
548
|
-
return umiSerializers.mapSerializer(
|
|
549
|
-
umiSerializers.struct([
|
|
550
|
-
...getPreludeStructSerializerTuple(),
|
|
551
|
-
["addressTableLookups", getAddressTableLookupsSerializer()]
|
|
552
|
-
]),
|
|
553
|
-
(value) => {
|
|
554
|
-
if (value.version === "legacy") {
|
|
555
|
-
return value;
|
|
556
|
-
}
|
|
557
|
-
return {
|
|
558
|
-
...value,
|
|
559
|
-
addressTableLookups: value.addressTableLookups ?? []
|
|
560
|
-
};
|
|
561
|
-
}
|
|
562
|
-
).serialize(compiledMessage);
|
|
563
|
-
}
|
|
881
|
+
// src/serializers/transaction.ts
|
|
882
|
+
function getCompiledTransactionEncoder() {
|
|
883
|
+
return codecsDataStructures.getStructEncoder([
|
|
884
|
+
["signatures", codecsDataStructures.getArrayEncoder(codecsDataStructures.getBytesEncoder({ size: 64 }), { size: codecsNumbers.getShortU16Encoder() })],
|
|
885
|
+
["compiledMessage", getCompiledMessageEncoder()]
|
|
886
|
+
]);
|
|
564
887
|
}
|
|
565
|
-
function
|
|
566
|
-
return [
|
|
567
|
-
["version", getTransactionVersionCodec()],
|
|
568
|
-
["header", getMessageHeaderCodec()],
|
|
569
|
-
[
|
|
570
|
-
"staticAccounts",
|
|
571
|
-
umiSerializers.array(keys.getBase58EncodedAddressCodec(), {
|
|
572
|
-
description: __DEV__ ? "A compact-array of static account addresses belonging to this transaction" : "",
|
|
573
|
-
size: umiSerializers.shortU16()
|
|
574
|
-
})
|
|
575
|
-
],
|
|
888
|
+
function getCompiledTransactionDecoder() {
|
|
889
|
+
return codecsDataStructures.getStructDecoder([
|
|
576
890
|
[
|
|
577
|
-
"
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
encoding: umiSerializers.base58,
|
|
581
|
-
size: 32
|
|
891
|
+
"signatures",
|
|
892
|
+
codecsDataStructures.getArrayDecoder(codecsDataStructures.getBytesDecoder({ size: 64 }), {
|
|
893
|
+
size: codecsNumbers.getShortU16Decoder()
|
|
582
894
|
})
|
|
583
895
|
],
|
|
584
|
-
[
|
|
585
|
-
|
|
586
|
-
umiSerializers.array(getInstructionCodec(), {
|
|
587
|
-
description: __DEV__ ? "A compact-array of instructions belonging to this transaction" : "",
|
|
588
|
-
size: umiSerializers.shortU16()
|
|
589
|
-
})
|
|
590
|
-
]
|
|
591
|
-
];
|
|
896
|
+
["compiledMessage", getCompiledMessageDecoder()]
|
|
897
|
+
]);
|
|
592
898
|
}
|
|
593
|
-
function
|
|
594
|
-
return
|
|
595
|
-
...__DEV__ ? { description: "A compact array of address table lookups belonging to this transaction" } : null,
|
|
596
|
-
size: umiSerializers.shortU16()
|
|
597
|
-
});
|
|
899
|
+
function getTransactionEncoder() {
|
|
900
|
+
return codecsCore.mapEncoder(getCompiledTransactionEncoder(), getCompiledTransaction);
|
|
598
901
|
}
|
|
599
|
-
function
|
|
600
|
-
return
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
};
|
|
902
|
+
function getTransactionDecoder(config) {
|
|
903
|
+
return codecsCore.mapDecoder(
|
|
904
|
+
getCompiledTransactionDecoder(),
|
|
905
|
+
(compiledTransaction) => decompileTransaction(compiledTransaction, config)
|
|
906
|
+
);
|
|
605
907
|
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
async function getCompiledMessageSignature(message, secretKey) {
|
|
609
|
-
const wireMessageBytes = getCompiledMessageEncoder().serialize(message);
|
|
610
|
-
const signature = await keys.signBytes(secretKey, wireMessageBytes);
|
|
611
|
-
return signature;
|
|
908
|
+
function getTransactionCodec(config) {
|
|
909
|
+
return codecsCore.combineCodec(getTransactionEncoder(), getTransactionDecoder(config));
|
|
612
910
|
}
|
|
613
|
-
|
|
911
|
+
var base58Decoder;
|
|
912
|
+
function getSignatureFromTransaction(transaction) {
|
|
913
|
+
if (!base58Decoder)
|
|
914
|
+
base58Decoder = codecsStrings.getBase58Decoder();
|
|
915
|
+
const signatureBytes = transaction.signatures[transaction.feePayer];
|
|
916
|
+
if (!signatureBytes) {
|
|
917
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_SIGNATURE_NOT_COMPUTABLE);
|
|
918
|
+
}
|
|
919
|
+
const transactionSignature = base58Decoder.decode(signatureBytes);
|
|
920
|
+
return transactionSignature;
|
|
921
|
+
}
|
|
922
|
+
async function partiallySignTransaction(keyPairs, transaction) {
|
|
614
923
|
const compiledMessage = compileMessage(transaction);
|
|
615
|
-
const
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
924
|
+
const nextSignatures = "signatures" in transaction ? { ...transaction.signatures } : {};
|
|
925
|
+
const wireMessageBytes = getCompiledMessageEncoder().encode(compiledMessage);
|
|
926
|
+
const publicKeySignaturePairs = await Promise.all(
|
|
927
|
+
keyPairs.map(
|
|
928
|
+
(keyPair) => Promise.all([addresses.getAddressFromPublicKey(keyPair.publicKey), keys.signBytes(keyPair.privateKey, wireMessageBytes)])
|
|
929
|
+
)
|
|
930
|
+
);
|
|
931
|
+
for (const [signerPublicKey, signature] of publicKeySignaturePairs) {
|
|
932
|
+
nextSignatures[signerPublicKey] = signature;
|
|
933
|
+
}
|
|
623
934
|
const out = {
|
|
624
935
|
...transaction,
|
|
625
936
|
signatures: nextSignatures
|
|
@@ -627,9 +938,57 @@ async function signTransaction(keyPair, transaction) {
|
|
|
627
938
|
Object.freeze(out);
|
|
628
939
|
return out;
|
|
629
940
|
}
|
|
941
|
+
async function signTransaction(keyPairs, transaction) {
|
|
942
|
+
const out = await partiallySignTransaction(keyPairs, transaction);
|
|
943
|
+
assertTransactionIsFullySigned(out);
|
|
944
|
+
Object.freeze(out);
|
|
945
|
+
return out;
|
|
946
|
+
}
|
|
947
|
+
function assertTransactionIsFullySigned(transaction) {
|
|
948
|
+
const signerAddressesFromInstructions = transaction.instructions.flatMap((i) => i.accounts?.filter((a) => isSignerRole(a.role)) ?? []).map((a) => a.address);
|
|
949
|
+
const requiredSigners = /* @__PURE__ */ new Set([transaction.feePayer, ...signerAddressesFromInstructions]);
|
|
950
|
+
const missingSigs = [];
|
|
951
|
+
requiredSigners.forEach((address) => {
|
|
952
|
+
if (!transaction.signatures[address]) {
|
|
953
|
+
missingSigs.push(address);
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
if (missingSigs.length > 0) {
|
|
957
|
+
throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_MISSING_SIGNATURES, {
|
|
958
|
+
addresses: missingSigs
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
function getBase64EncodedWireTransaction(transaction) {
|
|
963
|
+
const wireTransactionBytes = getTransactionEncoder().encode(transaction);
|
|
964
|
+
return codecsStrings.getBase64Decoder().decode(wireTransactionBytes);
|
|
965
|
+
}
|
|
630
966
|
|
|
631
967
|
exports.appendTransactionInstruction = appendTransactionInstruction;
|
|
968
|
+
exports.appendTransactionInstructions = appendTransactionInstructions;
|
|
969
|
+
exports.assertIsDurableNonceTransaction = assertIsDurableNonceTransaction;
|
|
970
|
+
exports.assertIsTransactionWithBlockhashLifetime = assertIsTransactionWithBlockhashLifetime;
|
|
971
|
+
exports.assertTransactionIsFullySigned = assertTransactionIsFullySigned;
|
|
972
|
+
exports.compileMessage = compileMessage;
|
|
632
973
|
exports.createTransaction = createTransaction;
|
|
974
|
+
exports.decompileTransaction = decompileTransaction;
|
|
975
|
+
exports.getBase64EncodedWireTransaction = getBase64EncodedWireTransaction;
|
|
976
|
+
exports.getCompiledMessageCodec = getCompiledMessageCodec;
|
|
977
|
+
exports.getCompiledMessageDecoder = getCompiledMessageDecoder;
|
|
978
|
+
exports.getCompiledMessageEncoder = getCompiledMessageEncoder;
|
|
979
|
+
exports.getCompiledTransactionDecoder = getCompiledTransactionDecoder;
|
|
980
|
+
exports.getSignatureFromTransaction = getSignatureFromTransaction;
|
|
981
|
+
exports.getTransactionCodec = getTransactionCodec;
|
|
982
|
+
exports.getTransactionDecoder = getTransactionDecoder;
|
|
983
|
+
exports.getTransactionEncoder = getTransactionEncoder;
|
|
984
|
+
exports.getUnsignedTransaction = getUnsignedTransaction;
|
|
985
|
+
exports.isAdvanceNonceAccountInstruction = isAdvanceNonceAccountInstruction;
|
|
986
|
+
exports.partiallySignTransaction = partiallySignTransaction;
|
|
633
987
|
exports.prependTransactionInstruction = prependTransactionInstruction;
|
|
988
|
+
exports.prependTransactionInstructions = prependTransactionInstructions;
|
|
634
989
|
exports.setTransactionFeePayer = setTransactionFeePayer;
|
|
990
|
+
exports.setTransactionLifetimeUsingBlockhash = setTransactionLifetimeUsingBlockhash;
|
|
991
|
+
exports.setTransactionLifetimeUsingDurableNonce = setTransactionLifetimeUsingDurableNonce;
|
|
635
992
|
exports.signTransaction = signTransaction;
|
|
993
|
+
//# sourceMappingURL=out.js.map
|
|
994
|
+
//# sourceMappingURL=index.node.cjs.map
|