@solana/web3.js 1.90.0 → 1.90.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.
package/lib/index.cjs.js CHANGED
@@ -708,6 +708,31 @@ class CompiledKeys {
708
708
  }
709
709
  }
710
710
 
711
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
712
+
713
+ /**
714
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
715
+ */
716
+ function guardedShift(byteArray) {
717
+ if (byteArray.length === 0) {
718
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
719
+ }
720
+ return byteArray.shift();
721
+ }
722
+
723
+ /**
724
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
725
+ * the array.
726
+ */
727
+ function guardedSplice(byteArray, ...args) {
728
+ const [start] = args;
729
+ if (args.length === 2 // Implies that `deleteCount` was supplied
730
+ ? start + (args[1] ?? 0) > byteArray.length : start >= byteArray.length) {
731
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
732
+ }
733
+ return byteArray.splice(...args);
734
+ }
735
+
711
736
  /**
712
737
  * An instruction to execute by a program
713
738
  *
@@ -849,32 +874,28 @@ class Message {
849
874
  static from(buffer$1) {
850
875
  // Slice up wire data
851
876
  let byteArray = [...buffer$1];
852
- const numRequiredSignatures = byteArray.shift();
877
+ const numRequiredSignatures = guardedShift(byteArray);
853
878
  if (numRequiredSignatures !== (numRequiredSignatures & VERSION_PREFIX_MASK)) {
854
879
  throw new Error('Versioned messages must be deserialized with VersionedMessage.deserialize()');
855
880
  }
856
- const numReadonlySignedAccounts = byteArray.shift();
857
- const numReadonlyUnsignedAccounts = byteArray.shift();
881
+ const numReadonlySignedAccounts = guardedShift(byteArray);
882
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
858
883
  const accountCount = decodeLength(byteArray);
859
884
  let accountKeys = [];
860
885
  for (let i = 0; i < accountCount; i++) {
861
- const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
862
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
886
+ const account = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
863
887
  accountKeys.push(new PublicKey(buffer.Buffer.from(account)));
864
888
  }
865
- const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
866
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
889
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH);
867
890
  const instructionCount = decodeLength(byteArray);
868
891
  let instructions = [];
869
892
  for (let i = 0; i < instructionCount; i++) {
870
- const programIdIndex = byteArray.shift();
893
+ const programIdIndex = guardedShift(byteArray);
871
894
  const accountCount = decodeLength(byteArray);
872
- const accounts = byteArray.slice(0, accountCount);
873
- byteArray = byteArray.slice(accountCount);
895
+ const accounts = guardedSplice(byteArray, 0, accountCount);
874
896
  const dataLength = decodeLength(byteArray);
875
- const dataSlice = byteArray.slice(0, dataLength);
897
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
876
898
  const data = bs58__default.default.encode(buffer.Buffer.from(dataSlice));
877
- byteArray = byteArray.slice(dataLength);
878
899
  instructions.push({
879
900
  programIdIndex,
880
901
  accounts,
@@ -1079,30 +1100,30 @@ class MessageV0 {
1079
1100
  }
1080
1101
  static deserialize(serializedMessage) {
1081
1102
  let byteArray = [...serializedMessage];
1082
- const prefix = byteArray.shift();
1103
+ const prefix = guardedShift(byteArray);
1083
1104
  const maskedPrefix = prefix & VERSION_PREFIX_MASK;
1084
1105
  assert(prefix !== maskedPrefix, `Expected versioned message but received legacy message`);
1085
1106
  const version = maskedPrefix;
1086
1107
  assert(version === 0, `Expected versioned message with version 0 but found version ${version}`);
1087
1108
  const header = {
1088
- numRequiredSignatures: byteArray.shift(),
1089
- numReadonlySignedAccounts: byteArray.shift(),
1090
- numReadonlyUnsignedAccounts: byteArray.shift()
1109
+ numRequiredSignatures: guardedShift(byteArray),
1110
+ numReadonlySignedAccounts: guardedShift(byteArray),
1111
+ numReadonlyUnsignedAccounts: guardedShift(byteArray)
1091
1112
  };
1092
1113
  const staticAccountKeys = [];
1093
1114
  const staticAccountKeysLength = decodeLength(byteArray);
1094
1115
  for (let i = 0; i < staticAccountKeysLength; i++) {
1095
- staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1116
+ staticAccountKeys.push(new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH)));
1096
1117
  }
1097
- const recentBlockhash = bs58__default.default.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1118
+ const recentBlockhash = bs58__default.default.encode(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1098
1119
  const instructionCount = decodeLength(byteArray);
1099
1120
  const compiledInstructions = [];
1100
1121
  for (let i = 0; i < instructionCount; i++) {
1101
- const programIdIndex = byteArray.shift();
1122
+ const programIdIndex = guardedShift(byteArray);
1102
1123
  const accountKeyIndexesLength = decodeLength(byteArray);
1103
- const accountKeyIndexes = byteArray.splice(0, accountKeyIndexesLength);
1124
+ const accountKeyIndexes = guardedSplice(byteArray, 0, accountKeyIndexesLength);
1104
1125
  const dataLength = decodeLength(byteArray);
1105
- const data = new Uint8Array(byteArray.splice(0, dataLength));
1126
+ const data = new Uint8Array(guardedSplice(byteArray, 0, dataLength));
1106
1127
  compiledInstructions.push({
1107
1128
  programIdIndex,
1108
1129
  accountKeyIndexes,
@@ -1112,11 +1133,11 @@ class MessageV0 {
1112
1133
  const addressTableLookupsCount = decodeLength(byteArray);
1113
1134
  const addressTableLookups = [];
1114
1135
  for (let i = 0; i < addressTableLookupsCount; i++) {
1115
- const accountKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1136
+ const accountKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
1116
1137
  const writableIndexesLength = decodeLength(byteArray);
1117
- const writableIndexes = byteArray.splice(0, writableIndexesLength);
1138
+ const writableIndexes = guardedSplice(byteArray, 0, writableIndexesLength);
1118
1139
  const readonlyIndexesLength = decodeLength(byteArray);
1119
- const readonlyIndexes = byteArray.splice(0, readonlyIndexesLength);
1140
+ const readonlyIndexes = guardedSplice(byteArray, 0, readonlyIndexesLength);
1120
1141
  addressTableLookups.push({
1121
1142
  accountKey,
1122
1143
  writableIndexes,
@@ -1907,8 +1928,7 @@ class Transaction {
1907
1928
  const signatureCount = decodeLength(byteArray);
1908
1929
  let signatures = [];
1909
1930
  for (let i = 0; i < signatureCount; i++) {
1910
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1911
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
1931
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
1912
1932
  signatures.push(bs58__default.default.encode(buffer.Buffer.from(signature)));
1913
1933
  }
1914
1934
  return Transaction.populate(Message.from(byteArray), signatures);
@@ -2083,7 +2103,7 @@ class VersionedTransaction {
2083
2103
  const signatures = [];
2084
2104
  const signaturesLength = decodeLength(byteArray);
2085
2105
  for (let i = 0; i < signaturesLength; i++) {
2086
- signatures.push(new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)));
2106
+ signatures.push(new Uint8Array(guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES)));
2087
2107
  }
2088
2108
  const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
2089
2109
  return new VersionedTransaction(message, signatures);
@@ -5823,7 +5843,7 @@ const LogsNotificationResult = superstruct.type({
5823
5843
 
5824
5844
  /** @internal */
