@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,35 +1,33 @@
1
1
  'use strict';
2
2
 
3
+ var errors = require('@solana/errors');
3
4
  var codecsStrings = require('@solana/codecs-strings');
4
5
  var addresses = require('@solana/addresses');
5
6
  var functional = require('@solana/functional');
6
7
  var codecsCore = require('@solana/codecs-core');
7
8
  var codecsDataStructures = require('@solana/codecs-data-structures');
8
9
  var codecsNumbers = require('@solana/codecs-numbers');
9
- var errors = require('@solana/errors');
10
10
  var keys = require('@solana/keys');
11
11
 
12
- // ../rpc-types/dist/index.browser.js
12
+ // src/blockhash.ts
13
13
  var base58Encoder;
14
14
  function assertIsBlockhash(putativeBlockhash) {
15
15
  if (!base58Encoder)
16
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
17
+ if (
18
+ // Lowest value (32 bytes of zeroes)
19
+ putativeBlockhash.length < 32 || // Highest value (32 bytes of 255)
20
+ putativeBlockhash.length > 44
21
+ ) {
22
+ throw new errors.SolanaError(errors.SOLANA_ERROR__BLOCKHASH_STRING_LENGTH_OUT_OF_RANGE, {
23
+ actualLength: putativeBlockhash.length
24
+ });
25
+ }
26
+ const bytes = base58Encoder.encode(putativeBlockhash);
27
+ const numBytes = bytes.byteLength;
28
+ if (numBytes !== 32) {
29
+ throw new errors.SolanaError(errors.SOLANA_ERROR__INVALID_BLOCKHASH_BYTE_LENGTH, {
30
+ actualLength: numBytes
33
31
  });
34
32
  }
35
33
  }
@@ -62,7 +60,7 @@ function isTransactionWithBlockhashLifetime(transaction) {
62
60
  }
63
61
  function assertIsTransactionWithBlockhashLifetime(transaction) {
64
62
  if (!isTransactionWithBlockhashLifetime(transaction)) {
65
- throw new Error("Transaction does not have a blockhash lifetime");
63
+ throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION__EXPECTED_BLOCKHASH_LIFETIME);
66
64
  }
67
65
  }
