@solana/transaction-messages 2.0.0-20241006045741
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 +20 -0
- package/README.md +226 -0
- package/dist/index.browser.cjs +928 -0
- package/dist/index.browser.cjs.map +1 -0
- package/dist/index.browser.mjs +905 -0
- package/dist/index.browser.mjs.map +1 -0
- package/dist/index.native.mjs +905 -0
- package/dist/index.native.mjs.map +1 -0
- package/dist/index.node.cjs +928 -0
- package/dist/index.node.cjs.map +1 -0
- package/dist/index.node.mjs +905 -0
- package/dist/index.node.mjs.map +1 -0
- package/dist/types/addresses-by-lookup-table-address.d.ts +5 -0
- package/dist/types/addresses-by-lookup-table-address.d.ts.map +1 -0
- package/dist/types/blockhash.d.ts +16 -0
- package/dist/types/blockhash.d.ts.map +1 -0
- package/dist/types/codecs/address-table-lookup.d.ts +8 -0
- package/dist/types/codecs/address-table-lookup.d.ts.map +1 -0
- package/dist/types/codecs/header.d.ts +8 -0
- package/dist/types/codecs/header.d.ts.map +1 -0
- package/dist/types/codecs/index.d.ts +3 -0
- package/dist/types/codecs/index.d.ts.map +1 -0
- package/dist/types/codecs/instruction.d.ts +8 -0
- package/dist/types/codecs/instruction.d.ts.map +1 -0
- package/dist/types/codecs/message.d.ts +6 -0
- package/dist/types/codecs/message.d.ts.map +1 -0
- package/dist/types/codecs/transaction-version.d.ts +6 -0
- package/dist/types/codecs/transaction-version.d.ts.map +1 -0
- package/dist/types/compilable-transaction-message.d.ts +7 -0
- package/dist/types/compilable-transaction-message.d.ts.map +1 -0
- package/dist/types/compile/accounts.d.ts +28 -0
- package/dist/types/compile/accounts.d.ts.map +1 -0
- package/dist/types/compile/address-table-lookups.d.ts +10 -0
- package/dist/types/compile/address-table-lookups.d.ts.map +1 -0
- package/dist/types/compile/header.d.ts +9 -0
- package/dist/types/compile/header.d.ts.map +1 -0
- package/dist/types/compile/index.d.ts +2 -0
- package/dist/types/compile/index.d.ts.map +1 -0
- package/dist/types/compile/instructions.d.ts +10 -0
- package/dist/types/compile/instructions.d.ts.map +1 -0
- package/dist/types/compile/lifetime-token.d.ts +3 -0
- package/dist/types/compile/lifetime-token.d.ts.map +1 -0
- package/dist/types/compile/message.d.ts +26 -0
- package/dist/types/compile/message.d.ts.map +1 -0
- package/dist/types/compile/static-accounts.d.ts +4 -0
- package/dist/types/compile/static-accounts.d.ts.map +1 -0
- package/dist/types/compress-transaction-message.d.ts +14 -0
- package/dist/types/compress-transaction-message.d.ts.map +1 -0
- package/dist/types/create-transaction-message.d.ts +9 -0
- package/dist/types/create-transaction-message.d.ts.map +1 -0
- package/dist/types/decompile-message.d.ts +9 -0
- package/dist/types/decompile-message.d.ts.map +1 -0
- package/dist/types/durable-nonce.d.ts +35 -0
- package/dist/types/durable-nonce.d.ts.map +1 -0
- package/dist/types/fee-payer.d.ts +7 -0
- package/dist/types/fee-payer.d.ts.map +1 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/instructions.d.ts +6 -0
- package/dist/types/instructions.d.ts.map +1 -0
- package/dist/types/transaction-message.d.ts +12 -0
- package/dist/types/transaction-message.d.ts.map +1 -0
- package/package.json +93 -0
|
@@ -0,0 +1,905 @@
|
|
|
1
|
+
import { SolanaError, SOLANA_ERROR__TRANSACTION__EXPECTED_BLOCKHASH_LIFETIME, SOLANA_ERROR__TRANSACTION__VERSION_NUMBER_OUT_OF_RANGE, SOLANA_ERROR__TRANSACTION__EXPECTED_NONCE_LIFETIME, SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_FEE_PAYER_MISSING, SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_ADDRESS_LOOKUP_TABLE_CONTENTS_MISSING, SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_ADDRESS_LOOKUP_TABLE_INDEX_OUT_OF_RANGE, SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_INSTRUCTION_PROGRAM_ADDRESS_NOT_FOUND, SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE, SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_CANNOT_PAY_FEES, SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE } from '@solana/errors';
|
|
2
|
+
import { assertIsBlockhash } from '@solana/rpc-types';
|
|
3
|
+
import { getAddressEncoder, getAddressDecoder, getAddressComparator, assertIsAddress } from '@solana/addresses';
|
|
4
|
+
import { createEncoder, createDecoder, combineCodec, transformDecoder, transformEncoder, fixEncoderSize, fixDecoderSize, addEncoderSizePrefix, addDecoderSizePrefix } from '@solana/codecs-core';
|
|
5
|
+
import { getStructDecoder, getStructEncoder, getArrayEncoder, getArrayDecoder, getBytesEncoder, getBytesDecoder } from '@solana/codecs-data-structures';
|
|
6
|
+
import { getShortU16Encoder, getShortU16Decoder, getU8Encoder, getU8Decoder } from '@solana/codecs-numbers';
|
|
7
|
+
import { isSignerRole, AccountRole, isWritableRole, mergeRoles } from '@solana/instructions';
|
|
8
|
+
import { pipe } from '@solana/functional';
|
|
9
|
+
|
|
10
|
+
// src/blockhash.ts
|
|
11
|
+
function isTransactionMessageWithBlockhashLifetime(transaction) {
|
|
12
|
+
const lifetimeConstraintShapeMatches = "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.blockhash === "string" && typeof transaction.lifetimeConstraint.lastValidBlockHeight === "bigint";
|
|
13
|
+
if (!lifetimeConstraintShapeMatches) return false;
|
|
14
|
+
try {
|
|
15
|
+
assertIsBlockhash(transaction.lifetimeConstraint.blockhash);
|
|
16
|
+
return true;
|
|
17
|
+
} catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function assertIsTransactionMessageWithBlockhashLifetime(transaction) {
|
|
22
|
+
if (!isTransactionMessageWithBlockhashLifetime(transaction)) {
|
|
23
|
+
throw new SolanaError(SOLANA_ERROR__TRANSACTION__EXPECTED_BLOCKHASH_LIFETIME);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function setTransactionMessageLifetimeUsingBlockhash(blockhashLifetimeConstraint, transaction) {
|
|
27
|
+
if ("lifetimeConstraint" in transaction && transaction.lifetimeConstraint.blockhash === blockhashLifetimeConstraint.blockhash && transaction.lifetimeConstraint.lastValidBlockHeight === blockhashLifetimeConstraint.lastValidBlockHeight) {
|
|
28
|
+
return transaction;
|
|
29
|
+
}
|
|
30
|
+
const out = {
|
|
31
|
+
...transaction,
|
|
32
|
+
lifetimeConstraint: Object.freeze(blockhashLifetimeConstraint)
|
|
33
|
+
};
|
|
34
|
+
Object.freeze(out);
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
|
|
38
|
+
if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
|
|
39
|
+
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_STRING_FOR_BASE, {
|
|
40
|
+
alphabet: alphabet4,
|
|
41
|
+
base: alphabet4.length,
|
|
42
|
+
value: givenValue
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
var getBaseXEncoder = (alphabet4) => {
|
|
47
|
+
return createEncoder({
|
|
48
|
+
getSizeFromValue: (value) => {
|
|
49
|
+
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
|
|
50
|
+
if (!tailChars) return value.length;
|
|
51
|
+
const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
|
|
52
|
+
return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
|
|
53
|
+
},
|
|
54
|
+
write(value, bytes, offset) {
|
|
55
|
+
assertValidBaseString(alphabet4, value);
|
|
56
|
+
if (value === "") return offset;
|
|
57
|
+
const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
|
|
58
|
+
if (!tailChars) {
|
|
59
|
+
bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
|
|
60
|
+
return offset + leadingZeroes.length;
|
|
61
|
+
}
|
|
62
|
+
let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
|
|
63
|
+
const tailBytes = [];
|
|
64
|
+
while (base10Number > 0n) {
|
|
65
|
+
tailBytes.unshift(Number(base10Number % 256n));
|
|
66
|
+
base10Number /= 256n;
|
|
67
|
+
}
|
|
68
|
+
const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
|
|
69
|
+
bytes.set(bytesToAdd, offset);
|
|
70
|
+
return offset + bytesToAdd.length;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
var getBaseXDecoder = (alphabet4) => {
|
|
75
|
+
return createDecoder({
|
|
76
|
+
read(rawBytes, offset) {
|
|
77
|
+
const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
|
|
78
|
+
if (bytes.length === 0) return ["", 0];
|
|
79
|
+
let trailIndex = bytes.findIndex((n) => n !== 0);
|
|
80
|
+
trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
|
|
81
|
+
const leadingZeroes = alphabet4[0].repeat(trailIndex);
|
|
82
|
+
if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
|
|
83
|
+
const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
|
|
84
|
+
const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
|
|
85
|
+
return [leadingZeroes + tailChars, rawBytes.length];
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
function partitionLeadingZeroes(value, zeroCharacter) {
|
|
90
|
+
const [leadingZeros, tailChars] = value.split(new RegExp(`((?!${zeroCharacter}).*)`));
|
|
91
|
+
return [leadingZeros, tailChars];
|
|
92
|
+
}
|
|
93
|
+
function getBigIntFromBaseX(value, alphabet4) {
|
|
94
|
+
const base = BigInt(alphabet4.length);
|
|
95
|
+
let sum = 0n;
|
|
96
|
+
for (const char of value) {
|
|
97
|
+
sum *= base;
|
|
98
|
+
sum += BigInt(alphabet4.indexOf(char));
|
|
99
|
+
}
|
|
100
|
+
return sum;
|
|
101
|
+
}
|
|
102
|
+
function getBaseXFromBigInt(value, alphabet4) {
|
|
103
|
+
const base = BigInt(alphabet4.length);
|
|
104
|
+
const tailChars = [];
|
|
105
|
+
while (value > 0n) {
|
|
106
|
+
tailChars.unshift(alphabet4[Number(value % base)]);
|
|
107
|
+
value /= base;
|
|
108
|
+
}
|
|
109
|
+
return tailChars.join("");
|
|
110
|
+
}
|
|
111
|
+
var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
112
|
+
var getBase58Encoder = () => getBaseXEncoder(alphabet2);
|
|
113
|
+
var getBase58Decoder = () => getBaseXDecoder(alphabet2);
|
|
114
|
+
var memoizedAddressTableLookupEncoder;
|
|
115
|
+
function getAddressTableLookupEncoder() {
|
|
116
|
+
if (!memoizedAddressTableLookupEncoder) {
|
|
117
|
+
memoizedAddressTableLookupEncoder = getStructEncoder([
|
|
118
|
+
["lookupTableAddress", getAddressEncoder()],
|
|
119
|
+
[
|
|
120
|
+
"writableIndices",
|
|
121
|
+
getArrayEncoder(getU8Encoder(), { size: getShortU16Encoder() })
|
|
122
|
+
],
|
|
123
|
+
[
|
|
124
|
+
"readableIndices",
|
|
125
|
+
getArrayEncoder(getU8Encoder(), { size: getShortU16Encoder() })
|
|
126
|
+
]
|
|
127
|
+
]);
|
|
128
|
+
}
|
|
129
|
+
return memoizedAddressTableLookupEncoder;
|
|
130
|
+
}
|
|
131
|
+
var memoizedAddressTableLookupDecoder;
|
|
132
|
+
function getAddressTableLookupDecoder() {
|
|
133
|
+
if (!memoizedAddressTableLookupDecoder) {
|
|
134
|
+
memoizedAddressTableLookupDecoder = getStructDecoder([
|
|
135
|
+
["lookupTableAddress", getAddressDecoder()],
|
|
136
|
+
["writableIndices", getArrayDecoder(getU8Decoder(), { size: getShortU16Decoder() })],
|
|
137
|
+
["readableIndices", getArrayDecoder(getU8Decoder(), { size: getShortU16Decoder() })]
|
|
138
|
+
]);
|
|
139
|
+
}
|
|
140
|
+
return memoizedAddressTableLookupDecoder;
|
|
141
|
+
}
|
|
142
|
+
var memoizedU8Encoder;
|
|
143
|
+
function getMemoizedU8Encoder() {
|
|
144
|
+
if (!memoizedU8Encoder) memoizedU8Encoder = getU8Encoder();
|
|
145
|
+
return memoizedU8Encoder;
|
|
146
|
+
}
|
|
147
|
+
var memoizedU8Decoder;
|
|
148
|
+
function getMemoizedU8Decoder() {
|
|
149
|
+
if (!memoizedU8Decoder) memoizedU8Decoder = getU8Decoder();
|
|
150
|
+
return memoizedU8Decoder;
|
|
151
|
+
}
|
|
152
|
+
function getMessageHeaderEncoder() {
|
|
153
|
+
return getStructEncoder([
|
|
154
|
+
["numSignerAccounts", getMemoizedU8Encoder()],
|
|
155
|
+
["numReadonlySignerAccounts", getMemoizedU8Encoder()],
|
|
156
|
+
["numReadonlyNonSignerAccounts", getMemoizedU8Encoder()]
|
|
157
|
+
]);
|
|
158
|
+
}
|
|
159
|
+
function getMessageHeaderDecoder() {
|
|
160
|
+
return getStructDecoder([
|
|
161
|
+
["numSignerAccounts", getMemoizedU8Decoder()],
|
|
162
|
+
["numReadonlySignerAccounts", getMemoizedU8Decoder()],
|
|
163
|
+
["numReadonlyNonSignerAccounts", getMemoizedU8Decoder()]
|
|
164
|
+
]);
|
|
165
|
+
}
|
|
166
|
+
var memoizedGetInstructionEncoder;
|
|
167
|
+
function getInstructionEncoder() {
|
|
168
|
+
if (!memoizedGetInstructionEncoder) {
|
|
169
|
+
memoizedGetInstructionEncoder = transformEncoder(
|
|
170
|
+
getStructEncoder([
|
|
171
|
+
["programAddressIndex", getU8Encoder()],
|
|
172
|
+
["accountIndices", getArrayEncoder(getU8Encoder(), { size: getShortU16Encoder() })],
|
|
173
|
+
["data", addEncoderSizePrefix(getBytesEncoder(), getShortU16Encoder())]
|
|
174
|
+
]),
|
|
175
|
+
// Convert an instruction to have all fields defined
|
|
176
|
+
(instruction) => {
|
|
177
|
+
if (instruction.accountIndices !== void 0 && instruction.data !== void 0) {
|
|
178
|
+
return instruction;
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
...instruction,
|
|
182
|
+
accountIndices: instruction.accountIndices ?? [],
|
|
183
|
+
data: instruction.data ?? new Uint8Array(0)
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
return memoizedGetInstructionEncoder;
|
|
189
|
+
}
|
|
190
|
+
var memoizedGetInstructionDecoder;
|
|
191
|
+
function getInstructionDecoder() {
|
|
192
|
+
if (!memoizedGetInstructionDecoder) {
|
|
193
|
+
memoizedGetInstructionDecoder = transformDecoder(
|
|
194
|
+
getStructDecoder([
|
|
195
|
+
["programAddressIndex", getU8Decoder()],
|
|
196
|
+
["accountIndices", getArrayDecoder(getU8Decoder(), { size: getShortU16Decoder() })],
|
|
197
|
+
[
|
|
198
|
+
"data",
|
|
199
|
+
addDecoderSizePrefix(getBytesDecoder(), getShortU16Decoder())
|
|
200
|
+
]
|
|
201
|
+
]),
|
|
202
|
+
// Convert an instruction to exclude optional fields if they are empty
|
|
203
|
+
(instruction) => {
|
|
204
|
+
if (instruction.accountIndices.length && instruction.data.byteLength) {
|
|
205
|
+
return instruction;
|
|
206
|
+
}
|
|
207
|
+
const { accountIndices, data, ...rest } = instruction;
|
|
208
|
+
return {
|
|
209
|
+
...rest,
|
|
210
|
+
...accountIndices.length ? { accountIndices } : null,
|
|
211
|
+
...data.byteLength ? { data } : null
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
return memoizedGetInstructionDecoder;
|
|
217
|
+
}
|
|
218
|
+
var VERSION_FLAG_MASK = 128;
|
|
219
|
+
function getTransactionVersionEncoder() {
|
|
220
|
+
return createEncoder({
|
|
221
|
+
getSizeFromValue: (value) => value === "legacy" ? 0 : 1,
|
|
222
|
+
maxSize: 1,
|
|
223
|
+
write: (value, bytes, offset) => {
|
|
224
|
+
if (value === "legacy") {
|
|
225
|
+
return offset;
|
|
226
|
+
}
|
|
227
|
+
if (value < 0 || value > 127) {
|
|
228
|
+
throw new SolanaError(SOLANA_ERROR__TRANSACTION__VERSION_NUMBER_OUT_OF_RANGE, {
|
|
229
|
+
actualVersion: value
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
bytes.set([value | VERSION_FLAG_MASK], offset);
|
|
233
|
+
return offset + 1;
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
function getTransactionVersionDecoder() {
|
|
238
|
+
return createDecoder({
|
|
239
|
+
maxSize: 1,
|
|
240
|
+
read: (bytes, offset) => {
|
|
241
|
+
const firstByte = bytes[offset];
|
|
242
|
+
if ((firstByte & VERSION_FLAG_MASK) === 0) {
|
|
243
|
+
return ["legacy", offset];
|
|
244
|
+
} else {
|
|
245
|
+
const version = firstByte ^ VERSION_FLAG_MASK;
|
|
246
|
+
return [version, offset + 1];
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
function getTransactionVersionCodec() {
|
|
252
|
+
return combineCodec(getTransactionVersionEncoder(), getTransactionVersionDecoder());
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/codecs/message.ts
|
|
256
|
+
function getCompiledMessageLegacyEncoder() {
|
|
257
|
+
return getStructEncoder(getPreludeStructEncoderTuple());
|
|
258
|
+
}
|
|
259
|
+
function getCompiledMessageVersionedEncoder() {
|
|
260
|
+
return transformEncoder(
|
|
261
|
+
getStructEncoder([
|
|
262
|
+
...getPreludeStructEncoderTuple(),
|
|
263
|
+
["addressTableLookups", getAddressTableLookupArrayEncoder()]
|
|
264
|
+
]),
|
|
265
|
+
(value) => {
|
|
266
|
+
if (value.version === "legacy") {
|
|
267
|
+
return value;
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
...value,
|
|
271
|
+
addressTableLookups: value.addressTableLookups ?? []
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
function getPreludeStructEncoderTuple() {
|
|
277
|
+
return [
|
|
278
|
+
["version", getTransactionVersionEncoder()],
|
|
279
|
+
["header", getMessageHeaderEncoder()],
|
|
280
|
+
["staticAccounts", getArrayEncoder(getAddressEncoder(), { size: getShortU16Encoder() })],
|
|
281
|
+
["lifetimeToken", fixEncoderSize(getBase58Encoder(), 32)],
|
|
282
|
+
["instructions", getArrayEncoder(getInstructionEncoder(), { size: getShortU16Encoder() })]
|
|
283
|
+
];
|
|
284
|
+
}
|
|
285
|
+
function getPreludeStructDecoderTuple() {
|
|
286
|
+
return [
|
|
287
|
+
["version", getTransactionVersionDecoder()],
|
|
288
|
+
["header", getMessageHeaderDecoder()],
|
|
289
|
+
["staticAccounts", getArrayDecoder(getAddressDecoder(), { size: getShortU16Decoder() })],
|
|
290
|
+
["lifetimeToken", fixDecoderSize(getBase58Decoder(), 32)],
|
|
291
|
+
["instructions", getArrayDecoder(getInstructionDecoder(), { size: getShortU16Decoder() })],
|
|
292
|
+
["addressTableLookups", getAddressTableLookupArrayDecoder()]
|
|
293
|
+
];
|
|
294
|
+
}
|
|
295
|
+
function getAddressTableLookupArrayEncoder() {
|
|
296
|
+
return getArrayEncoder(getAddressTableLookupEncoder(), { size: getShortU16Encoder() });
|
|
297
|
+
}
|
|
298
|
+
function getAddressTableLookupArrayDecoder() {
|
|
299
|
+
return getArrayDecoder(getAddressTableLookupDecoder(), { size: getShortU16Decoder() });
|
|
300
|
+
}
|
|
301
|
+
function getCompiledTransactionMessageEncoder() {
|
|
302
|
+
return createEncoder({
|
|
303
|
+
getSizeFromValue: (compiledMessage) => {
|
|
304
|
+
if (compiledMessage.version === "legacy") {
|
|
305
|
+
return getCompiledMessageLegacyEncoder().getSizeFromValue(compiledMessage);
|
|
306
|
+
} else {
|
|
307
|
+
return getCompiledMessageVersionedEncoder().getSizeFromValue(compiledMessage);
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
write: (compiledMessage, bytes, offset) => {
|
|
311
|
+
if (compiledMessage.version === "legacy") {
|
|
312
|
+
return getCompiledMessageLegacyEncoder().write(compiledMessage, bytes, offset);
|
|
313
|
+
} else {
|
|
314
|
+
return getCompiledMessageVersionedEncoder().write(compiledMessage, bytes, offset);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
function getCompiledTransactionMessageDecoder() {
|
|
320
|
+
return transformDecoder(
|
|
321
|
+
getStructDecoder(getPreludeStructDecoderTuple()),
|
|
322
|
+
({ addressTableLookups, ...restOfMessage }) => {
|
|
323
|
+
if (restOfMessage.version === "legacy" || !addressTableLookups?.length) {
|
|
324
|
+
return restOfMessage;
|
|
325
|
+
}
|
|
326
|
+
return { ...restOfMessage, addressTableLookups };
|
|
327
|
+
}
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
function getCompiledTransactionMessageCodec() {
|
|
331
|
+
return combineCodec(getCompiledTransactionMessageEncoder(), getCompiledTransactionMessageDecoder());
|
|
332
|
+
}
|
|
333
|
+
function upsert(addressMap, address, update) {
|
|
334
|
+
addressMap[address] = update(addressMap[address] ?? { role: AccountRole.READONLY });
|
|
335
|
+
}
|
|
336
|
+
var TYPE = Symbol("AddressMapTypeProperty");
|
|
337
|
+
function getAddressMapFromInstructions(feePayer, instructions) {
|
|
338
|
+
const addressMap = {
|
|
339
|
+
[feePayer]: { [TYPE]: 0 /* FEE_PAYER */, role: AccountRole.WRITABLE_SIGNER }
|
|
340
|
+
};
|
|
341
|
+
const addressesOfInvokedPrograms = /* @__PURE__ */ new Set();
|
|
342
|
+
for (const instruction of instructions) {
|
|
343
|
+
upsert(addressMap, instruction.programAddress, (entry) => {
|
|
344
|
+
addressesOfInvokedPrograms.add(instruction.programAddress);
|
|
345
|
+
if (TYPE in entry) {
|
|
346
|
+
if (isWritableRole(entry.role)) {
|
|
347
|
+
switch (entry[TYPE]) {
|
|
348
|
+
case 0 /* FEE_PAYER */:
|
|
349
|
+
throw new SolanaError(SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_CANNOT_PAY_FEES, {
|
|
350
|
+
programAddress: instruction.programAddress
|
|
351
|
+
});
|
|
352
|
+
default:
|
|
353
|
+
throw new SolanaError(SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE, {
|
|
354
|
+
programAddress: instruction.programAddress
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (entry[TYPE] === 2 /* STATIC */) {
|
|
359
|
+
return entry;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return { [TYPE]: 2 /* STATIC */, role: AccountRole.READONLY };
|
|
363
|
+
});
|
|
364
|
+
let addressComparator;
|
|
365
|
+
if (!instruction.accounts) {
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
for (const account of instruction.accounts) {
|
|
369
|
+
upsert(addressMap, account.address, (entry) => {
|
|
370
|
+
const {
|
|
371
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
372
|
+
address: _,
|
|
373
|
+
...accountMeta
|
|
374
|
+
} = account;
|
|
375
|
+
if (TYPE in entry) {
|
|
376
|
+
switch (entry[TYPE]) {
|
|
377
|
+
case 0 /* FEE_PAYER */:
|
|
378
|
+
return entry;
|
|
379
|
+
case 1 /* LOOKUP_TABLE */: {
|
|
380
|
+
const nextRole = mergeRoles(entry.role, accountMeta.role);
|
|
381
|
+
if ("lookupTableAddress" in accountMeta) {
|
|
382
|
+
const shouldReplaceEntry = (
|
|
383
|
+
// Consider using the new LOOKUP_TABLE if its address is different...
|
|
384
|
+
entry.lookupTableAddress !== accountMeta.lookupTableAddress && // ...and sorts before the existing one.
|
|
385
|
+
(addressComparator ||= getAddressComparator())(
|
|
386
|
+
accountMeta.lookupTableAddress,
|
|
387
|
+
entry.lookupTableAddress
|
|
388
|
+
) < 0
|
|
389
|
+
);
|
|
390
|
+
if (shouldReplaceEntry) {
|
|
391
|
+
return {
|
|
392
|
+
[TYPE]: 1 /* LOOKUP_TABLE */,
|
|
393
|
+
...accountMeta,
|
|
394
|
+
role: nextRole
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
} else if (isSignerRole(accountMeta.role)) {
|
|
398
|
+
return {
|
|
399
|
+
[TYPE]: 2 /* STATIC */,
|
|
400
|
+
role: nextRole
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
if (entry.role !== nextRole) {
|
|
404
|
+
return {
|
|
405
|
+
...entry,
|
|
406
|
+
role: nextRole
|
|
407
|
+
};
|
|
408
|
+
} else {
|
|
409
|
+
return entry;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
case 2 /* STATIC */: {
|
|
413
|
+
const nextRole = mergeRoles(entry.role, accountMeta.role);
|
|
414
|
+
if (
|
|
415
|
+
// Check to see if this address represents a program that is invoked
|
|
416
|
+
// in this transaction.
|
|
417
|
+
addressesOfInvokedPrograms.has(account.address)
|
|
418
|
+
) {
|
|
419
|
+
if (isWritableRole(accountMeta.role)) {
|
|
420
|
+
throw new SolanaError(
|
|
421
|
+
SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE,
|
|
422
|
+
{
|
|
423
|
+
programAddress: account.address
|
|
424
|
+
}
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
if (entry.role !== nextRole) {
|
|
428
|
+
return {
|
|
429
|
+
...entry,
|
|
430
|
+
role: nextRole
|
|
431
|
+
};
|
|
432
|
+
} else {
|
|
433
|
+
return entry;
|
|
434
|
+
}
|
|
435
|
+
} else if ("lookupTableAddress" in accountMeta && // Static accounts can be 'upgraded' to lookup table accounts as
|
|
436
|
+
// long as they are not require to sign the transaction.
|
|
437
|
+
!isSignerRole(entry.role)) {
|
|
438
|
+
return {
|
|
439
|
+
...accountMeta,
|
|
440
|
+
[TYPE]: 1 /* LOOKUP_TABLE */,
|
|
441
|
+
role: nextRole
|
|
442
|
+
};
|
|
443
|
+
} else {
|
|
444
|
+
if (entry.role !== nextRole) {
|
|
445
|
+
return {
|
|
446
|
+
...entry,
|
|
447
|
+
role: nextRole
|
|
448
|
+
};
|
|
449
|
+
} else {
|
|
450
|
+
return entry;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
if ("lookupTableAddress" in accountMeta) {
|
|
457
|
+
return {
|
|
458
|
+
...accountMeta,
|
|
459
|
+
[TYPE]: 1 /* LOOKUP_TABLE */
|
|
460
|
+
};
|
|
461
|
+
} else {
|
|
462
|
+
return {
|
|
463
|
+
...accountMeta,
|
|
464
|
+
[TYPE]: 2 /* STATIC */
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return addressMap;
|
|
471
|
+
}
|
|
472
|
+
function getOrderedAccountsFromAddressMap(addressMap) {
|
|
473
|
+
let addressComparator;
|
|
474
|
+
const orderedAccounts = Object.entries(addressMap).sort(([leftAddress, leftEntry], [rightAddress, rightEntry]) => {
|
|
475
|
+
if (leftEntry[TYPE] !== rightEntry[TYPE]) {
|
|
476
|
+
if (leftEntry[TYPE] === 0 /* FEE_PAYER */) {
|
|
477
|
+
return -1;
|
|
478
|
+
} else if (rightEntry[TYPE] === 0 /* FEE_PAYER */) {
|
|
479
|
+
return 1;
|
|
480
|
+
} else if (leftEntry[TYPE] === 2 /* STATIC */) {
|
|
481
|
+
return -1;
|
|
482
|
+
} else if (rightEntry[TYPE] === 2 /* STATIC */) {
|
|
483
|
+
return 1;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
const leftIsSigner = isSignerRole(leftEntry.role);
|
|
487
|
+
if (leftIsSigner !== isSignerRole(rightEntry.role)) {
|
|
488
|
+
return leftIsSigner ? -1 : 1;
|
|
489
|
+
}
|
|
490
|
+
const leftIsWritable = isWritableRole(leftEntry.role);
|
|
491
|
+
if (leftIsWritable !== isWritableRole(rightEntry.role)) {
|
|
492
|
+
return leftIsWritable ? -1 : 1;
|
|
493
|
+
}
|
|
494
|
+
addressComparator ||= getAddressComparator();
|
|
495
|
+
if (leftEntry[TYPE] === 1 /* LOOKUP_TABLE */ && rightEntry[TYPE] === 1 /* LOOKUP_TABLE */ && leftEntry.lookupTableAddress !== rightEntry.lookupTableAddress) {
|
|
496
|
+
return addressComparator(leftEntry.lookupTableAddress, rightEntry.lookupTableAddress);
|
|
497
|
+
} else {
|
|
498
|
+
return addressComparator(leftAddress, rightAddress);
|
|
499
|
+
}
|
|
500
|
+
}).map(([address, addressMeta]) => ({
|
|
501
|
+
address,
|
|
502
|
+
...addressMeta
|
|
503
|
+
}));
|
|
504
|
+
return orderedAccounts;
|
|
505
|
+
}
|
|
506
|
+
function getCompiledAddressTableLookups(orderedAccounts) {
|
|
507
|
+
const index = {};
|
|
508
|
+
for (const account of orderedAccounts) {
|
|
509
|
+
if (!("lookupTableAddress" in account)) {
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
const entry = index[account.lookupTableAddress] ||= {
|
|
513
|
+
readableIndices: [],
|
|
514
|
+
writableIndices: []
|
|
515
|
+
};
|
|
516
|
+
if (account.role === AccountRole.WRITABLE) {
|
|
517
|
+
entry.writableIndices.push(account.addressIndex);
|
|
518
|
+
} else {
|
|
519
|
+
entry.readableIndices.push(account.addressIndex);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return Object.keys(index).sort(getAddressComparator()).map((lookupTableAddress) => ({
|
|
523
|
+
lookupTableAddress,
|
|
524
|
+
...index[lookupTableAddress]
|
|
525
|
+
}));
|
|
526
|
+
}
|
|
527
|
+
function getCompiledMessageHeader(orderedAccounts) {
|
|
528
|
+
let numReadonlyNonSignerAccounts = 0;
|
|
529
|
+
let numReadonlySignerAccounts = 0;
|
|
530
|
+
let numSignerAccounts = 0;
|
|
531
|
+
for (const account of orderedAccounts) {
|
|
532
|
+
if ("lookupTableAddress" in account) {
|
|
533
|
+
break;
|
|
534
|
+
}
|
|
535
|
+
const accountIsWritable = isWritableRole(account.role);
|
|
536
|
+
if (isSignerRole(account.role)) {
|
|
537
|
+
numSignerAccounts++;
|
|
538
|
+
if (!accountIsWritable) {
|
|
539
|
+
numReadonlySignerAccounts++;
|
|
540
|
+
}
|
|
541
|
+
} else if (!accountIsWritable) {
|
|
542
|
+
numReadonlyNonSignerAccounts++;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return {
|
|
546
|
+
numReadonlyNonSignerAccounts,
|
|
547
|
+
numReadonlySignerAccounts,
|
|
548
|
+
numSignerAccounts
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// src/compile/instructions.ts
|
|
553
|
+
function getAccountIndex(orderedAccounts) {
|
|
554
|
+
const out = {};
|
|
555
|
+
for (const [index, account] of orderedAccounts.entries()) {
|
|
556
|
+
out[account.address] = index;
|
|
557
|
+
}
|
|
558
|
+
return out;
|
|
559
|
+
}
|
|
560
|
+
function getCompiledInstructions(instructions, orderedAccounts) {
|
|
561
|
+
const accountIndex = getAccountIndex(orderedAccounts);
|
|
562
|
+
return instructions.map(({ accounts, data, programAddress }) => {
|
|
563
|
+
return {
|
|
564
|
+
programAddressIndex: accountIndex[programAddress],
|
|
565
|
+
...accounts ? { accountIndices: accounts.map(({ address }) => accountIndex[address]) } : null,
|
|
566
|
+
...data ? { data } : null
|
|
567
|
+
};
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// src/compile/lifetime-token.ts
|
|
572
|
+
function getCompiledLifetimeToken(lifetimeConstraint) {
|
|
573
|
+
if ("nonce" in lifetimeConstraint) {
|
|
574
|
+
return lifetimeConstraint.nonce;
|
|
575
|
+
}
|
|
576
|
+
return lifetimeConstraint.blockhash;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// src/compile/static-accounts.ts
|
|
580
|
+
function getCompiledStaticAccounts(orderedAccounts) {
|
|
581
|
+
const firstLookupTableAccountIndex = orderedAccounts.findIndex((account) => "lookupTableAddress" in account);
|
|
582
|
+
const orderedStaticAccounts = firstLookupTableAccountIndex === -1 ? orderedAccounts : orderedAccounts.slice(0, firstLookupTableAccountIndex);
|
|
583
|
+
return orderedStaticAccounts.map(({ address }) => address);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// src/compile/message.ts
|
|
587
|
+
function compileTransactionMessage(transaction) {
|
|
588
|
+
const addressMap = getAddressMapFromInstructions(transaction.feePayer, transaction.instructions);
|
|
589
|
+
const orderedAccounts = getOrderedAccountsFromAddressMap(addressMap);
|
|
590
|
+
return {
|
|
591
|
+
...transaction.version !== "legacy" ? { addressTableLookups: getCompiledAddressTableLookups(orderedAccounts) } : null,
|
|
592
|
+
header: getCompiledMessageHeader(orderedAccounts),
|
|
593
|
+
instructions: getCompiledInstructions(transaction.instructions, orderedAccounts),
|
|
594
|
+
lifetimeToken: getCompiledLifetimeToken(transaction.lifetimeConstraint),
|
|
595
|
+
staticAccounts: getCompiledStaticAccounts(orderedAccounts),
|
|
596
|
+
version: transaction.version
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
function findAddressInLookupTables(address, role, addressesByLookupTableAddress) {
|
|
600
|
+
for (const [lookupTableAddress, addresses] of Object.entries(addressesByLookupTableAddress)) {
|
|
601
|
+
for (let i = 0; i < addresses.length; i++) {
|
|
602
|
+
if (address === addresses[i]) {
|
|
603
|
+
return {
|
|
604
|
+
address,
|
|
605
|
+
addressIndex: i,
|
|
606
|
+
lookupTableAddress,
|
|
607
|
+
role
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
function compressTransactionMessageUsingAddressLookupTables(transactionMessage, addressesByLookupTableAddress) {
|
|
614
|
+
const lookupTableAddresses = new Set(Object.values(addressesByLookupTableAddress).flatMap((a) => a));
|
|
615
|
+
const newInstructions = [];
|
|
616
|
+
let updatedAnyInstructions = false;
|
|
617
|
+
for (const instruction of transactionMessage.instructions) {
|
|
618
|
+
if (!instruction.accounts) {
|
|
619
|
+
newInstructions.push(instruction);
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
const newAccounts = [];
|
|
623
|
+
let updatedAnyAccounts = false;
|
|
624
|
+
for (const account of instruction.accounts) {
|
|
625
|
+
if ("lookupTableAddress" in account || !lookupTableAddresses.has(account.address) || isSignerRole(account.role)) {
|
|
626
|
+
newAccounts.push(account);
|
|
627
|
+
continue;
|
|
628
|
+
}
|
|
629
|
+
const lookupMetaAccount = findAddressInLookupTables(
|
|
630
|
+
account.address,
|
|
631
|
+
account.role,
|
|
632
|
+
addressesByLookupTableAddress
|
|
633
|
+
);
|
|
634
|
+
newAccounts.push(Object.freeze(lookupMetaAccount));
|
|
635
|
+
updatedAnyAccounts = true;
|
|
636
|
+
updatedAnyInstructions = true;
|
|
637
|
+
}
|
|
638
|
+
newInstructions.push(
|
|
639
|
+
Object.freeze(updatedAnyAccounts ? { ...instruction, accounts: newAccounts } : instruction)
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
return Object.freeze(
|
|
643
|
+
updatedAnyInstructions ? { ...transactionMessage, instructions: newInstructions } : transactionMessage
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// src/create-transaction-message.ts
|
|
648
|
+
function createTransactionMessage({
|
|
649
|
+
version
|
|
650
|
+
}) {
|
|
651
|
+
return Object.freeze({
|
|
652
|
+
instructions: Object.freeze([]),
|
|
653
|
+
version
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
var RECENT_BLOCKHASHES_SYSVAR_ADDRESS = "SysvarRecentB1ockHashes11111111111111111111";
|
|
657
|
+
var SYSTEM_PROGRAM_ADDRESS = "11111111111111111111111111111111";
|
|
658
|
+
function assertIsDurableNonceTransactionMessage(transaction) {
|
|
659
|
+
if (!isDurableNonceTransaction(transaction)) {
|
|
660
|
+
throw new SolanaError(SOLANA_ERROR__TRANSACTION__EXPECTED_NONCE_LIFETIME);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
function createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress) {
|
|
664
|
+
return {
|
|
665
|
+
accounts: [
|
|
666
|
+
{ address: nonceAccountAddress, role: AccountRole.WRITABLE },
|
|
667
|
+
{
|
|
668
|
+
address: RECENT_BLOCKHASHES_SYSVAR_ADDRESS,
|
|
669
|
+
role: AccountRole.READONLY
|
|
670
|
+
},
|
|
671
|
+
{ address: nonceAuthorityAddress, role: AccountRole.READONLY_SIGNER }
|
|
672
|
+
],
|
|
673
|
+
data: new Uint8Array([4, 0, 0, 0]),
|
|
674
|
+
programAddress: SYSTEM_PROGRAM_ADDRESS
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
function isAdvanceNonceAccountInstruction(instruction) {
|
|
678
|
+
return instruction.programAddress === SYSTEM_PROGRAM_ADDRESS && // Test for `AdvanceNonceAccount` instruction data
|
|
679
|
+
instruction.data != null && isAdvanceNonceAccountInstructionData(instruction.data) && // Test for exactly 3 accounts
|
|
680
|
+
instruction.accounts?.length === 3 && // First account is nonce account address
|
|
681
|
+
instruction.accounts[0].address != null && instruction.accounts[0].role === AccountRole.WRITABLE && // Second account is recent blockhashes sysvar
|
|
682
|
+
instruction.accounts[1].address === RECENT_BLOCKHASHES_SYSVAR_ADDRESS && instruction.accounts[1].role === AccountRole.READONLY && // Third account is nonce authority account
|
|
683
|
+
instruction.accounts[2].address != null && isSignerRole(instruction.accounts[2].role);
|
|
684
|
+
}
|
|
685
|
+
function isAdvanceNonceAccountInstructionData(data) {
|
|
686
|
+
return data.byteLength === 4 && data[0] === 4 && data[1] === 0 && data[2] === 0 && data[3] === 0;
|
|
687
|
+
}
|
|
688
|
+
function isDurableNonceTransaction(transaction) {
|
|
689
|
+
return "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.nonce === "string" && transaction.instructions[0] != null && isAdvanceNonceAccountInstruction(transaction.instructions[0]);
|
|
690
|
+
}
|
|
691
|
+
function isAdvanceNonceAccountInstructionForNonce(instruction, nonceAccountAddress, nonceAuthorityAddress) {
|
|
692
|
+
return instruction.accounts[0].address === nonceAccountAddress && instruction.accounts[2].address === nonceAuthorityAddress;
|
|
693
|
+
}
|
|
694
|
+
function setTransactionMessageLifetimeUsingDurableNonce({
|
|
695
|
+
nonce,
|
|
696
|
+
nonceAccountAddress,
|
|
697
|
+
nonceAuthorityAddress
|
|
698
|
+
}, transaction) {
|
|
699
|
+
let newInstructions;
|
|
700
|
+
const firstInstruction = transaction.instructions[0];
|
|
701
|
+
if (firstInstruction && isAdvanceNonceAccountInstruction(firstInstruction)) {
|
|
702
|
+
if (isAdvanceNonceAccountInstructionForNonce(firstInstruction, nonceAccountAddress, nonceAuthorityAddress)) {
|
|
703
|
+
if (isDurableNonceTransaction(transaction) && transaction.lifetimeConstraint.nonce === nonce) {
|
|
704
|
+
return transaction;
|
|
705
|
+
} else {
|
|
706
|
+
newInstructions = [firstInstruction, ...transaction.instructions.slice(1)];
|
|
707
|
+
}
|
|
708
|
+
} else {
|
|
709
|
+
newInstructions = [
|
|
710
|
+
Object.freeze(createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress)),
|
|
711
|
+
...transaction.instructions.slice(1)
|
|
712
|
+
];
|
|
713
|
+
}
|
|
714
|
+
} else {
|
|
715
|
+
newInstructions = [
|
|
716
|
+
Object.freeze(createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress)),
|
|
717
|
+
...transaction.instructions
|
|
718
|
+
];
|
|
719
|
+
}
|
|
720
|
+
return Object.freeze({
|
|
721
|
+
...transaction,
|
|
722
|
+
instructions: Object.freeze(newInstructions),
|
|
723
|
+
lifetimeConstraint: Object.freeze({
|
|
724
|
+
nonce
|
|
725
|
+
})
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
// src/fee-payer.ts
|
|
730
|
+
function setTransactionMessageFeePayer(feePayer, transaction) {
|
|
731
|
+
if ("feePayer" in transaction && feePayer === transaction.feePayer) {
|
|
732
|
+
return transaction;
|
|
733
|
+
}
|
|
734
|
+
const out = {
|
|
735
|
+
...transaction,
|
|
736
|
+
feePayer
|
|
737
|
+
};
|
|
738
|
+
Object.freeze(out);
|
|
739
|
+
return out;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// src/instructions.ts
|
|
743
|
+
function appendTransactionMessageInstruction(instruction, transaction) {
|
|
744
|
+
return appendTransactionMessageInstructions([instruction], transaction);
|
|
745
|
+
}
|
|
746
|
+
function appendTransactionMessageInstructions(instructions, transaction) {
|
|
747
|
+
return Object.freeze({
|
|
748
|
+
...transaction,
|
|
749
|
+
instructions: Object.freeze([...transaction.instructions, ...instructions])
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
function prependTransactionMessageInstruction(instruction, transaction) {
|
|
753
|
+
return prependTransactionMessageInstructions([instruction], transaction);
|
|
754
|
+
}
|
|
755
|
+
function prependTransactionMessageInstructions(instructions, transaction) {
|
|
756
|
+
return Object.freeze({
|
|
757
|
+
...transaction,
|
|
758
|
+
instructions: Object.freeze([...instructions, ...transaction.instructions])
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
// src/decompile-message.ts
|
|
763
|
+
function getAccountMetas(message) {
|
|
764
|
+
const { header } = message;
|
|
765
|
+
const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
|
|
766
|
+
const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
|
|
767
|
+
const accountMetas = [];
|
|
768
|
+
let accountIndex = 0;
|
|
769
|
+
for (let i = 0; i < numWritableSignerAccounts; i++) {
|
|
770
|
+
accountMetas.push({
|
|
771
|
+
address: message.staticAccounts[accountIndex],
|
|
772
|
+
role: AccountRole.WRITABLE_SIGNER
|
|
773
|
+
});
|
|
774
|
+
accountIndex++;
|
|
775
|
+
}
|
|
776
|
+
for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
|
|
777
|
+
accountMetas.push({
|
|
778
|
+
address: message.staticAccounts[accountIndex],
|
|
779
|
+
role: AccountRole.READONLY_SIGNER
|
|
780
|
+
});
|
|
781
|
+
accountIndex++;
|
|
782
|
+
}
|
|
783
|
+
for (let i = 0; i < numWritableNonSignerAccounts; i++) {
|
|
784
|
+
accountMetas.push({
|
|
785
|
+
address: message.staticAccounts[accountIndex],
|
|
786
|
+
role: AccountRole.WRITABLE
|
|
787
|
+
});
|
|
788
|
+
accountIndex++;
|
|
789
|
+
}
|
|
790
|
+
for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
|
|
791
|
+
accountMetas.push({
|
|
792
|
+
address: message.staticAccounts[accountIndex],
|
|
793
|
+
role: AccountRole.READONLY
|
|
794
|
+
});
|
|
795
|
+
accountIndex++;
|
|
796
|
+
}
|
|
797
|
+
return accountMetas;
|
|
798
|
+
}
|
|
799
|
+
function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTableAddress) {
|
|
800
|
+
const compiledAddressTableLookupAddresses = compiledAddressTableLookups.map((l) => l.lookupTableAddress);
|
|
801
|
+
const missing = compiledAddressTableLookupAddresses.filter((a) => addressesByLookupTableAddress[a] === void 0);
|
|
802
|
+
if (missing.length > 0) {
|
|
803
|
+
throw new SolanaError(SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_ADDRESS_LOOKUP_TABLE_CONTENTS_MISSING, {
|
|
804
|
+
lookupTableAddresses: missing
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
const readOnlyMetas = [];
|
|
808
|
+
const writableMetas = [];
|
|
809
|
+
for (const lookup of compiledAddressTableLookups) {
|
|
810
|
+
const addresses = addressesByLookupTableAddress[lookup.lookupTableAddress];
|
|
811
|
+
const highestIndex = Math.max(...lookup.readableIndices, ...lookup.writableIndices);
|
|
812
|
+
if (highestIndex >= addresses.length) {
|
|
813
|
+
throw new SolanaError(
|
|
814
|
+
SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_ADDRESS_LOOKUP_TABLE_INDEX_OUT_OF_RANGE,
|
|
815
|
+
{
|
|
816
|
+
highestKnownIndex: addresses.length - 1,
|
|
817
|
+
highestRequestedIndex: highestIndex,
|
|
818
|
+
lookupTableAddress: lookup.lookupTableAddress
|
|
819
|
+
}
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
const readOnlyForLookup = lookup.readableIndices.map((r) => ({
|
|
823
|
+
address: addresses[r],
|
|
824
|
+
addressIndex: r,
|
|
825
|
+
lookupTableAddress: lookup.lookupTableAddress,
|
|
826
|
+
role: AccountRole.READONLY
|
|
827
|
+
}));
|
|
828
|
+
readOnlyMetas.push(...readOnlyForLookup);
|
|
829
|
+
const writableForLookup = lookup.writableIndices.map((w) => ({
|
|
830
|
+
address: addresses[w],
|
|
831
|
+
addressIndex: w,
|
|
832
|
+
lookupTableAddress: lookup.lookupTableAddress,
|
|
833
|
+
role: AccountRole.WRITABLE
|
|
834
|
+
}));
|
|
835
|
+
writableMetas.push(...writableForLookup);
|
|
836
|
+
}
|
|
837
|
+
return [...writableMetas, ...readOnlyMetas];
|
|
838
|
+
}
|
|
839
|
+
function convertInstruction(instruction, accountMetas) {
|
|
840
|
+
const programAddress = accountMetas[instruction.programAddressIndex]?.address;
|
|
841
|
+
if (!programAddress) {
|
|
842
|
+
throw new SolanaError(SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_INSTRUCTION_PROGRAM_ADDRESS_NOT_FOUND, {
|
|
843
|
+
index: instruction.programAddressIndex
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
|
|
847
|
+
const { data } = instruction;
|
|
848
|
+
return Object.freeze({
|
|
849
|
+
programAddress,
|
|
850
|
+
...accounts && accounts.length ? { accounts: Object.freeze(accounts) } : {},
|
|
851
|
+
...data && data.length ? { data } : {}
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
|
|
855
|
+
if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
|
|
856
|
+
return {
|
|
857
|
+
blockhash: messageLifetimeToken,
|
|
858
|
+
lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
|
|
859
|
+
// U64 MAX
|
|
860
|
+
};
|
|
861
|
+
} else {
|
|
862
|
+
const nonceAccountAddress = firstInstruction.accounts[0].address;
|
|
863
|
+
assertIsAddress(nonceAccountAddress);
|
|
864
|
+
const nonceAuthorityAddress = firstInstruction.accounts[2].address;
|
|
865
|
+
assertIsAddress(nonceAuthorityAddress);
|
|
866
|
+
return {
|
|
867
|
+
nonce: messageLifetimeToken,
|
|
868
|
+
nonceAccountAddress,
|
|
869
|
+
nonceAuthorityAddress
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
function decompileTransactionMessage(compiledTransactionMessage, config) {
|
|
874
|
+
const feePayer = compiledTransactionMessage.staticAccounts[0];
|
|
875
|
+
if (!feePayer) {
|
|
876
|
+
throw new SolanaError(SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_FEE_PAYER_MISSING);
|
|
877
|
+
}
|
|
878
|
+
const accountMetas = getAccountMetas(compiledTransactionMessage);
|
|
879
|
+
const accountLookupMetas = "addressTableLookups" in compiledTransactionMessage && compiledTransactionMessage.addressTableLookups !== void 0 && compiledTransactionMessage.addressTableLookups.length > 0 ? getAddressLookupMetas(
|
|
880
|
+
compiledTransactionMessage.addressTableLookups,
|
|
881
|
+
config?.addressesByLookupTableAddress ?? {}
|
|
882
|
+
) : [];
|
|
883
|
+
const transactionMetas = [...accountMetas, ...accountLookupMetas];
|
|
884
|
+
const instructions = compiledTransactionMessage.instructions.map(
|
|
885
|
+
(compiledInstruction) => convertInstruction(compiledInstruction, transactionMetas)
|
|
886
|
+
);
|
|
887
|
+
const firstInstruction = instructions[0];
|
|
888
|
+
const lifetimeConstraint = getLifetimeConstraint(
|
|
889
|
+
compiledTransactionMessage.lifetimeToken,
|
|
890
|
+
firstInstruction,
|
|
891
|
+
config?.lastValidBlockHeight
|
|
892
|
+
);
|
|
893
|
+
return pipe(
|
|
894
|
+
createTransactionMessage({ version: compiledTransactionMessage.version }),
|
|
895
|
+
(tx) => setTransactionMessageFeePayer(feePayer, tx),
|
|
896
|
+
(tx) => instructions.reduce((acc, instruction) => {
|
|
897
|
+
return appendTransactionMessageInstruction(instruction, acc);
|
|
898
|
+
}, tx),
|
|
899
|
+
(tx) => "blockhash" in lifetimeConstraint ? setTransactionMessageLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionMessageLifetimeUsingDurableNonce(lifetimeConstraint, tx)
|
|
900
|
+
);
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
export { appendTransactionMessageInstruction, appendTransactionMessageInstructions, assertIsDurableNonceTransactionMessage, assertIsTransactionMessageWithBlockhashLifetime, compileTransactionMessage, compressTransactionMessageUsingAddressLookupTables, createTransactionMessage, decompileTransactionMessage, getCompiledTransactionMessageCodec, getCompiledTransactionMessageDecoder, getCompiledTransactionMessageEncoder, getTransactionVersionCodec, getTransactionVersionDecoder, getTransactionVersionEncoder, isAdvanceNonceAccountInstruction, isDurableNonceTransaction, isTransactionMessageWithBlockhashLifetime, prependTransactionMessageInstruction, prependTransactionMessageInstructions, setTransactionMessageFeePayer, setTransactionMessageLifetimeUsingBlockhash, setTransactionMessageLifetimeUsingDurableNonce };
|
|
904
|
+
//# sourceMappingURL=index.browser.mjs.map
|
|
905
|
+
//# sourceMappingURL=index.browser.mjs.map
|