@solana/transactions 2.0.0-experimental.eb14acd → 2.0.0-experimental.eb951b0

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) {
@@ -264,8 +258,9 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
264
258
  const compiledAddressTableLookupAddresses = compiledAddressTableLookups.map((l) => l.lookupTableAddress);
265
259
  const missing = compiledAddressTableLookupAddresses.filter((a) => addressesByLookupTableAddress[a] === void 0);
266
260
  if (missing.length > 0) {
267
- const missingAddresses = missing.join(", ");
268
- 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
+ });
269
264
  }
270
265
  const readOnlyMetas = [];
271
266
  const writableMetas = [];
@@ -273,8 +268,13 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
273
268
  const addresses = addressesByLookupTableAddress[lookup.lookupTableAddress];
274
269
  const highestIndex = Math.max(...lookup.readableIndices, ...lookup.writableIndices);
275
270
  if (highestIndex >= addresses.length) {
276
- throw new Error(
277
- `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
+ }
278
278
  );
279
279
  }
280
280
  const readOnlyForLookup = lookup.readableIndices.map((r) => ({
@@ -297,7 +297,9 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
297
297
  function convertInstruction(instruction, accountMetas) {
298
298
  const programAddress = accountMetas[instruction.programAddressIndex]?.address;
299
299
  if (!programAddress) {
300
- 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
+ });
301
303
  }
302
304
  const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
303
305
  const { data } = instruction;
@@ -342,8 +344,9 @@ function convertSignatures(compiledTransaction) {
342
344
  function decompileTransaction(compiledTransaction, config) {
343
345
  const { compiledMessage } = compiledTransaction;
344
346
  const feePayer = compiledMessage.staticAccounts[0];
345
- if (!feePayer)
346
- 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
+ }
347
350
  const accountMetas = getAccountMetas(compiledMessage);
348
351
  const accountLookupMetas = "addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups !== void 0 && compiledMessage.addressTableLookups.length > 0 ? getAddressLookupMetas(compiledMessage.addressTableLookups, config?.addressesByLookupTableAddress ?? {}) : [];
349
352
  const transactionMetas = [...accountMetas, ...accountLookupMetas];
@@ -383,13 +386,13 @@ function getAddressMapFromInstructions(feePayer, instructions) {
383
386
  if (isWritableRole(entry.role)) {
384
387
  switch (entry[TYPE]) {
385
388
  case 0 /* FEE_PAYER */:
386
- throw new Error(
387
- `This transaction includes an address (\`${instruction.programAddress}\`) which is both invoked and set as the fee payer. Program addresses may not pay fees.`
388
- );
389
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_CANNOT_PAY_FEES, {
390
+ programAddress: instruction.programAddress
391
+ });
389
392
  default:
390
- throw new Error(
391
- `This transaction includes an address (\`${instruction.programAddress}\`) which is both invoked and marked writable. Program addresses may not be writable.`
392
- );
393
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE, {
394
+ programAddress: instruction.programAddress
395
+ });
393
396
  }
394
397
  }
395
398
  if (entry[TYPE] === 2 /* STATIC */) {
@@ -454,8 +457,11 @@ function getAddressMapFromInstructions(feePayer, instructions) {
454
457
  addressesOfInvokedPrograms.has(account.address)
455
458
  ) {
456
459
  if (isWritableRole(accountMeta.role)) {
457
- throw new Error(
458
- `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
+ }
459
465
  );
460
466
  }
461
467
  if (entry.role !== nextRole) {
@@ -745,7 +751,9 @@ function getTransactionVersionEncoder() {
745
751
  return offset;
746
752
  }
747
753
  if (value < 0 || value > 127) {
748
- 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
+ });
749
757
  }
750
758
  bytes.set([value | VERSION_FLAG_MASK], offset);
751
759
  return offset + 1;
@@ -832,12 +840,15 @@ function getCompiledMessageEncoder() {
832
840
  });
833
841
  }
834
842
  function getCompiledMessageDecoder() {
835
- return mapDecoder(getStructDecoder(getPreludeStructDecoderTuple()), ({ addressTableLookups, ...restOfMessage }) => {
836
- if (restOfMessage.version === "legacy" || !addressTableLookups?.length) {
837
- 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 };
838
850
  }
839
- return { ...restOfMessage, addressTableLookups };
840
- });
851
+ );
841
852
  }
842
853
  function getCompiledMessageCodec() {
843
854
  return combineCodec(getCompiledMessageEncoder(), getCompiledMessageDecoder());
@@ -897,7 +908,7 @@ function getSignatureFromTransaction(transaction) {
897
908
  base58Decoder = getBase58Decoder();
898
909
  const signatureBytes = transaction.signatures[transaction.feePayer];
899
910
  if (!signatureBytes) {
900
- throw new SolanaError(SOLANA_ERROR__TRANSACTION_SIGNATURE_NOT_COMPUTABLE);
911
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING);
901
912
  }
902
913
  const transactionSignature = base58Decoder.decode(signatureBytes);
903
914
  return transactionSignature;
@@ -937,7 +948,7 @@ function assertTransactionIsFullySigned(transaction) {
937
948
  }
938
949
  });
939
950
  if (missingSigs.length > 0) {
940
- throw new SolanaError(SOLANA_ERROR__TRANSACTION_MISSING_SIGNATURES, {
951
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__SIGNATURES_MISSING, {
941
952
  addresses: missingSigs
942
953
  });
943
954
  }