68
66
  function setTransactionLifetimeUsingBlockhash(blockhashLifetimeConstraint, transaction) {
@@ -88,8 +86,6 @@ function createTransaction({
88
86
  Object.freeze(out);
89
87
  return out;
90
88
  }
91
-
92
- // ../instructions/dist/index.browser.js
93
89
  var AccountRole = /* @__PURE__ */ ((AccountRole2) => {
94
90
  AccountRole2[AccountRole2["WRITABLE_SIGNER"] = /* 3 */
95
91
  3] = "WRITABLE_SIGNER";
@@ -111,13 +107,11 @@ function isWritableRole(role) {
111
107
  function mergeRoles(roleA, roleB) {
112
108
  return roleA | roleB;
113
109
  }
114
-
115
- // src/durable-nonce.ts
116
110
  var RECENT_BLOCKHASHES_SYSVAR_ADDRESS = "SysvarRecentB1ockHashes11111111111111111111";
117
111
  var SYSTEM_PROGRAM_ADDRESS = "11111111111111111111111111111111";
118
112
  function assertIsDurableNonceTransaction(transaction) {
119
113
  if (!isDurableNonceTransaction(transaction)) {
120
- throw new Error("Transaction is not a durable nonce transaction");
114
+ throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION__EXPECTED_NONCE_LIFETIME);
121
115
  }
122
116
  }
123
117
  function createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress) {
@@ -266,8 +260,9 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
266
260
  const compiledAddressTableLookupAddresses = compiledAddressTableLookups.map((l) => l.lookupTableAddress);
267
261
  const missing = compiledAddressTableLookupAddresses.filter((a) => addressesByLookupTableAddress[a] === void 0);
268
262
  if (missing.length > 0) {
269
- const missingAddresses = missing.join(", ");
270
- throw new Error(`Addresses not provided for lookup tables: [${missingAddresses}]`);
263
+ throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_ADDRESS_LOOKUP_TABLE_CONTENTS_MISSING, {
264
+ lookupTableAddresses: missing
265
+ });
271
266
  }
272
267
  const readOnlyMetas = [];
273
268
  const writableMetas = [];
@@ -275,8 +270,13 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
275
270
  const addresses = addressesByLookupTableAddress[lookup.lookupTableAddress];
276
271
  const highestIndex = Math.max(...lookup.readableIndices, ...lookup.writableIndices);
277
272
  if (highestIndex >= addresses.length) {
278
- throw new Error(
279
- `Cannot look up index ${highestIndex} in lookup table [${lookup.lookupTableAddress}]. The lookup table may have been extended since the addresses provided were retrieved.`
273
+ throw new errors.SolanaError(
274
+ errors.SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_ADDRESS_LOOKUP_TABLE_INDEX_OUT_OF_RANGE,
275
+ {
276
+ highestKnownIndex: addresses.length - 1,
277
+ highestRequestedIndex: highestIndex,
278
+ lookupTableAddress: lookup.lookupTableAddress
279
+ }
280
280
  );
281
281
  }
282
282
  const readOnlyForLookup = lookup.readableIndices.map((r) => ({
@@ -299,7 +299,9 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
299
299
  function convertInstruction(instruction, accountMetas) {
300
300
  const programAddress = accountMetas[instruction.programAddressIndex]?.address;
301
301
  if (!programAddress) {
302
- throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
302
+ throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_INSTRUCTION_PROGRAM_ADDRESS_NOT_FOUND, {
303
+ index: instruction.programAddressIndex
304
+ });
303
305
  }
304
306
  const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
305
307
  const { data } = instruction;
@@ -344,8 +346,9 @@ function convertSignatures(compiledTransaction) {
344
346
  function decompileTransaction(compiledTransaction, config) {
345
347
  const { compiledMessage } = compiledTransaction;
346
348
  const feePayer = compiledMessage.staticAccounts[0];
347
- if (!feePayer)
348
- throw new Error("No fee payer set in CompiledTransaction");
349
+ if (!feePayer) {
350
+ throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_FEE_PAYER_MISSING);
351
+ }
349
352
  const accountMetas = getAccountMetas(compiledMessage);
350
353
  const accountLookupMetas = "addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups !== void 0 && compiledMessage.addressTableLookups.length > 0 ? getAddressLookupMetas(compiledMessage.addressTableLookups, config?.addressesByLookupTableAddress ?? {}) : [];
351
354
  const transactionMetas = [...accountMetas, ...accountLookupMetas];
@@ -385,13 +388,13 @@ function getAddressMapFromInstructions(feePayer, instructions) {
385
388
  if (isWritableRole(entry.role)) {
386
389
  switch (entry[TYPE]) {
387
390
  case 0 /* FEE_PAYER */:
388
- throw new Error(
389
- `This transaction includes an address (\`${instruction.programAddress}\`) which is both invoked and set as the fee payer. Program addresses may not pay fees.`
390
- );
391
+ throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_CANNOT_PAY_FEES, {
392
+ programAddress: instruction.programAddress
393
+ });
391
394
  default:
392
- throw new Error(
393
- `This transaction includes an address (\`${instruction.programAddress}\`) which is both invoked and marked writable. Program addresses may not be writable.`
394
- );
395
+ throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE, {
396
+ programAddress: instruction.programAddress
397
+ });
395
398
  }
396
399
  }
397
400
  if (entry[TYPE] === 2 /* STATIC */) {
@@ -456,8 +459,11 @@ function getAddressMapFromInstructions(feePayer, instructions) {
456
459
  addressesOfInvokedPrograms.has(account.address)
457
460
  ) {
458
461
  if (isWritableRole(accountMeta.role)) {
459
- throw new Error(
460
- `This transaction includes an address (\`${account.address}\`) which is both invoked and marked writable. Program addresses may not be writable.`
462
+ throw new errors.SolanaError(
463
+ errors.SOLANA_ERROR__TRANSACTION__INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE,
464
+ {
465
+ programAddress: account.address
466
+ }
461
467
  );
462
468
  }
463
469
  if (entry.role !== nextRole) {
@@ -747,7 +753,9 @@ function getTransactionVersionEncoder() {
747
753
  return offset;
748
754
  }
749
755
  if (value < 0 || value > 127) {
750
- throw new Error(`Transaction version must be in the range [0, 127]. \`${value}\` given.`);
756
+ throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION__VERSION_NUMBER_OUT_OF_RANGE, {
757
+ actualVersion: value
758
+ });
751
759
  }
752
760
  bytes.set([value | VERSION_FLAG_MASK], offset);
753
761
  return offset + 1;
@@ -834,12 +842,15 @@ function getCompiledMessageEncoder() {
834
842
  });
835
843
  }
836
844
  function getCompiledMessageDecoder() {
837
- return codecsCore.mapDecoder(codecsDataStructures.getStructDecoder(getPreludeStructDecoderTuple()), ({ addressTableLookups, ...restOfMessage }) => {
838
- if (restOfMessage.version === "legacy" || !addressTableLookups?.length) {
839
- return restOfMessage;
845
+ return codecsCore.mapDecoder(
846
+ codecsDataStructures.getStructDecoder(getPreludeStructDecoderTuple()),
847
+ ({ addressTableLookups, ...restOfMessage }) => {
848
+ if (restOfMessage.version === "legacy" || !addressTableLookups?.length) {
849
+ return restOfMessage;
850
+ }
851
+ return { ...restOfMessage, addressTableLookups };
840
852
  }
841
- return { ...restOfMessage, addressTableLookups };
842
- });
853
+ );
843
854
  }
844
855
  function getCompiledMessageCodec() {
845
856
  return codecsCore.combineCodec(getCompiledMessageEncoder(), getCompiledMessageDecoder());
@@ -899,7 +910,7 @@ function getSignatureFromTransaction(transaction) {
899
910
  base58Decoder = codecsStrings.getBase58Decoder();
900
911
  const signatureBytes = transaction.signatures[transaction.feePayer];
901
912
  if (!signatureBytes) {
902
- throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_SIGNATURE_NOT_COMPUTABLE);
913
+ throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING);
903
914
  }
904
915
  const transactionSignature = base58Decoder.decode(signatureBytes);
905
916
  return transactionSignature;
@@ -939,7 +950,7 @@ function assertTransactionIsFullySigned(transaction) {
939
950
  }
940
951
  });
941
952
  if (missingSigs.length > 0) {
942
- throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION_MISSING_SIGNATURES, {
953
+ throw new errors.SolanaError(errors.SOLANA_ERROR__TRANSACTION__SIGNATURES_MISSING, {
943
954
  addresses: missingSigs
944
955
  });
945
956
  }