@solana/compat 6.3.1 → 6.3.2-canary-20260313112147

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/compat",
3
- "version": "6.3.1",
3
+ "version": "6.3.2-canary-20260313112147",
4
4
  "description": "Helpers for converting from legacy web3js classes",
5
5
  "homepage": "https://www.solanakit.com/api#solanacompat",
6
6
  "exports": {
@@ -33,7 +33,8 @@
33
33
  "types": "./dist/types/index.d.ts",
34
34
  "type": "commonjs",
35
35
  "files": [
36
- "./dist/"
36
+ "./dist/",
37
+ "./src/"
37
38
  ],
38
39
  "sideEffects": false,
39
40
  "keywords": [
@@ -55,12 +56,12 @@
55
56
  "maintained node versions"
56
57
  ],
57
58
  "dependencies": {
58
- "@solana/addresses": "6.3.1",
59
- "@solana/codecs-core": "6.3.1",
60
- "@solana/errors": "6.3.1",
61
- "@solana/instructions": "6.3.1",
62
- "@solana/keys": "6.3.1",
63
- "@solana/transactions": "6.3.1"
59
+ "@solana/addresses": "6.3.2-canary-20260313112147",
60
+ "@solana/codecs-core": "6.3.2-canary-20260313112147",
61
+ "@solana/errors": "6.3.2-canary-20260313112147",
62
+ "@solana/keys": "6.3.2-canary-20260313112147",
63
+ "@solana/instructions": "6.3.2-canary-20260313112147",
64
+ "@solana/transactions": "6.3.2-canary-20260313112147"
64
65
  },
65
66
  "peerDependencies": {
66
67
  "typescript": "^5.0.0"
package/src/address.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { Address } from '@solana/addresses';
2
+ import { PublicKey } from '@solana/web3.js';
3
+
4
+ /**
5
+ * Converts a legacy [PublicKey](https://solana-foundation.github.io/solana-web3.js/classes/PublicKey.html)
6
+ * object to an {@link Address}.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { fromLegacyPublicKey } from '@solana/compat';
11
+ *
12
+ * const legacyPublicKey = new PublicKey('49XBVQsvSW44ULKL9qufS9YqQPbdcps1TQRijx4FQ9sH');
13
+ * const address = fromLegacyPublicKey(legacyPublicKey);
14
+ * ```
15
+ */
16
+ export function fromLegacyPublicKey<TAddress extends string>(publicKey: PublicKey): Address<TAddress> {
17
+ return publicKey.toBase58() as Address<TAddress>;
18
+ }
package/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * This package contains utilities for converting from legacy web3.js classes to the data
3
+ * structures in Kit. It can be used standalone, but it is also exported as part of Kit
4
+ * [`@solana/kit`](https://github.com/anza-xyz/kit/tree/main/packages/kit).
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ export * from './address';
9
+ export * from './instruction';
10
+ export * from './keypair';
11
+ export * from './transaction';
@@ -0,0 +1,40 @@
1
+ import { AccountRole, Instruction } from '@solana/instructions';
2
+ import { TransactionInstruction } from '@solana/web3.js';
3
+
4
+ import { fromLegacyPublicKey } from './address';
5
+
6
+ /**
7
+ * This can be used to convert a legacy [`TransactionInstruction`](https://solana-foundation.github.io/solana-web3.js/classes/TransactionInstruction.html)
8
+ * object to an {@link Instruction}.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { fromLegacyTransactionInstruction } from '@solana/compat';
13
+ *
14
+ * // Imagine a function that returns a legacy `TransactionInstruction`
15
+ * const legacyInstruction = getMyLegacyInstruction();
16
+ * const instruction = fromLegacyTransactionInstruction(legacyInstruction);
17
+ * ```
18
+ */
19
+ export function fromLegacyTransactionInstruction(legacyInstruction: TransactionInstruction): Instruction {
20
+ const data = legacyInstruction.data?.byteLength > 0 ? Uint8Array.from(legacyInstruction.data) : undefined;
21
+ const accounts = legacyInstruction.keys.map(accountMeta =>
22
+ Object.freeze({
23
+ address: fromLegacyPublicKey(accountMeta.pubkey),
24
+ role: determineRole(accountMeta.isSigner, accountMeta.isWritable),
25
+ }),
26
+ );
27
+ const programAddress = fromLegacyPublicKey(legacyInstruction.programId);
28
+ return Object.freeze({
29
+ ...(accounts.length ? { accounts: Object.freeze(accounts) } : null),
30
+ ...(data ? { data } : null),
31
+ programAddress,
32
+ });
33
+ }
34
+
35
+ function determineRole(isSigner: boolean, isWritable: boolean): AccountRole {
36
+ if (isSigner && isWritable) return AccountRole.WRITABLE_SIGNER;
37
+ if (isSigner) return AccountRole.READONLY_SIGNER;
38
+ if (isWritable) return AccountRole.WRITABLE;
39
+ return AccountRole.READONLY;
40
+ }
package/src/keypair.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { createKeyPairFromBytes } from '@solana/keys';
2
+ import { Keypair } from '@solana/web3.js';
3
+
4
+ /**
5
+ * Converts a legacy [Keypair](https://solana-foundation.github.io/solana-web3.js/classes/Keypair.html)
6
+ * object to a native Ed25519 {@link CryptoKeyPair} object.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { fromLegacyKeypair } from '@solana/compat';
11
+ *
12
+ * const legacyKeyPair = Keypair.generate();
13
+ * const { privateKey, publicKey } = await fromLegacyKeypair(legacyKeyPair);
14
+ * ```
15
+ */
16
+ export async function fromLegacyKeypair(keypair: Keypair, extractable?: boolean): Promise<CryptoKeyPair> {
17
+ const bytes = new Uint8Array(64);
18
+ bytes.set(keypair.secretKey);
19
+ bytes.set(keypair.publicKey.toBytes(), /* offset */ 32);
20
+ return await createKeyPairFromBytes(bytes, extractable);
21
+ }
@@ -0,0 +1,54 @@
1
+ import { ReadonlyUint8Array } from '@solana/codecs-core';
2
+ import { SOLANA_ERROR__TRANSACTION__MESSAGE_SIGNATURES_MISMATCH, SolanaError } from '@solana/errors';
3
+ import type { SignatureBytes } from '@solana/keys';
4
+ import { type SignaturesMap, Transaction, TransactionMessageBytes } from '@solana/transactions';
5
+ import type { PublicKey, VersionedTransaction } from '@solana/web3.js';
6
+
7
+ function convertSignatures(transaction: VersionedTransaction, staticAccountKeys: PublicKey[]): SignaturesMap {
8
+ return Object.fromEntries(
9
+ transaction.signatures.map((sig, index) => {
10
+ const address = staticAccountKeys[index];
11
+ if (sig.every(b => b === 0)) {
12
+ // all-0 signatures are stored as null
13
+ return [address, null];
14
+ } else {
15
+ return [address, sig as ReadonlyUint8Array as SignatureBytes];
16
+ }
17
+ }),
18
+ );
19
+ }
20
+
21
+ /**
22
+ * This can be used to convert a legacy [VersionedTransaction](https://solana-foundation.github.io/solana-web3.js/classes/VersionedTransaction.html)
23
+ * object to a {@link Transaction}.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * import { fromVersionedTransaction } from '@solana/compat';
28
+ *
29
+ * // Imagine a function that returns a legacy `VersionedTransaction`
30
+ * const legacyVersionedTransaction = getMyLegacyVersionedTransaction();
31
+ * const transaction = fromVersionedTransaction(legacyVersionedTransaction);
32
+ * ```
33
+ */
34
+ export function fromVersionedTransaction(transaction: VersionedTransaction): Transaction {
35
+ const { message } = transaction;
36
+ const staticAccountKeys = message.staticAccountKeys;
37
+
38
+ const { numRequiredSignatures } = message.header;
39
+ if (numRequiredSignatures !== transaction.signatures.length) {
40
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__MESSAGE_SIGNATURES_MISMATCH, {
41
+ numRequiredSignatures: transaction.message.header.numRequiredSignatures,
42
+ signaturesLength: transaction.signatures.length,
43
+ signerAddresses: staticAccountKeys.slice(0, numRequiredSignatures).map(p => p.toBase58()),
44
+ });
45
+ }
46
+
47
+ const messageBytes = message.serialize() as ReadonlyUint8Array as TransactionMessageBytes;
48
+ const signatures = convertSignatures(transaction, staticAccountKeys);
49
+
50
+ return {
51
+ messageBytes,
52
+ signatures: Object.freeze(signatures),
53
+ };
54
+ }