@solana/transactions 2.0.0-experimental.098806e → 2.0.0-experimental.0af01ac

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,3 +1,4 @@
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_SIGNATURE_NOT_COMPUTABLE, SOLANA_ERROR__TRANSACTION_MISSING_SIGNATURES, 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__BLOCKHASH_BYTE_LENGTH_OUT_OF_RANGE, 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';
@@ -6,27 +7,25 @@ import { getStructDecoder, getArrayDecoder, getBytesDecoder, getStructEncoder, g
6
7
  import { getShortU16Decoder, getShortU16Encoder, getU8Encoder, getU8Decoder } from '@solana/codecs-numbers';
7
8
  import { signBytes } from '@solana/keys';
8
9
 
9
- // ../rpc-types/dist/index.browser.js
10
+ // src/blockhash.ts
10
11
  var base58Encoder;
11
12
  function assertIsBlockhash(putativeBlockhash) {
12
13
  if (!base58Encoder)
13
14
  base58Encoder = getBase58Encoder();
14
- try {
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 Error("Expected input string to decode to a byte array of length 32.");
21
- }
22
- const bytes = base58Encoder.encode(putativeBlockhash);
23
- const numBytes = bytes.byteLength;
24
- if (numBytes !== 32) {
25
- throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
26
- }
27
- } catch (e) {
28
- throw new Error(`\`${putativeBlockhash}\` is not a blockhash`, {
29
- 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__BLOCKHASH_BYTE_LENGTH_OUT_OF_RANGE, {
28
+ actualLength: numBytes
30
29
  });
31
30
  }
32
31
  }
@@ -59,7 +58,7 @@ function isTransactionWithBlockhashLifetime(transaction) {
59
58
  }
60
59
  function assertIsTransactionWithBlockhashLifetime(transaction) {
61
60
  if (!isTransactionWithBlockhashLifetime(transaction)) {
62
- throw new Error("Transaction does not have a blockhash lifetime");
61
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION_EXPECTED_BLOCKHASH_LIFETIME);
63
62
  }
64
63
  }
