@solana/transactions 2.0.0-experimental.fd64233 → 2.0.0-experimental.feaeef2

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.
@@ -1,33 +1,31 @@
1
+ import { SolanaError, SOLANA_ERROR__TRANSACTION__EXPECTED_BLOCKHASH_LIFETIME, SOLANA_ERROR__TRANSACTION__EXPECTED_NONCE_LIFETIME, SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_FEE_PAYER_MISSING, SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING, SOLANA_ERROR__TRANSACTION__SIGNATURES_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__BLOCKHASH_STRING_LENGTH_OUT_OF_RANGE, SOLANA_ERROR__INVALID_BLOCKHASH_BYTE_LENGTH, SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE, SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_CANNOT_PAY_FEES, SOLANA_ERROR__TRANSACTION__VERSION_NUMBER_OUT_OF_RANGE } from '@solana/errors';
1
2
  import { getBase58Decoder, getBase64Decoder, getStringEncoder, getBase58Encoder, getStringDecoder } from '@solana/codecs-strings';
2
3
  import { getAddressFromPublicKey, assertIsAddress, getAddressComparator, getAddressEncoder, getAddressDecoder } from '@solana/addresses';
3
4
  import { pipe } from '@solana/functional';
4
5
  import { createEncoder, mapDecoder, combineCodec, mapEncoder, createDecoder } from '@solana/codecs-core';
5
6
  import { getStructDecoder, getArrayDecoder, getBytesDecoder, getStructEncoder, getArrayEncoder, getBytesEncoder } from '@solana/codecs-data-structures';
6
7
  import { getShortU16Decoder, getShortU16Encoder, getU8Encoder, getU8Decoder } from '@solana/codecs-numbers';
7
- import { SolanaError, SOLANA_ERROR__TRANSACTION_SIGNATURE_NOT_COMPUTABLE, SOLANA_ERROR__TRANSACTION_MISSING_SIGNATURES } from '@solana/errors';
8
8
  import { signBytes } from '@solana/keys';
9
9
 
10
- // ../rpc-types/dist/index.browser.js
10
+ // src/blockhash.ts
11
11
  var base58Encoder;
