@solana/web3.js 1.59.0 → 1.59.2

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.
@@ -734,6 +734,36 @@ class CompiledKeys {
734
734
 
735
735
  }
736
736
 
737
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
738
+ /**
739
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
740
+ */
741
+
742
+ function guardedShift(byteArray) {
743
+ if (byteArray.length === 0) {
744
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
745
+ }
746
+
747
+ return byteArray.shift();
748
+ }
749
+ /**
750
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
751
+ * the array.
752
+ */
753
+
754
+ function guardedSplice(byteArray, ...args) {
755
+ var _args$;
756
+
757
+ const [start] = args;
758
+
759
+ if (args.length === 2 // Implies that `deleteCount` was supplied
760
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
761
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
762
+ }
763
+
764
+ return byteArray.splice(...args);
765
+ }
766
+
737
767
  /**
738
768
  * An instruction to execute by a program
739
769
  *
@@ -875,37 +905,33 @@ class Message {
875
905
  static from(buffer) {
876
906
  // Slice up wire data
877
907
  let byteArray = [...buffer];
878
- const numRequiredSignatures = byteArray.shift();
908
+ const numRequiredSignatures = guardedShift(byteArray);
879
909
 
880
910
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
881
911
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
882
912
  }
883
913
 
884
- const numReadonlySignedAccounts = byteArray.shift();
885
- const numReadonlyUnsignedAccounts = byteArray.shift();
914
+ const numReadonlySignedAccounts = guardedShift(byteArray);
915
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
886
916
  const accountCount = decodeLength(byteArray);
887
917
  let accountKeys = [];
888
918
 
889
919
  for (let i = 0; i < accountCount; i++) {
890
- const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
891
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
920
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
892
921
  accountKeys.push(new PublicKey(Buffer.from(account)));
893
922
  }
894
923
 
895
- const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
896
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
924
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
897
925
  const instructionCount = decodeLength(byteArray);
898
926
  let instructions = [];
899
927
 
900
928
  for (let i = 0; i < instructionCount; i++) {
901
- const programIdIndex = byteArray.shift();
929
+ const programIdIndex = guardedShift(byteArray);
902
930
  const accountCount = decodeLength(byteArray);
903
- const accounts = byteArray.slice(0, accountCount);
904
- byteArray = byteArray.slice(accountCount);
931
+ const accounts = guardedSplice(byteArray, 0, accountCount);
905
932
  const dataLength = decodeLength(byteArray);
906
- const dataSlice = byteArray.slice(0, dataLength);
933
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
907
934
  const data = bs58.encode(Buffer.from(dataSlice));
908
- byteArray = byteArray.slice(dataLength);
909
935
  instructions.push({
910
936
  programIdIndex,
911
937
  accounts,
@@ -1118,33 +1144,33 @@ class MessageV0 {
1118
1144
 
1119
1145
  static deserialize(serializedMessage) {
1120
1146
  let byteArray = [...serializedMessage];
1121
- const prefix = byteArray.shift();
1147
+ const prefix = guardedShift(byteArray);
1122
1148
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1123
1149
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1124
1150
  const version = maskedPrefix;
1125
1151
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1126
1152
  const header = {
1127
- numRequiredSignatures: byteArray.shift(),
1128
- numReadonlySignedAccounts: byteArray.shift(),
1129
- numReadonlyUnsignedAccounts: byteArray.shift()
1153
+ numRequiredSignatures: guardedShift(byteArray),
1154
+ numReadonlySignedAccounts: guardedShift(byteArray),
1155
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1130
1156
  };
1131
1157
  const staticAccountKeys = [];
1132
1158
  const staticAccountKeysLength = decodeLength(byteArray);
1133
1159
 
1134
1160
  for (let i = 0; i < staticAccountKeysLength; i++) {
1135
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1161
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1136
1162
  }
1137
1163
 
1138
- const recentBlockhash = bs58.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1164
+ const recentBlockhash = bs58.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1139
1165
  const instructionCount = decodeLength(byteArray);
1140
1166
  const compiledInstructions = [];
1141
1167
 
1142
1168
  for (let i = 0; i < instructionCount; i++) {
1143
- const programIdIndex = byteArray.shift();
1169
+ const programIdIndex = guardedShift(byteArray);
1144
1170
  const accountKeyIndexesLength = decodeLength(byteArray);
1145
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1171
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1146
1172
  const dataLength = decodeLength(byteArray);
1147
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1173
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1148
1174
  compiledInstructions.push({
1149
1175
  programIdIndex,
1150
1176
  accountKeyIndexes,
@@ -1156,11 +1182,11 @@ class MessageV0 {
1156
1182
  const addressTableLookups = [];
1157
1183
 
1158
1184
  for (let i = 0; i < addressTableLookupsCount; i++) {
1159
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1185
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1160
1186
  const writableIndexesLength = decodeLength(byteArray);
1161
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1187
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1162
1188
  const readonlyIndexesLength = decodeLength(byteArray);
1163
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1189
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1164
1190
  addressTableLookups.push({
1165
1191
  accountKey,
1166
1192
  writableIndexes,
@@ -1891,8 +1917,7 @@ class Transaction {
1891
1917
  let signatures = [];
1892
1918
 
1893
1919
  for (let i = 0; i < signatureCount; i++) {
1894
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1895
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
1920
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1896
1921
  signatures.push(bs58.encode(Buffer.from(signature)));
1897
1922
  }
1898
1923
 
@@ -1942,10 +1967,10 @@ class Transaction {
1942
1967
 
1943
1968
  class TransactionMessage {
1944
1969
  constructor(args) {
1945
- this.accountKeys = void 0;
1970
+ this.payerKey = void 0;
1946
1971
  this.instructions = void 0;
1947
1972
  this.recentBlockhash = void 0;
1948
- this.accountKeys = args.accountKeys;
1973
+ this.payerKey = args.payerKey;
1949
1974
  this.instructions = args.instructions;
1950
1975
  this.recentBlockhash = args.recentBlockhash;
1951
1976
  }
@@ -1966,6 +1991,12 @@ class TransactionMessage {
1966
1991
  const numWritableUnsignedAccounts = message.staticAccountKeys.length - numReadonlyUnsignedAccounts;
1967
1992
  assert(numWritableUnsignedAccounts >= 0, 'Message header is invalid');
1968
1993
  const accountKeys = message.getAccountKeys(args);
1994
+ const payerKey = accountKeys.get(0);
1995
+
1996
+ if (payerKey === undefined) {
1997
+ throw new Error('Failed to decompile message because no account keys were found');
1998
+ }
1999
+
1969
2000
  const instructions = [];
1970
2001
 
1971
2002
  for (const compiledIx of compiledInstructions) {
@@ -2011,35 +2042,23 @@ class TransactionMessage {
2011
2042
  }
2012
2043
 
2013
2044
  return new TransactionMessage({
2014
- accountKeys,
2045
+ payerKey,
2015
2046
  instructions,
2016
2047
  recentBlockhash
2017
2048
  });
2018
2049
  }
2019
2050
 
2020
2051
  compileToLegacyMessage() {
2021
- const payerKey = this.accountKeys.get(0);
2022
-
2023
- if (payerKey === undefined) {
2024
- throw new Error('Failed to compile message because no account keys were found');
2025
- }
2026
-
2027
2052
  return Message.compile({
2028
- payerKey,
2053
+ payerKey: this.payerKey,
2029
2054
  recentBlockhash: this.recentBlockhash,
2030
2055
  instructions: this.instructions
2031
2056
  });
2032
2057
  }
2033
2058
 
2034
2059
  compileToV0Message(addressLookupTableAccounts) {
2035
- const payerKey = this.accountKeys.get(0);
2036
-
2037
- if (payerKey === undefined) {
2038
- throw new Error('Failed to compile message because no account keys were found');
2039
- }
2040
-
2041
2060
  return MessageV0.compile({
2042
- payerKey,
2061
+ payerKey: this.payerKey,
2043
2062
  recentBlockhash: this.recentBlockhash,
2044
2063
  instructions: this.instructions,
2045
2064
  addressLookupTableAccounts
@@ -2092,7 +2111,7 @@ class VersionedTransaction {
2092
2111
  const signaturesLength = decodeLength(byteArray);
2093
2112
 
2094
2113
  for (let i = 0; i < signaturesLength; i++) {
2095
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2114
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2096
2115
  }
2097
2116
 
2098
2117
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
@@ -4491,7 +4510,7 @@ const LogsNotificationResult = type({
4491
4510
 
4492
4511
  /** @internal */
4493
4512
  const COMMON_HTTP_HEADERS = {
4494
- 'solana-client': `js/${(_process$env$npm_pack = "0.0.0-development") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4513
+ 'solana-client': `js/${(_process$env$npm_pack = "1.59.2") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4495
4514
  };
4496
4515
  /**
4497
4516
  * A connection to a fullnode JSON RPC endpoint
@@ -9405,10 +9424,8 @@ class ValidatorInfo {
9405
9424
  const configKeys = [];
9406
9425
 
9407
9426
  for (let i = 0; i < 2; i++) {
9408
- const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
9409
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
9410
- const isSigner = byteArray.slice(0, 1)[0] === 1;
9411
- byteArray = byteArray.slice(1);
9427
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
9428
+ const isSigner = guardedShift(byteArray) === 1;
9412
9429
  configKeys.push({
9413
9430
  publicKey,
9414
9431
  isSigner