@solana/compat 2.0.0-experimental.fc4e943 → 2.0.0-experimental.fcff844

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.
@@ -2,22 +2,185 @@ this.globalThis = this.globalThis || {};
2
2
  this.globalThis.solanaWeb3 = (function (exports) {
3
3
  'use strict';
4
4
 
5
+ // src/address.ts
6
+ function fromLegacyPublicKey(publicKey) {
7
+ return publicKey.toBase58();
8
+ }
9
+
10
+ // ../codecs-core/dist/index.browser.js
11
+ function getEncodedSize(value, encoder) {
12
+ return "fixedSize" in encoder ? encoder.fixedSize : encoder.getSizeFromValue(value);
13
+ }
14
+ function createEncoder(encoder) {
15
+ return Object.freeze({
16
+ ...encoder,
17
+ encode: (value) => {
18
+ const bytes = new Uint8Array(getEncodedSize(value, encoder));
19
+ encoder.write(value, bytes, 0);
20
+ return bytes;
21
+ }
22
+ });
23
+ }
24
+
25
+ // ../codecs-strings/dist/index.browser.js
26
+ function assertValidBaseString(alphabet4, testValue, givenValue = testValue) {
27
+ if (!testValue.match(new RegExp(`^[${alphabet4}]*$`))) {
28
+ throw new Error(`Expected a string of base ${alphabet4.length}, got [${givenValue}].`);
29
+ }
30
+ }
31
+ var getBaseXEncoder = (alphabet4) => {
32
+ return createEncoder({
33
+ getSizeFromValue: (value) => {
34
+ const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
35
+ if (tailChars === "")
36
+ return value.length;
37
+ const base10Number = getBigIntFromBaseX(tailChars, alphabet4);
38
+ return leadingZeroes.length + Math.ceil(base10Number.toString(16).length / 2);
39
+ },
40
+ write(value, bytes, offset) {
41
+ assertValidBaseString(alphabet4, value);
42
+ if (value === "")
43
+ return offset;
44
+ const [leadingZeroes, tailChars] = partitionLeadingZeroes(value, alphabet4[0]);
45
+ if (tailChars === "") {
46
+ bytes.set(new Uint8Array(leadingZeroes.length).fill(0), offset);
47
+ return offset + leadingZeroes.length;
48
+ }
49
+ let base10Number = getBigIntFromBaseX(tailChars, alphabet4);
50
+ const tailBytes = [];
51
+ while (base10Number > 0n) {
52
+ tailBytes.unshift(Number(base10Number % 256n));
53
+ base10Number /= 256n;
54
+ }
55
+ const bytesToAdd = [...Array(leadingZeroes.length).fill(0), ...tailBytes];
56
+ bytes.set(bytesToAdd, offset);
57
+ return offset + bytesToAdd.length;
58
+ }
59
+ });
60
+ };
61
+ function partitionLeadingZeroes(value, zeroCharacter) {
62
+ const leadingZeroIndex = [...value].findIndex((c) => c !== zeroCharacter);
63
+ return leadingZeroIndex === -1 ? [value, ""] : [value.slice(0, leadingZeroIndex), value.slice(leadingZeroIndex)];
64
+ }
65
+ function getBigIntFromBaseX(value, alphabet4) {
66
+ const base = BigInt(alphabet4.length);
67
+ return [...value].reduce((sum, char) => sum * base + BigInt(alphabet4.indexOf(char)), 0n);
68
+ }
69
+ var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
70
+ var getBase58Encoder = () => getBaseXEncoder(alphabet2);
71
+
72
+ // ../keys/dist/index.browser.js
73
+ function addPkcs8Header(bytes) {
74
+ return new Uint8Array([
75
+ /**
76
+ * PKCS#8 header
77
+ */
78
+ 48,
79
+ // ASN.1 sequence tag
80
+ 46,
81
+ // Length of sequence (46 more bytes)
82
+ 2,
83
+ // ASN.1 integer tag
84
+ 1,
85
+ // Length of integer
86
+ 0,
87
+ // Version number
88
+ 48,
89
+ // ASN.1 sequence tag
90
+ 5,
91
+ // Length of sequence
92
+ 6,
93
+ // ASN.1 object identifier tag
94
+ 3,
95
+ // Length of object identifier
96
+ // Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
97
+ 43,
98
+ // iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
99
+ 101,
100
+ // thawte(101)
101
+ // Ed25519 identifier
102
+ 112,
103
+ // id-Ed25519(112)
104
+ /**
105
+ * Private key payload
106
+ */
107
+ 4,
108
+ // ASN.1 octet string tag
109
+ 34,
110
+ // String length (34 more bytes)
111
+ // Private key bytes as octet string
112
+ 4,
113
+ // ASN.1 octet string tag
114
+ 32,
115
+ // String length (32 bytes)
116
+ ...bytes
117
+ ]);
118
+ }
119
+ async function createPrivateKeyFromBytes(bytes, extractable) {
120
+ if (bytes.byteLength !== 32) {
121
+ throw new Error("Private key bytes must be of length 32");
122
+ }
123
+ const privateKeyBytesPkcs8 = addPkcs8Header(bytes);
124
+ return await crypto.subtle.importKey("pkcs8", privateKeyBytesPkcs8, "Ed25519", extractable != null ? extractable : false, ["sign"]);
125
+ }
126
+
127
+ // src/keypair.ts
128
+ async function fromLegacyKeypair(keypair, extractable) {
129
+ const [publicKey, privateKey] = await Promise.all([
130
+ crypto.subtle.importKey("raw", keypair.publicKey.toBytes(), "Ed25519", true, ["verify"]),
131
+ createPrivateKeyFromBytes(keypair.secretKey.slice(0, 32), extractable)
132
+ ]);
133
+ return {
134
+ privateKey,
135
+ publicKey
136
+ };
137
+ }
138
+
139
+ // ../addresses/dist/index.browser.js
140
+ var memoizedBase58Encoder;
141
+ function getMemoizedBase58Encoder() {
142
+ if (!memoizedBase58Encoder)
143
+ memoizedBase58Encoder = getBase58Encoder();
144
+ return memoizedBase58Encoder;
145
+ }
146
+ function assertIsAddress(putativeAddress) {
147
+ try {
148
+ if (
149
+ // Lowest address (32 bytes of zeroes)
150
+ putativeAddress.length < 32 || // Highest address (32 bytes of 255)
151
+ putativeAddress.length > 44
152
+ ) {
153
+ throw new Error("Expected input string to decode to a byte array of length 32.");
154
+ }
155
+ const base58Encoder = getMemoizedBase58Encoder();
156
+ const bytes = base58Encoder.encode(putativeAddress);
157
+ const numBytes = bytes.byteLength;
158
+ if (numBytes !== 32) {
159
+ throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
160
+ }
161
+ } catch (e2) {
162
+ throw new Error(`\`${putativeAddress}\` is not a base-58 encoded address`, {
163
+ cause: e2
164
+ });
165
+ }
166
+ }
167
+
5
168
  // ../functional/dist/index.browser.js
6
169
  function pipe(init, ...fns) {
7
170
  return fns.reduce((acc, fn) => fn(acc), init);
8
171
  }
9
172
 
10
173
  // ../instructions/dist/index.browser.js
11
- var AccountRole = /* @__PURE__ */ ((AccountRole2) => {
12
- AccountRole2[AccountRole2["WRITABLE_SIGNER"] = /* 3 */
174
+ var AccountRole = /* @__PURE__ */ ((AccountRole22) => {
175
+ AccountRole22[AccountRole22["WRITABLE_SIGNER"] = /* 3 */
13
176
  3] = "WRITABLE_SIGNER";
14
- AccountRole2[AccountRole2["READONLY_SIGNER"] = /* 2 */
177
+ AccountRole22[AccountRole22["READONLY_SIGNER"] = /* 2 */
15
178
  2] = "READONLY_SIGNER";
16
- AccountRole2[AccountRole2["WRITABLE"] = /* 1 */
179
+ AccountRole22[AccountRole22["WRITABLE"] = /* 1 */
17
180
  1] = "WRITABLE";
18
- AccountRole2[AccountRole2["READONLY"] = /* 0 */
181
+ AccountRole22[AccountRole22["READONLY"] = /* 0 */
19
182
  0] = "READONLY";
20
- return AccountRole2;
183
+ return AccountRole22;
21
184
  })(AccountRole || {});
22
185
 
23
186
  // ../transactions/dist/index.browser.js
@@ -54,6 +217,90 @@ this.globalThis.solanaWeb3 = (function (exports) {
54
217
  Object.freeze(out);
55
218
  return out;
56
219
  }
220
+ var AccountRole2 = /* @__PURE__ */ ((AccountRole22) => {
221
+ AccountRole22[AccountRole22["WRITABLE_SIGNER"] = /* 3 */
222
+ 3] = "WRITABLE_SIGNER";
223
+ AccountRole22[AccountRole22["READONLY_SIGNER"] = /* 2 */
224
+ 2] = "READONLY_SIGNER";
225
+ AccountRole22[AccountRole22["WRITABLE"] = /* 1 */
226
+ 1] = "WRITABLE";
227
+ AccountRole22[AccountRole22["READONLY"] = /* 0 */
228
+ 0] = "READONLY";
229
+ return AccountRole22;
230
+ })(AccountRole2 || {});
231
+ function isSignerRole(role) {
232
+ return role >= 2;
233
+ }
234
+ var RECENT_BLOCKHASHES_SYSVAR_ADDRESS = "SysvarRecentB1ockHashes11111111111111111111";
235
+ var SYSTEM_PROGRAM_ADDRESS = "11111111111111111111111111111111";
236
+ function createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress) {
237
+ return {
238
+ accounts: [
239
+ { address: nonceAccountAddress, role: AccountRole2.WRITABLE },
240
+ {
241
+ address: RECENT_BLOCKHASHES_SYSVAR_ADDRESS,
242
+ role: AccountRole2.READONLY
243
+ },
244
+ { address: nonceAuthorityAddress, role: AccountRole2.READONLY_SIGNER }
245
+ ],
246
+ data: new Uint8Array([4, 0, 0, 0]),
247
+ programAddress: SYSTEM_PROGRAM_ADDRESS
248
+ };
249
+ }
250
+ function isAdvanceNonceAccountInstruction(instruction) {
251
+ var _a;
252
+ return instruction.programAddress === SYSTEM_PROGRAM_ADDRESS && // Test for `AdvanceNonceAccount` instruction data
253
+ instruction.data != null && isAdvanceNonceAccountInstructionData(instruction.data) && // Test for exactly 3 accounts
254
+ ((_a = instruction.accounts) == null ? void 0 : _a.length) === 3 && // First account is nonce account address
255
+ instruction.accounts[0].address != null && instruction.accounts[0].role === AccountRole2.WRITABLE && // Second account is recent blockhashes sysvar
256
+ instruction.accounts[1].address === RECENT_BLOCKHASHES_SYSVAR_ADDRESS && instruction.accounts[1].role === AccountRole2.READONLY && // Third account is nonce authority account
257
+ instruction.accounts[2].address != null && isSignerRole(instruction.accounts[2].role);
258
+ }
259
+ function isAdvanceNonceAccountInstructionData(data) {
260
+ return data.byteLength === 4 && data[0] === 4 && data[1] === 0 && data[2] === 0 && data[3] === 0;
261
+ }
262
+ function isDurableNonceTransaction(transaction) {
263
+ return "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.nonce === "string" && transaction.instructions[0] != null && isAdvanceNonceAccountInstruction(transaction.instructions[0]);
264
+ }
265
+ function isAdvanceNonceAccountInstructionForNonce(instruction, nonceAccountAddress, nonceAuthorityAddress) {
266
+ return instruction.accounts[0].address === nonceAccountAddress && instruction.accounts[2].address === nonceAuthorityAddress;
267
+ }
268
+ function setTransactionLifetimeUsingDurableNonce({
269
+ nonce,
270
+ nonceAccountAddress,
271
+ nonceAuthorityAddress
272
+ }, transaction) {
273
+ let newInstructions;
274
+ const firstInstruction = transaction.instructions[0];
275
+ if (firstInstruction && isAdvanceNonceAccountInstruction(firstInstruction)) {
276
+ if (isAdvanceNonceAccountInstructionForNonce(firstInstruction, nonceAccountAddress, nonceAuthorityAddress)) {
277
+ if (isDurableNonceTransaction(transaction) && transaction.lifetimeConstraint.nonce === nonce) {
278
+ return transaction;
279
+ } else {
280
+ newInstructions = [firstInstruction, ...transaction.instructions.slice(1)];
281
+ }
282
+ } else {
283
+ newInstructions = [
284
+ createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
285
+ ...transaction.instructions.slice(1)
286
+ ];
287
+ }
288
+ } else {
289
+ newInstructions = [
290
+ createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
291
+ ...transaction.instructions
292
+ ];
293
+ }
294
+ const out = {
295
+ ...getUnsignedTransaction(transaction),
296
+ instructions: newInstructions,
297
+ lifetimeConstraint: {
298
+ nonce
299
+ }
300
+ };
301
+ Object.freeze(out);
302
+ return out;
303
+ }
57
304
  function setTransactionFeePayer(feePayer, transaction) {
58
305
  if ("feePayer" in transaction && feePayer === transaction.feePayer) {
59
306
  return transaction;
@@ -121,7 +368,7 @@ this.globalThis.solanaWeb3 = (function (exports) {
121
368
  throw new Error("No fee payer set in VersionedTransaction");
122
369
  const blockhashLifetime = {
123
370
  blockhash: transaction.message.recentBlockhash,
124
- lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
371
+ lastValidBlockHeight: lastValidBlockHeight != null ? lastValidBlockHeight : 2n ** 64n - 1n
125
372
  // U64 MAX
126
373
  };
127
374
  const instructions = transaction.message.compiledInstructions.map(
@@ -138,8 +385,48 @@ this.globalThis.solanaWeb3 = (function (exports) {
138
385
  (tx) => transaction.signatures.length ? { ...tx, signatures } : tx
139
386
  );
140
387
  }
388
+ function fromVersionedTransactionWithDurableNonce(transaction) {
389
+ if (transaction.message.addressTableLookups.length > 0) {
390
+ throw new Error("Cannot convert transaction with addressTableLookups");
391
+ }
392
+ const accountKeys = transaction.message.getAccountKeys();
393
+ const feePayer = accountKeys.staticAccountKeys[0];
394
+ if (!feePayer)
395
+ throw new Error("No fee payer set in VersionedTransaction");
396
+ const instructions = transaction.message.compiledInstructions.map(
397
+ (instruction) => convertInstruction(transaction.message, accountKeys, instruction)
398
+ );
399
+ if (instructions.length === 0) {
400
+ throw new Error("transaction with no instructions cannot be durable nonce transaction");
401
+ }
402
+ if (!isAdvanceNonceAccountInstruction(instructions[0])) {
403
+ throw new Error("transaction first instruction is not advance nonce account instruction");
404
+ }
405
+ const nonceAccountAddress = instructions[0].accounts[0].address;
406
+ assertIsAddress(nonceAccountAddress);
407
+ const nonceAuthorityAddress = instructions[0].accounts[2].address;
408
+ assertIsAddress(nonceAuthorityAddress);
409
+ const durableNonceLifetime = {
410
+ nonce: transaction.message.recentBlockhash,
411
+ nonceAccountAddress,
412
+ nonceAuthorityAddress
413
+ };
414
+ const signatures = convertSignatures(transaction, accountKeys.staticAccountKeys);
415
+ return pipe(
416
+ createTransaction({ version: transaction.version }),
417
+ (tx) => setTransactionFeePayer(feePayer.toBase58(), tx),
418
+ (tx) => setTransactionLifetimeUsingDurableNonce(durableNonceLifetime, tx),
419
+ (tx) => instructions.slice(1).reduce((acc, instruction) => {
420
+ return appendTransactionInstruction(instruction, acc);
421
+ }, tx),
422
+ (tx) => transaction.signatures.length ? { ...tx, signatures } : tx
423
+ );
424
+ }
141
425
 
426
+ exports.fromLegacyKeypair = fromLegacyKeypair;
427
+ exports.fromLegacyPublicKey = fromLegacyPublicKey;
142
428
  exports.fromVersionedTransactionWithBlockhash = fromVersionedTransactionWithBlockhash;
429
+ exports.fromVersionedTransactionWithDurableNonce = fromVersionedTransactionWithDurableNonce;
143
430
 
144
431
  return exports;
145
432