65
64
  function setTransactionLifetimeUsingBlockhash(blockhashLifetimeConstraint, transaction) {
@@ -85,8 +84,6 @@ function createTransaction({
85
84
  Object.freeze(out);
86
85
  return out;
87
86
  }
88
-
89
- // ../instructions/dist/index.browser.js
90
87
  var AccountRole = /* @__PURE__ */ ((AccountRole2) => {
91
88
  AccountRole2[AccountRole2["WRITABLE_SIGNER"] = /* 3 */
92
89
  3] = "WRITABLE_SIGNER";
@@ -108,13 +105,11 @@ function isWritableRole(role) {
108
105
  function mergeRoles(roleA, roleB) {
109
106
  return roleA | roleB;
110
107
  }
111
-
112
- // src/durable-nonce.ts
113
108
  var RECENT_BLOCKHASHES_SYSVAR_ADDRESS = "SysvarRecentB1ockHashes11111111111111111111";
114
109
  var SYSTEM_PROGRAM_ADDRESS = "11111111111111111111111111111111";
115
110
  function assertIsDurableNonceTransaction(transaction) {
116
111
  if (!isDurableNonceTransaction(transaction)) {
117
- throw new Error("Transaction is not a durable nonce transaction");
112
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION_EXPECTED_NONCE_LIFETIME);
118
113
  }
119
114
  }
120
115
  function createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress) {
@@ -200,17 +195,23 @@ function setTransactionFeePayer(feePayer, transaction) {
200
195
 
201
196
  // src/instructions.ts
202
197
  function appendTransactionInstruction(instruction, transaction) {
198
+ return appendTransactionInstructions([instruction], transaction);
199
+ }
200
+ function appendTransactionInstructions(instructions, transaction) {
203
201
  const out = {
204
202
  ...getUnsignedTransaction(transaction),
205
- instructions: [...transaction.instructions, instruction]
203
+ instructions: [...transaction.instructions, ...instructions]
206
204
  };
207
205
  Object.freeze(out);
208
206
  return out;
209
207
  }
210
208
  function prependTransactionInstruction(instruction, transaction) {
209
+ return prependTransactionInstructions([instruction], transaction);
210
+ }
211
+ function prependTransactionInstructions(instructions, transaction) {
211
212
  const out = {
212
213
  ...getUnsignedTransaction(transaction),
213
- instructions: [instruction, ...transaction.instructions]
214
+ instructions: [...instructions, ...transaction.instructions]
214
215
  };
215
216
  Object.freeze(out);
216
217
  return out;
@@ -257,8 +258,9 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
257
258
  const compiledAddressTableLookupAddresses = compiledAddressTableLookups.map((l) => l.lookupTableAddress);
258
259
  const missing = compiledAddressTableLookupAddresses.filter((a) => addressesByLookupTableAddress[a] === void 0);
259
260
  if (missing.length > 0) {
260
- const missingAddresses = missing.join(", ");
261
- 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
+ });
262
264
  }
263
265
  const readOnlyMetas = [];
264
266
  const writableMetas = [];
@@ -266,8 +268,13 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
266
268
  const addresses = addressesByLookupTableAddress[lookup.lookupTableAddress];
267
269
  const highestIndex = Math.max(...lookup.readableIndices, ...lookup.writableIndices);
268
270
  if (highestIndex >= addresses.length) {
269
- throw new Error(
270
- `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
+ }
271
278
  );
272
279
  }
273
280
  const readOnlyForLookup = lookup.readableIndices.map((r) => ({
@@ -290,7 +297,9 @@ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTab
290
297
  function convertInstruction(instruction, accountMetas) {
291
298
  const programAddress = accountMetas[instruction.programAddressIndex]?.address;
292
299
  if (!programAddress) {
293
- 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
+ });
294
303
  }
295
304
  const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
296
305
  const { data } = instruction;
@@ -335,8 +344,9 @@ function convertSignatures(compiledTransaction) {
335
344
  function decompileTransaction(compiledTransaction, config) {
336
345
  const { compiledMessage } = compiledTransaction;
337
346
  const feePayer = compiledMessage.staticAccounts[0];
338
- if (!feePayer)
339
- 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
+ }
340
350
  const accountMetas = getAccountMetas(compiledMessage);
341
351
  const accountLookupMetas = "addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups !== void 0 && compiledMessage.addressTableLookups.length > 0 ? getAddressLookupMetas(compiledMessage.addressTableLookups, config?.addressesByLookupTableAddress ?? {}) : [];
342
352
  const transactionMetas = [...accountMetas, ...accountLookupMetas];
@@ -376,13 +386,13 @@ function getAddressMapFromInstructions(feePayer, instructions) {
376
386
  if (isWritableRole(entry.role)) {
377
387
  switch (entry[TYPE]) {
378
388
  case 0 /* FEE_PAYER */:
379
- throw new Error(
380
- `This transaction includes an address (\`${instruction.programAddress}\`) which is both invoked and set as the fee payer. Program addresses may not pay fees.`
381
- );
389
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION_INVOKED_PROGRAMS_CANNOT_PAY_FEES, {
390
+ programAddress: instruction.programAddress
391
+ });
382
392
  default:
383
- throw new Error(
384
- `This transaction includes an address (\`${instruction.programAddress}\`) which is both invoked and marked writable. Program addresses may not be writable.`
385
- );
393
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION_INVOKED_PROGRAMS_MUST_NOT_BE_WRITABLE, {
394
+ programAddress: instruction.programAddress
395
+ });
386
396
  }
387
397
  }
388
398
  if (entry[TYPE] === 2 /* STATIC */) {
@@ -447,8 +457,11 @@ function getAddressMapFromInstructions(feePayer, instructions) {
447
457
  addressesOfInvokedPrograms.has(account.address)
448
458
  ) {
449
459
  if (isWritableRole(accountMeta.role)) {
450
- throw new Error(
451
- `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
+ }
452
465
  );
453
466
  }
454
467
  if (entry.role !== nextRole) {
@@ -738,7 +751,9 @@ function getTransactionVersionEncoder() {
738
751
  return offset;
739
752
  }
740
753
  if (value < 0 || value > 127) {
741
- 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
+ });
742
757
  }
743
758
  bytes.set([value | VERSION_FLAG_MASK], offset);
744
759
  return offset + 1;
@@ -825,12 +840,15 @@ function getCompiledMessageEncoder() {
825
840
  });
826
841
  }
827
842
  function getCompiledMessageDecoder() {
828
- return mapDecoder(getStructDecoder(getPreludeStructDecoderTuple()), ({ addressTableLookups, ...restOfMessage }) => {
829
- if (restOfMessage.version === "legacy" || !addressTableLookups?.length) {
830
- 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 };
831
850
  }
832
- return { ...restOfMessage, addressTableLookups };
833
- });
851
+ );
834
852
  }
835
853
  function getCompiledMessageCodec() {
836
854
  return combineCodec(getCompiledMessageEncoder(), getCompiledMessageDecoder());
@@ -890,9 +908,7 @@ function getSignatureFromTransaction(transaction) {
890
908
  base58Decoder = getBase58Decoder();
891
909
  const signatureBytes = transaction.signatures[transaction.feePayer];
892
910
  if (!signatureBytes) {
893
- throw new Error(
894
- "Could not determine this transaction's signature. Make sure that the transaction has been signed by its fee payer."
895
- );
911
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION_SIGNATURE_NOT_COMPUTABLE);
896
912
  }
897
913
  const transactionSignature = base58Decoder.decode(signatureBytes);
898
914
  return transactionSignature;
@@ -932,7 +948,9 @@ function assertTransactionIsFullySigned(transaction) {
932
948
  }
933
949
  });
934
950
  if (missingSigs.length > 0) {
935
- throw new Error("Transaction is missing signatures for addresses: " + missingSigs.join(", "));
951
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION_MISSING_SIGNATURES, {
952
+ addresses: missingSigs
953
+ });
936
954
  }
937
955
  }
938
956
  function getBase64EncodedWireTransaction(transaction) {
@@ -940,6 +958,6 @@ function getBase64EncodedWireTransaction(transaction) {
940
958
  return getBase64Decoder().decode(wireTransactionBytes);
941
959
  }
942
960
 
943
- 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 };
944
962
  //# sourceMappingURL=out.js.map
945
963
  //# sourceMappingURL=index.browser.js.map