5825
5845
  const COMMON_HTTP_HEADERS = {
5826
- 'solana-client': `js/${"0.0.0-development" }`
5846
+ 'solana-client': `js/${"1.90.2" }`
5827
5847
  };
5828
5848
 
5829
5849
  /**
@@ -10133,12 +10153,14 @@ class StakeProgram {
10133
10153
  /**
10134
10154
  * Generate a Transaction that splits Stake tokens into another stake account
10135
10155
  */
10136
- static split(params) {
10156
+ static split(params,
10157
+ // Compute the cost of allocating the new stake account in lamports
10158
+ rentExemptReserve) {
10137
10159
  const transaction = new Transaction();
10138
10160
  transaction.add(SystemProgram.createAccount({
10139
10161
  fromPubkey: params.authorizedPubkey,
10140
10162
  newAccountPubkey: params.splitStakePubkey,
10141
- lamports: 0,
10163
+ lamports: rentExemptReserve,
10142
10164
  space: this.space,
10143
10165
  programId: this.programId
10144
10166
  }));
@@ -10149,7 +10171,9 @@ class StakeProgram {
10149
10171
  * Generate a Transaction that splits Stake tokens into another account
10150
10172
  * derived from a base public key and seed
10151
10173
  */
10152
- static splitWithSeed(params) {
10174
+ static splitWithSeed(params,
10175
+ // If this stake account is new, compute the cost of allocating it in lamports
10176
+ rentExemptReserve) {
10153
10177
  const {
10154
10178
  stakePubkey,
10155
10179
  authorizedPubkey,
@@ -10166,6 +10190,13 @@ class StakeProgram {
10166
10190
  space: this.space,
10167
10191
  programId: this.programId
10168
10192
  }));
10193
+ if (rentExemptReserve && rentExemptReserve > 0) {
10194
+ transaction.add(SystemProgram.transfer({
10195
+ fromPubkey: params.authorizedPubkey,
10196
+ toPubkey: splitStakePubkey,
10197
+ lamports: rentExemptReserve
10198
+ }));
10199
+ }
10169
10200
  return transaction.add(this.splitInstruction({
10170
10201
  stakePubkey,
10171
10202
  authorizedPubkey,
@@ -10296,8 +10327,8 @@ StakeProgram.programId = new PublicKey('Stake11111111111111111111111111111111111
10296
10327
  * Max space of a Stake account
10297
10328
  *
10298
10329
  * This is generated from the solana-stake-program StakeState struct as
10299
- * `StakeState::size_of()`:
10300
- * https://docs.rs/solana-stake-program/latest/solana_stake_program/stake_state/enum.StakeState.html
10330
+ * `StakeStateV2::size_of()`:
10331
+ * https://docs.rs/solana-stake-program/latest/solana_stake_program/stake_state/enum.StakeStateV2.html
10301
10332
  */
10302
10333
  StakeProgram.space = 200;
10303
10334
 
@@ -10772,10 +10803,8 @@ class ValidatorInfo {
10772
10803
  if (configKeyCount !== 2) return null;
10773
10804
  const configKeys = [];
10774
10805
  for (let i = 0; i < 2; i++) {
10775
- const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
10776
- byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
10777
- const isSigner = byteArray.slice(0, 1)[0] === 1;
10778
- byteArray = byteArray.slice(1);
10806
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBLIC_KEY_LENGTH));
10807
+ const isSigner = guardedShift(byteArray) === 1;
10779
10808
  configKeys.push({
10780
10809
  publicKey,
10781
10810
  isSigner