12
12
  function assertIsBlockhash(putativeBlockhash) {
13
13
  if (!base58Encoder)
14
14
  base58Encoder = getBase58Encoder();
15
- try {
16
- if (
17
- // Lowest value (32 bytes of zeroes)
18
- putativeBlockhash.length < 32 || // Highest value (32 bytes of 255)
19
- putativeBlockhash.length > 44
20
- ) {
21
- throw new Error("Expected input string to decode to a byte array of length 32.");
22
- }
23
- const bytes = base58Encoder.encode(putativeBlockhash);
24
- const numBytes = bytes.byteLength;
25
- if (numBytes !== 32) {
26
- throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
27
- }
28
- } catch (e) {
29
- throw new Error(`\`${putativeBlockhash}\` is not a blockhash`, {
30
- cause: e
15
+ if (
16
+ // Lowest value (32 bytes of zeroes)
17
+ putativeBlockhash.length < 32 || // Highest value (32 bytes of 255)
18
+ putativeBlockhash.length > 44
19
+ ) {
20
+ throw new SolanaError(SOLANA_ERROR__BLOCKHASH_STRING_LENGTH_OUT_OF_RANGE, {
21
+ actualLength: putativeBlockhash.length
22
+ });
23
+ }
24
+ const bytes = base58Encoder.encode(putativeBlockhash);
25
+ const numBytes = bytes.byteLength;
26
+ if (numBytes !== 32) {
27
+ throw new SolanaError(SOLANA_ERROR__INVALID_BLOCKHASH_BYTE_LENGTH, {
28
+ actualLength: numBytes
31
29
  });
32
30
  }
33
31
  }
@@ -60,7 +58,7 @@ function isTransactionWithBlockhashLifetime(transaction) {
60
58
  }
61
59
  function assertIsTransactionWithBlockhashLifetime(transaction) {
62
60
  if (!isTransactionWithBlockhashLifetime(transaction)) {
63
- throw new Error("Transaction does not have a blockhash lifetime");
61
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__EXPECTED_BLOCKHASH_LIFETIME);
64
62
  }
65
63
  }
66
64
  function setTransactionLifetimeUsingBlockhash(blockhashLifetimeConstraint, transaction) {
@@ -86,8 +84,6 @@ function createTransaction({
86
84
  Object.freeze(out);
87
85
  return out;
88
86
  }
89
-
90
- // ../instructions/dist/index.browser.js
91
87
  var AccountRole = /* @__PURE__ */ ((AccountRole2) => {
92
88
  AccountRole2[AccountRole2["WRITABLE_SIGNER"] = /* 3 */
93
89
  3] = "WRITABLE_SIGNER";
@@ -109,13 +105,11 @@ function isWritableRole(role) {
109
105
  function mergeRoles(roleA, roleB) {
110
106
  return roleA | roleB;
111
107
  }
112
-
113
- // src/durable-nonce.ts
114
108
  var RECENT_BLOCKHASHES_SYSVAR_ADDRESS = "SysvarRecentB1ockHashes11111111111111111111";
115
109
  var SYSTEM_PROGRAM_ADDRESS = "11111111111111111111111111111111";
116
110
  function assertIsDurableNonceTransaction(transaction) {
117
111
  if (!isDurableNonceTransaction(transaction)) {
118
- throw new Error("Transaction is not a durable nonce transaction");
112
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__EXPECTED_NONCE_LIFETIME);
119
113
  }
120
114
  }
121
115
  function createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress) {
@@ -201,17 +195,23 @@ function setTransactionFeePayer(feePayer, transaction) {
201
195
 
202
196
  // src/instructions.ts
203
197
  function appendTransactionInstruction(instruction, transaction) {
198
+ return appendTransactionInstructions([instruction], transaction);
199
+ }
200
+ function appendTransactionInstructions(instructions, transaction) {
204
201
  const out = {
205
202
  ...getUnsignedTransaction(transaction),
206
- instructions: [...transaction.instructions, instruction]
203
+ instructions: [...transaction.instructions, ...instructions]
207
204
  };
208
205
  Object.freeze(out);
209
206
  return out;
210
207
  }
211
208
  function prependTransactionInstruction(instruction, transaction) {
209
+ return prependTransactionInstructions([instruction], transaction);
210
+ }
211
+ function prependTransactionInstructions(instructions, transaction) {
212
212
  const out = {
213
213
  ...getUnsignedTransaction(transaction),
214
- instructions: [instruction, ...transaction.instructions]
214
+ instructions: [...instructions, ...transaction.instructions]
215
215
  };
216
216
  Object.freeze(out);
217
217
  return out;
@@ -258,8 +258,9 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
258
258
  const compiledAddressTableLookupAddresses = compiledAddressTableLookups.map((l) => l.lookupTableAddress);
259
259
  const missing = compiledAddressTableLookupAddresses.filter((a) => addressesByLookupTableAddress[a] === void 0);
260
260
  if (missing.length > 0) {
261
- const missingAddresses = missing.join(", ");
262
- throw new Error(`Addresses not provided for lookup tables: [${missingAddresses}]`);
261
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_ADDRESS_LOOKUP_TABLE_CONTENTS_MISSING, {
262
+ lookupTableAddresses: missing
263
+ });
263
264
  }
264
265
  const readOnlyMetas = [];
265
266
  const writableMetas = [];
@@ -267,8 +268,13 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
267
268
  const addresses = addressesByLookupTableAddress[lookup.lookupTableAddress];
268
269
  const highestIndex = Math.max(...lookup.readableIndices, ...lookup.writableIndices);
269
270
  if (highestIndex >= addresses.length) {
270
- throw new Error(
271
- `Cannot look up index ${highestIndex} in lookup table [${lookup.lookupTableAddress}]. The lookup table may have been extended since the addresses provided were retrieved.`
271
+ throw new SolanaError(
272
+ SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_ADDRESS_LOOKUP_TABLE_INDEX_OUT_OF_RANGE,
273
+ {
274
+ highestKnownIndex: addresses.length - 1,
275
+ highestRequestedIndex: highestIndex,
276
+ lookupTableAddress: lookup.lookupTableAddress
277
+ }
272
278
  );
273
279
  }
274
280
  const readOnlyForLookup = lookup.readableIndices.map((r) => ({
@@ -291,7 +297,9 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
291
297
  function convertInstruction(instruction, accountMetas) {
292
298
  const programAddress = accountMetas[instruction.programAddressIndex]?.address;
293
299
  if (!programAddress) {
294
- throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
300
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_INSTRUCTION_PROGRAM_ADDRESS_NOT_FOUND, {
301
+ index: instruction.programAddressIndex
302
+ });
295
303
  }
296
304
  const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
297
305
  const { data } = instruction;
@@ -336,8 +344,9 @@ function convertSignatures(compiledTransaction) {
336
344
  function decompileTransaction(compiledTransaction, config) {
337
345
  const { compiledMessage } = compiledTransaction;
338
346
  const feePayer = compiledMessage.staticAccounts[0];
339
- if (!feePayer)
340
- throw new Error("No fee payer set in CompiledTransaction");
347
+ if (!feePayer) {
348
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_FEE_PAYER_MISSING);
349
+ }
341
350
  const accountMetas = getAccountMetas(compiledMessage);
342
351
  const accountLookupMetas = "addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups !== void 0 && compiledMessage.addressTableLookups.length > 0 ? getAddressLookupMetas(compiledMessage.addressTableLookups, config?.addressesByLookupTableAddress ?? {}) : [];
343
352
  const transactionMetas = [...accountMetas, ...accountLookupMetas];
@@ -377,13 +386,13 @@ function getAddressMapFromInstructions(feePayer, instructions) {
377
386
  if (isWritableRole(entry.role)) {
378
387
  switch (entry[TYPE]) {
379
388
  case 0 /* FEE_PAYER */:
380
- throw new Error(
381
- `This transaction includes an address (\`${instruction.programAddress}\`) which is both invoked and set as the fee payer. Program addresses may not pay fees.`
382
- );
389
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_CANNOT_PAY_FEES, {
390
+ programAddress: instruction.programAddress
391
+ });
383
392
  default:
384
- throw new Error(
385
- `This transaction includes an address (\`${instruction.programAddress}\`) which is both invoked and marked writable. Program addresses may not be writable.`
386
- );
393
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE, {
394
+ programAddress: instruction.programAddress
395
+ });
387
396
  }
388
397
  }
389
398
  if (entry[TYPE] === 2 /* STATIC */) {
@@ -448,8 +457,11 @@ function getAddressMapFromInstructions(feePayer, instructions) {
448
457
  addressesOfInvokedPrograms.has(account.address)
449
458
  ) {
450
459
  if (isWritableRole(accountMeta.role)) {
451
- throw new Error(
452
- `This transaction includes an address (\`${account.address}\`) which is both invoked and marked writable. Program addresses may not be writable.`
460
+ throw new SolanaError(
461
+ SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE,
462
+ {
463
+ programAddress: account.address
464
+ }
453
465
  );
454
466
  }
455
467
  if (entry.role !== nextRole) {
@@ -739,7 +751,9 @@ function getTransactionVersionEncoder() {
739
751
  return offset;
740
752
  }
741
753
  if (value < 0 || value > 127) {
742
- throw new Error(`Transaction version must be in the range [0, 127]. \`${value}\` given.`);
754
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__VERSION_NUMBER_OUT_OF_RANGE, {
755
+ actualVersion: value
756
+ });
743
757
  }
744
758
  bytes.set([value | VERSION_FLAG_MASK], offset);
745
759
  return offset + 1;
@@ -826,12 +840,15 @@ function getCompiledMessageEncoder() {
826
840
  });
827
841
  }
828
842
  function getCompiledMessageDecoder() {
829
- return mapDecoder(getStructDecoder(getPreludeStructDecoderTuple()), ({ addressTableLookups, ...restOfMessage }) => {
830
- if (restOfMessage.version === "legacy" || !addressTableLookups?.length) {
831
- return restOfMessage;
843
+ return mapDecoder(
844
+ getStructDecoder(getPreludeStructDecoderTuple()),
845
+ ({ addressTableLookups, ...restOfMessage }) => {
846
+ if (restOfMessage.version === "legacy" || !addressTableLookups?.length) {
847
+ return restOfMessage;
848
+ }
849
+ return { ...restOfMessage, addressTableLookups };
832
850
  }
833
- return { ...restOfMessage, addressTableLookups };
834
- });
851
+ );
835
852
  }
836
853
  function getCompiledMessageCodec() {
837
854
  return combineCodec(getCompiledMessageEncoder(), getCompiledMessageDecoder());
@@ -891,7 +908,7 @@ function getSignatureFromTransaction(transaction) {
891
908
  base58Decoder = getBase58Decoder();
892
909
  const signatureBytes = transaction.signatures[transaction.feePayer];
893
910
  if (!signatureBytes) {
894
- throw new SolanaError(SOLANA_ERROR__TRANSACTION_SIGNATURE_NOT_COMPUTABLE);
911
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING);
895
912
  }
896
913
  const transactionSignature = base58Decoder.decode(signatureBytes);
897
914
  return transactionSignature;
@@ -931,7 +948,7 @@ function assertTransactionIsFullySigned(transaction) {
931
948
  }
932
949
  });
933
950
  if (missingSigs.length > 0) {
934
- throw new SolanaError(SOLANA_ERROR__TRANSACTION_MISSING_SIGNATURES, {
951
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__SIGNATURES_MISSING, {
935
952
  addresses: missingSigs
936
953
  });
937
954
  }
@@ -941,6 +958,6 @@ function getBase64EncodedWireTransaction(transaction) {
941
958
  return getBase64Decoder().decode(wireTransactionBytes);
942
959
  }
943
960
 
944
- export { appendTransactionInstruction, assertIsDurableNonceTransaction, assertIsTransactionWithBlockhashLifetime, assertTransactionIsFullySigned, compileMessage, createTransaction, decompileTransaction, getBase64EncodedWireTransaction, getCompiledMessageCodec, getCompiledMessageDecoder, getCompiledMessageEncoder, getCompiledTransactionDecoder, getSignatureFromTransaction, getTransactionCodec, getTransactionDecoder, getTransactionEncoder, isAdvanceNonceAccountInstruction, partiallySignTransaction, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
961
+ export { appendTransactionInstruction, appendTransactionInstructions, assertIsDurableNonceTransaction, assertIsTransactionWithBlockhashLifetime, assertTransactionIsFullySigned, compileMessage, createTransaction, decompileTransaction, getBase64EncodedWireTransaction, getCompiledMessageCodec, getCompiledMessageDecoder, getCompiledMessageEncoder, getCompiledTransactionDecoder, getSignatureFromTransaction, getTransactionCodec, getTransactionDecoder, getTransactionEncoder, getUnsignedTransaction, isAdvanceNonceAccountInstruction, partiallySignTransaction, prependTransactionInstruction, prependTransactionInstructions, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
945
962
  //# sourceMappingURL=out.js.map
946
963
  //# sourceMappingURL=index.browser.js.map