@dynamic-labs-wallet/node-svm 0.0.48

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/index.cjs.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.cjs.js ADDED
@@ -0,0 +1,303 @@
1
+ 'use strict';
2
+
3
+ var node = require('@dynamic-labs-wallet/node');
4
+ var web3_js = require('@solana/web3.js');
5
+
6
+ function _extends() {
7
+ _extends = Object.assign || function assign(target) {
8
+ for(var i = 1; i < arguments.length; i++){
9
+ var source = arguments[i];
10
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
11
+ }
12
+ return target;
13
+ };
14
+ return _extends.apply(this, arguments);
15
+ }
16
+
17
+ const ERROR_CREATE_WALLET_ACCOUNT = 'Error creating svm wallet account';
18
+
19
+ async function getBalance({ address, rpcUrl = node.SOLANA_RPC_URL }) {
20
+ const connection = new web3_js.Connection(rpcUrl != null ? rpcUrl : 'https://api.devnet.solana.com');
21
+ const balance = await connection.getBalance(new web3_js.PublicKey(address));
22
+ return balance;
23
+ }
24
+ async function createSolanaTransaction({ senderSolanaAddress, amount, to, rpcUrl = 'https://api.devnet.solana.com' }) {
25
+ const connection = new web3_js.Connection(rpcUrl != null ? rpcUrl : 'https://api.devnet.solana.com');
26
+ const balance = await getBalance({
27
+ address: senderSolanaAddress,
28
+ rpcUrl
29
+ });
30
+ if (balance < amount * 1e9) {
31
+ throw new Error('Insufficient balance');
32
+ }
33
+ const fromPubkey = new web3_js.PublicKey(senderSolanaAddress);
34
+ const transaction = new web3_js.Transaction().add(web3_js.SystemProgram.transfer({
35
+ fromPubkey: fromPubkey,
36
+ toPubkey: new web3_js.PublicKey(to),
37
+ lamports: amount * 1e9
38
+ }));
39
+ const { blockhash } = await connection.getLatestBlockhash();
40
+ transaction.recentBlockhash = blockhash;
41
+ transaction.feePayer = fromPubkey;
42
+ const serializedTransaction = transaction.serializeMessage();
43
+ return {
44
+ transaction,
45
+ serializedTransaction
46
+ };
47
+ }
48
+ const addSignatureToTransaction = ({ transaction, signature, signerPublicKey })=>{
49
+ transaction.addSignature(signerPublicKey, Buffer.from(signature));
50
+ return transaction;
51
+ };
52
+ async function sendTransaction({ signedTransaction, rpcUrl = 'https://api.devnet.solana.com' }) {
53
+ const connection = new web3_js.Connection(rpcUrl != null ? rpcUrl : 'https://api.devnet.solana.com');
54
+ const txid = await connection.sendRawTransaction(Buffer.from(signedTransaction));
55
+ return txid;
56
+ }
57
+
58
+ class DynamicSvmWalletClient extends node.DynamicWalletClient {
59
+ /**
60
+ * Creates a wallet account on the Solana chain
61
+ *
62
+ * @param thresholdSignatureScheme The threshold signature scheme to use
63
+ * @returns The account address, public key hex, raw public key, and client key shares
64
+ */ async createWalletAccount({ thresholdSignatureScheme, password = undefined, onError }) {
65
+ try {
66
+ const { rawPublicKey, externalServerKeyShares } = await this.keyGen({
67
+ chainName: this.chainName,
68
+ thresholdSignatureScheme,
69
+ onError,
70
+ onCeremonyComplete: (accountAddress, walletId)=>{
71
+ // update wallet map
72
+ this.walletMap[accountAddress] = _extends({}, this.walletMap[accountAddress] || {}, {
73
+ accountAddress,
74
+ walletId,
75
+ chainName: this.chainName,
76
+ thresholdSignatureScheme,
77
+ externalServerKeySharesBackupInfo: node.getExternalServerKeyShareBackupInfo()
78
+ });
79
+ }
80
+ });
81
+ if (!rawPublicKey || !(rawPublicKey instanceof Uint8Array)) {
82
+ throw new Error('Raw public key is not a Uint8Array');
83
+ }
84
+ if (!externalServerKeyShares) {
85
+ throw new Error('Error creating wallet account');
86
+ }
87
+ const { accountAddress } = await this.deriveAccountAddress(rawPublicKey);
88
+ void this.storeEncryptedBackupByWalletWithRetry({
89
+ accountAddress,
90
+ externalServerKeyShares,
91
+ password
92
+ });
93
+ return {
94
+ accountAddress,
95
+ rawPublicKey: rawPublicKey,
96
+ externalServerKeyShares
97
+ };
98
+ } catch (error) {
99
+ this.logger.error('Error in createWalletAccount:', error);
100
+ throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
101
+ }
102
+ }
103
+ // Function to properly derive account address
104
+ async deriveAccountAddress(rawPublicKey) {
105
+ const pubKey = new web3_js.PublicKey(rawPublicKey);
106
+ const fromKey = pubKey.toBase58();
107
+ const accountAddress = fromKey;
108
+ return {
109
+ accountAddress
110
+ };
111
+ }
112
+ /**
113
+ * This function takes a message and returns it after being signed with MPC
114
+ *
115
+ * @param message The message to sign (Uint8Array)
116
+ * @param accountAddress Solana address (base58 encoded)
117
+ * @param password The password for encrypted backup shares
118
+ */ async signMessage({ message, accountAddress, password = undefined }) {
119
+ await this.verifyPassword({
120
+ accountAddress,
121
+ password,
122
+ walletOperation: node.WalletOperation.SIGN_MESSAGE
123
+ });
124
+ if (!accountAddress) {
125
+ throw new Error('Account address is required');
126
+ }
127
+ try {
128
+ const signatureEd25519 = await this.sign({
129
+ message,
130
+ accountAddress: accountAddress,
131
+ chainName: this.chainName,
132
+ password
133
+ });
134
+ const base58Signature = new web3_js.PublicKey(signatureEd25519).toBase58();
135
+ return base58Signature;
136
+ } catch (error) {
137
+ this.logger.error('Error signing message:', error);
138
+ throw error;
139
+ }
140
+ }
141
+ async signTransaction({ senderAddress, transaction, password = undefined }) {
142
+ await this.verifyPassword({
143
+ accountAddress: senderAddress,
144
+ password,
145
+ walletOperation: node.WalletOperation.SIGN_TRANSACTION
146
+ });
147
+ try {
148
+ let messageToSign;
149
+ if (transaction instanceof web3_js.VersionedTransaction) {
150
+ // For versioned transactions, we need to sign the message directly
151
+ const messageBytes = transaction.message.serialize();
152
+ messageToSign = Buffer.from(messageBytes).toString('hex');
153
+ } else {
154
+ // For legacy transactions, serialize the message
155
+ const messageBytes = transaction.serializeMessage();
156
+ messageToSign = Buffer.from(messageBytes).toString('hex');
157
+ }
158
+ const signatureEd25519 = await this.sign({
159
+ message: messageToSign,
160
+ accountAddress: senderAddress,
161
+ chainName: this.chainName,
162
+ password
163
+ });
164
+ if (!signatureEd25519) {
165
+ throw new Error('Signature is undefined');
166
+ }
167
+ const senderPublicKey = new web3_js.PublicKey(senderAddress);
168
+ const signedTransaction = addSignatureToTransaction({
169
+ transaction,
170
+ signature: signatureEd25519,
171
+ signerPublicKey: senderPublicKey
172
+ });
173
+ return signedTransaction;
174
+ } catch (error) {
175
+ this.logger.error('Error in signTransaction:', error);
176
+ if (error instanceof Error) {
177
+ this.logger.error('Error details:', error);
178
+ }
179
+ throw error;
180
+ }
181
+ }
182
+ /**
183
+ * Exports the private key for a given account address
184
+ *
185
+ * @param accountAddress The account address to export the private key for
186
+ * @param password The password for encrypted backup shares
187
+ * @returns The private key
188
+ */ async exportPrivateKey({ accountAddress, password = undefined }) {
189
+ const { derivedPrivateKey } = await this.exportKey({
190
+ accountAddress,
191
+ chainName: this.chainName,
192
+ password
193
+ });
194
+ if (!derivedPrivateKey) {
195
+ throw new Error('Derived private key is undefined');
196
+ }
197
+ const encodedPrivateKey = new web3_js.PublicKey(derivedPrivateKey).toBase58();
198
+ return {
199
+ derivedPrivateKey: encodedPrivateKey
200
+ };
201
+ }
202
+ /**
203
+ * Exports the private key for a given account address
204
+ *
205
+ * @param keyShares The key shares to export the private key for
206
+ * @returns The private key
207
+ */ async offlineExportPrivateKey({ keyShares, derivationPath }) {
208
+ const { derivedPrivateKey } = await this.offlineExportKey({
209
+ chainName: this.chainName,
210
+ keyShares,
211
+ derivationPath
212
+ });
213
+ return {
214
+ derivedPrivateKey
215
+ };
216
+ }
217
+ /**
218
+ * Converts the private key to a hex string
219
+ *
220
+ * @param privateKey The private key to convert
221
+ * @returns The hex string
222
+ */ decodePrivateKeyForSolana(privateKey) {
223
+ const decoded = new web3_js.PublicKey(privateKey).toBase58();
224
+ const slicedBytes = decoded.slice(0, 32);
225
+ return Buffer.from(slicedBytes).toString('hex');
226
+ }
227
+ getPublicKeyFromPrivateKey(privateKey) {
228
+ const privateKeyBytes = new web3_js.PublicKey(privateKey).toBase58();
229
+ const keypair = web3_js.Keypair.fromSecretKey(Buffer.from(privateKeyBytes));
230
+ const publicKeyBase58 = keypair.publicKey.toBase58();
231
+ return publicKeyBase58;
232
+ }
233
+ encodePublicKey(publicKey) {
234
+ const pubKey = new web3_js.PublicKey(publicKey);
235
+ const fromKey = pubKey.toBase58();
236
+ return fromKey;
237
+ }
238
+ /**
239
+ * Imports the private key for a given account address
240
+ *
241
+ * @param privateKey The private key to import
242
+ * @param chainName The chain name to import the private key for
243
+ * @param thresholdSignatureScheme The threshold signature scheme to use
244
+ * @param password The password for encrypted backup shares
245
+ * @returns The account address, raw public key, and client key shares
246
+ */ async importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password = undefined, onError }) {
247
+ //get public key from private key
248
+ const publicKey = this.getPublicKeyFromPrivateKey(privateKey);
249
+ const formattedPrivateKey = await this.decodePrivateKeyForSolana(privateKey);
250
+ const { rawPublicKey, externalServerKeyShares } = await this.importRawPrivateKey({
251
+ chainName,
252
+ privateKey: formattedPrivateKey,
253
+ thresholdSignatureScheme,
254
+ onError,
255
+ onCeremonyComplete: (accountAddress, walletId)=>{
256
+ // update wallet map
257
+ this.walletMap[accountAddress] = _extends({}, this.walletMap[accountAddress] || {}, {
258
+ accountAddress,
259
+ walletId,
260
+ chainName: this.chainName,
261
+ thresholdSignatureScheme,
262
+ externalServerKeySharesBackupInfo: node.getExternalServerKeyShareBackupInfo()
263
+ });
264
+ }
265
+ });
266
+ if (!rawPublicKey || !externalServerKeyShares) {
267
+ throw new Error('Error creating wallet account');
268
+ }
269
+ const { accountAddress } = await this.deriveAccountAddress(rawPublicKey);
270
+ if (accountAddress !== publicKey) {
271
+ throw new Error(`Public key mismatch: derived address ${accountAddress} !== public key ${publicKey}`);
272
+ }
273
+ // Backup the new wallet without waiting for the promise to resolve
274
+ void this.storeEncryptedBackupByWalletWithRetry({
275
+ accountAddress,
276
+ externalServerKeyShares,
277
+ password
278
+ });
279
+ return {
280
+ accountAddress,
281
+ rawPublicKey: rawPublicKey,
282
+ externalServerKeyShares
283
+ };
284
+ }
285
+ async getSvmWallets() {
286
+ const wallets = await this.getWallets();
287
+ const svmWallets = wallets.filter((wallet)=>wallet.chainName === 'solana');
288
+ return svmWallets;
289
+ }
290
+ constructor({ environmentId, baseApiUrl, baseMPCRelayApiUrl }){
291
+ super({
292
+ environmentId,
293
+ baseApiUrl,
294
+ baseMPCRelayApiUrl
295
+ }), this.chainName = 'SOL';
296
+ }
297
+ }
298
+
299
+ exports.DynamicSvmWalletClient = DynamicSvmWalletClient;
300
+ exports.addSignatureToTransaction = addSignatureToTransaction;
301
+ exports.createSolanaTransaction = createSolanaTransaction;
302
+ exports.getBalance = getBalance;
303
+ exports.sendTransaction = sendTransaction;
package/index.esm.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.esm.js ADDED
@@ -0,0 +1,297 @@
1
+ import { SOLANA_RPC_URL, DynamicWalletClient, getExternalServerKeyShareBackupInfo, WalletOperation } from '@dynamic-labs-wallet/node';
2
+ import { Connection, PublicKey, Transaction, SystemProgram, VersionedTransaction, Keypair } from '@solana/web3.js';
3
+
4
+ function _extends() {
5
+ _extends = Object.assign || function assign(target) {
6
+ for(var i = 1; i < arguments.length; i++){
7
+ var source = arguments[i];
8
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
9
+ }
10
+ return target;
11
+ };
12
+ return _extends.apply(this, arguments);
13
+ }
14
+
15
+ const ERROR_CREATE_WALLET_ACCOUNT = 'Error creating svm wallet account';
16
+
17
+ async function getBalance({ address, rpcUrl = SOLANA_RPC_URL }) {
18
+ const connection = new Connection(rpcUrl != null ? rpcUrl : 'https://api.devnet.solana.com');
19
+ const balance = await connection.getBalance(new PublicKey(address));
20
+ return balance;
21
+ }
22
+ async function createSolanaTransaction({ senderSolanaAddress, amount, to, rpcUrl = 'https://api.devnet.solana.com' }) {
23
+ const connection = new Connection(rpcUrl != null ? rpcUrl : 'https://api.devnet.solana.com');
24
+ const balance = await getBalance({
25
+ address: senderSolanaAddress,
26
+ rpcUrl
27
+ });
28
+ if (balance < amount * 1e9) {
29
+ throw new Error('Insufficient balance');
30
+ }
31
+ const fromPubkey = new PublicKey(senderSolanaAddress);
32
+ const transaction = new Transaction().add(SystemProgram.transfer({
33
+ fromPubkey: fromPubkey,
34
+ toPubkey: new PublicKey(to),
35
+ lamports: amount * 1e9
36
+ }));
37
+ const { blockhash } = await connection.getLatestBlockhash();
38
+ transaction.recentBlockhash = blockhash;
39
+ transaction.feePayer = fromPubkey;
40
+ const serializedTransaction = transaction.serializeMessage();
41
+ return {
42
+ transaction,
43
+ serializedTransaction
44
+ };
45
+ }
46
+ const addSignatureToTransaction = ({ transaction, signature, signerPublicKey })=>{
47
+ transaction.addSignature(signerPublicKey, Buffer.from(signature));
48
+ return transaction;
49
+ };
50
+ async function sendTransaction({ signedTransaction, rpcUrl = 'https://api.devnet.solana.com' }) {
51
+ const connection = new Connection(rpcUrl != null ? rpcUrl : 'https://api.devnet.solana.com');
52
+ const txid = await connection.sendRawTransaction(Buffer.from(signedTransaction));
53
+ return txid;
54
+ }
55
+
56
+ class DynamicSvmWalletClient extends DynamicWalletClient {
57
+ /**
58
+ * Creates a wallet account on the Solana chain
59
+ *
60
+ * @param thresholdSignatureScheme The threshold signature scheme to use
61
+ * @returns The account address, public key hex, raw public key, and client key shares
62
+ */ async createWalletAccount({ thresholdSignatureScheme, password = undefined, onError }) {
63
+ try {
64
+ const { rawPublicKey, externalServerKeyShares } = await this.keyGen({
65
+ chainName: this.chainName,
66
+ thresholdSignatureScheme,
67
+ onError,
68
+ onCeremonyComplete: (accountAddress, walletId)=>{
69
+ // update wallet map
70
+ this.walletMap[accountAddress] = _extends({}, this.walletMap[accountAddress] || {}, {
71
+ accountAddress,
72
+ walletId,
73
+ chainName: this.chainName,
74
+ thresholdSignatureScheme,
75
+ externalServerKeySharesBackupInfo: getExternalServerKeyShareBackupInfo()
76
+ });
77
+ }
78
+ });
79
+ if (!rawPublicKey || !(rawPublicKey instanceof Uint8Array)) {
80
+ throw new Error('Raw public key is not a Uint8Array');
81
+ }
82
+ if (!externalServerKeyShares) {
83
+ throw new Error('Error creating wallet account');
84
+ }
85
+ const { accountAddress } = await this.deriveAccountAddress(rawPublicKey);
86
+ void this.storeEncryptedBackupByWalletWithRetry({
87
+ accountAddress,
88
+ externalServerKeyShares,
89
+ password
90
+ });
91
+ return {
92
+ accountAddress,
93
+ rawPublicKey: rawPublicKey,
94
+ externalServerKeyShares
95
+ };
96
+ } catch (error) {
97
+ this.logger.error('Error in createWalletAccount:', error);
98
+ throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
99
+ }
100
+ }
101
+ // Function to properly derive account address
102
+ async deriveAccountAddress(rawPublicKey) {
103
+ const pubKey = new PublicKey(rawPublicKey);
104
+ const fromKey = pubKey.toBase58();
105
+ const accountAddress = fromKey;
106
+ return {
107
+ accountAddress
108
+ };
109
+ }
110
+ /**
111
+ * This function takes a message and returns it after being signed with MPC
112
+ *
113
+ * @param message The message to sign (Uint8Array)
114
+ * @param accountAddress Solana address (base58 encoded)
115
+ * @param password The password for encrypted backup shares
116
+ */ async signMessage({ message, accountAddress, password = undefined }) {
117
+ await this.verifyPassword({
118
+ accountAddress,
119
+ password,
120
+ walletOperation: WalletOperation.SIGN_MESSAGE
121
+ });
122
+ if (!accountAddress) {
123
+ throw new Error('Account address is required');
124
+ }
125
+ try {
126
+ const signatureEd25519 = await this.sign({
127
+ message,
128
+ accountAddress: accountAddress,
129
+ chainName: this.chainName,
130
+ password
131
+ });
132
+ const base58Signature = new PublicKey(signatureEd25519).toBase58();
133
+ return base58Signature;
134
+ } catch (error) {
135
+ this.logger.error('Error signing message:', error);
136
+ throw error;
137
+ }
138
+ }
139
+ async signTransaction({ senderAddress, transaction, password = undefined }) {
140
+ await this.verifyPassword({
141
+ accountAddress: senderAddress,
142
+ password,
143
+ walletOperation: WalletOperation.SIGN_TRANSACTION
144
+ });
145
+ try {
146
+ let messageToSign;
147
+ if (transaction instanceof VersionedTransaction) {
148
+ // For versioned transactions, we need to sign the message directly
149
+ const messageBytes = transaction.message.serialize();
150
+ messageToSign = Buffer.from(messageBytes).toString('hex');
151
+ } else {
152
+ // For legacy transactions, serialize the message
153
+ const messageBytes = transaction.serializeMessage();
154
+ messageToSign = Buffer.from(messageBytes).toString('hex');
155
+ }
156
+ const signatureEd25519 = await this.sign({
157
+ message: messageToSign,
158
+ accountAddress: senderAddress,
159
+ chainName: this.chainName,
160
+ password
161
+ });
162
+ if (!signatureEd25519) {
163
+ throw new Error('Signature is undefined');
164
+ }
165
+ const senderPublicKey = new PublicKey(senderAddress);
166
+ const signedTransaction = addSignatureToTransaction({
167
+ transaction,
168
+ signature: signatureEd25519,
169
+ signerPublicKey: senderPublicKey
170
+ });
171
+ return signedTransaction;
172
+ } catch (error) {
173
+ this.logger.error('Error in signTransaction:', error);
174
+ if (error instanceof Error) {
175
+ this.logger.error('Error details:', error);
176
+ }
177
+ throw error;
178
+ }
179
+ }
180
+ /**
181
+ * Exports the private key for a given account address
182
+ *
183
+ * @param accountAddress The account address to export the private key for
184
+ * @param password The password for encrypted backup shares
185
+ * @returns The private key
186
+ */ async exportPrivateKey({ accountAddress, password = undefined }) {
187
+ const { derivedPrivateKey } = await this.exportKey({
188
+ accountAddress,
189
+ chainName: this.chainName,
190
+ password
191
+ });
192
+ if (!derivedPrivateKey) {
193
+ throw new Error('Derived private key is undefined');
194
+ }
195
+ const encodedPrivateKey = new PublicKey(derivedPrivateKey).toBase58();
196
+ return {
197
+ derivedPrivateKey: encodedPrivateKey
198
+ };
199
+ }
200
+ /**
201
+ * Exports the private key for a given account address
202
+ *
203
+ * @param keyShares The key shares to export the private key for
204
+ * @returns The private key
205
+ */ async offlineExportPrivateKey({ keyShares, derivationPath }) {
206
+ const { derivedPrivateKey } = await this.offlineExportKey({
207
+ chainName: this.chainName,
208
+ keyShares,
209
+ derivationPath
210
+ });
211
+ return {
212
+ derivedPrivateKey
213
+ };
214
+ }
215
+ /**
216
+ * Converts the private key to a hex string
217
+ *
218
+ * @param privateKey The private key to convert
219
+ * @returns The hex string
220
+ */ decodePrivateKeyForSolana(privateKey) {
221
+ const decoded = new PublicKey(privateKey).toBase58();
222
+ const slicedBytes = decoded.slice(0, 32);
223
+ return Buffer.from(slicedBytes).toString('hex');
224
+ }
225
+ getPublicKeyFromPrivateKey(privateKey) {
226
+ const privateKeyBytes = new PublicKey(privateKey).toBase58();
227
+ const keypair = Keypair.fromSecretKey(Buffer.from(privateKeyBytes));
228
+ const publicKeyBase58 = keypair.publicKey.toBase58();
229
+ return publicKeyBase58;
230
+ }
231
+ encodePublicKey(publicKey) {
232
+ const pubKey = new PublicKey(publicKey);
233
+ const fromKey = pubKey.toBase58();
234
+ return fromKey;
235
+ }
236
+ /**
237
+ * Imports the private key for a given account address
238
+ *
239
+ * @param privateKey The private key to import
240
+ * @param chainName The chain name to import the private key for
241
+ * @param thresholdSignatureScheme The threshold signature scheme to use
242
+ * @param password The password for encrypted backup shares
243
+ * @returns The account address, raw public key, and client key shares
244
+ */ async importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password = undefined, onError }) {
245
+ //get public key from private key
246
+ const publicKey = this.getPublicKeyFromPrivateKey(privateKey);
247
+ const formattedPrivateKey = await this.decodePrivateKeyForSolana(privateKey);
248
+ const { rawPublicKey, externalServerKeyShares } = await this.importRawPrivateKey({
249
+ chainName,
250
+ privateKey: formattedPrivateKey,
251
+ thresholdSignatureScheme,
252
+ onError,
253
+ onCeremonyComplete: (accountAddress, walletId)=>{
254
+ // update wallet map
255
+ this.walletMap[accountAddress] = _extends({}, this.walletMap[accountAddress] || {}, {
256
+ accountAddress,
257
+ walletId,
258
+ chainName: this.chainName,
259
+ thresholdSignatureScheme,
260
+ externalServerKeySharesBackupInfo: getExternalServerKeyShareBackupInfo()
261
+ });
262
+ }
263
+ });
264
+ if (!rawPublicKey || !externalServerKeyShares) {
265
+ throw new Error('Error creating wallet account');
266
+ }
267
+ const { accountAddress } = await this.deriveAccountAddress(rawPublicKey);
268
+ if (accountAddress !== publicKey) {
269
+ throw new Error(`Public key mismatch: derived address ${accountAddress} !== public key ${publicKey}`);
270
+ }
271
+ // Backup the new wallet without waiting for the promise to resolve
272
+ void this.storeEncryptedBackupByWalletWithRetry({
273
+ accountAddress,
274
+ externalServerKeyShares,
275
+ password
276
+ });
277
+ return {
278
+ accountAddress,
279
+ rawPublicKey: rawPublicKey,
280
+ externalServerKeyShares
281
+ };
282
+ }
283
+ async getSvmWallets() {
284
+ const wallets = await this.getWallets();
285
+ const svmWallets = wallets.filter((wallet)=>wallet.chainName === 'solana');
286
+ return svmWallets;
287
+ }
288
+ constructor({ environmentId, baseApiUrl, baseMPCRelayApiUrl }){
289
+ super({
290
+ environmentId,
291
+ baseApiUrl,
292
+ baseMPCRelayApiUrl
293
+ }), this.chainName = 'SOL';
294
+ }
295
+ }
296
+
297
+ export { DynamicSvmWalletClient, addSignatureToTransaction, createSolanaTransaction, getBalance, sendTransaction };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@dynamic-labs-wallet/node-svm",
3
+ "version": "0.0.48",
4
+ "license": "MIT",
5
+ "dependencies": {
6
+ "@dynamic-labs-wallet/node": "0.0.48",
7
+ "@solana/web3.js": "^1.98.0"
8
+ },
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "nx": {
13
+ "sourceRoot": "packages/node-svm/src",
14
+ "projectType": "library",
15
+ "name": "node-svm",
16
+ "targets": {
17
+ "build": {}
18
+ }
19
+ },
20
+ "type": "module",
21
+ "main": "./index.cjs.js",
22
+ "module": "./index.esm.js",
23
+ "types": "./index.esm.d.ts",
24
+ "exports": {
25
+ "./package.json": "./package.json",
26
+ ".": {
27
+ "types": "./index.esm.d.ts",
28
+ "import": "./index.esm.js",
29
+ "require": "./index.cjs.js",
30
+ "default": "./index.cjs.js"
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,103 @@
1
+ import { ServerKeyShare, DynamicWalletClient, Ed25519KeygenResult, ThresholdSignatureScheme } from '@dynamic-labs-wallet/node';
2
+ import { Transaction, VersionedTransaction } from '@solana/web3.js';
3
+ export declare class DynamicSvmWalletClient extends DynamicWalletClient {
4
+ readonly chainName = "SOL";
5
+ accountAddress?: string;
6
+ constructor({ environmentId, baseApiUrl, baseMPCRelayApiUrl, }: {
7
+ environmentId: string;
8
+ authToken: string;
9
+ baseApiUrl?: string;
10
+ baseMPCRelayApiUrl?: string;
11
+ });
12
+ /**
13
+ * Creates a wallet account on the Solana chain
14
+ *
15
+ * @param thresholdSignatureScheme The threshold signature scheme to use
16
+ * @returns The account address, public key hex, raw public key, and client key shares
17
+ */
18
+ createWalletAccount({ thresholdSignatureScheme, password, onError, }: {
19
+ thresholdSignatureScheme: ThresholdSignatureScheme;
20
+ password?: string;
21
+ onError?: (error: Error) => void;
22
+ }): Promise<{
23
+ accountAddress: string;
24
+ rawPublicKey: Uint8Array;
25
+ externalServerKeyShares: ServerKeyShare[];
26
+ }>;
27
+ deriveAccountAddress(rawPublicKey: Uint8Array): Promise<{
28
+ accountAddress: string;
29
+ }>;
30
+ /**
31
+ * This function takes a message and returns it after being signed with MPC
32
+ *
33
+ * @param message The message to sign (Uint8Array)
34
+ * @param accountAddress Solana address (base58 encoded)
35
+ * @param password The password for encrypted backup shares
36
+ */
37
+ signMessage({ message, accountAddress, password, }: {
38
+ message: string;
39
+ accountAddress: string;
40
+ password?: string;
41
+ }): Promise<string>;
42
+ signTransaction({ senderAddress, transaction, password, }: {
43
+ senderAddress: string;
44
+ transaction: VersionedTransaction | Transaction;
45
+ password?: string;
46
+ }): Promise<VersionedTransaction | Transaction>;
47
+ /**
48
+ * Exports the private key for a given account address
49
+ *
50
+ * @param accountAddress The account address to export the private key for
51
+ * @param password The password for encrypted backup shares
52
+ * @returns The private key
53
+ */
54
+ exportPrivateKey({ accountAddress, password, }: {
55
+ accountAddress: string;
56
+ password?: string;
57
+ }): Promise<{
58
+ derivedPrivateKey: string;
59
+ }>;
60
+ /**
61
+ * Exports the private key for a given account address
62
+ *
63
+ * @param keyShares The key shares to export the private key for
64
+ * @returns The private key
65
+ */
66
+ offlineExportPrivateKey({ keyShares, derivationPath, }: {
67
+ keyShares: Ed25519KeygenResult[];
68
+ derivationPath?: string;
69
+ }): Promise<{
70
+ derivedPrivateKey: string | undefined;
71
+ }>;
72
+ /**
73
+ * Converts the private key to a hex string
74
+ *
75
+ * @param privateKey The private key to convert
76
+ * @returns The hex string
77
+ */
78
+ decodePrivateKeyForSolana(privateKey: string): string;
79
+ getPublicKeyFromPrivateKey(privateKey: string): string;
80
+ encodePublicKey(publicKey: Uint8Array): string;
81
+ /**
82
+ * Imports the private key for a given account address
83
+ *
84
+ * @param privateKey The private key to import
85
+ * @param chainName The chain name to import the private key for
86
+ * @param thresholdSignatureScheme The threshold signature scheme to use
87
+ * @param password The password for encrypted backup shares
88
+ * @returns The account address, raw public key, and client key shares
89
+ */
90
+ importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password, onError, }: {
91
+ privateKey: string;
92
+ chainName: string;
93
+ thresholdSignatureScheme: ThresholdSignatureScheme;
94
+ password?: string;
95
+ onError?: (error: Error) => void;
96
+ }): Promise<{
97
+ accountAddress: string;
98
+ rawPublicKey: Uint8Array | undefined;
99
+ externalServerKeyShares: ServerKeyShare[];
100
+ }>;
101
+ getSvmWallets(): Promise<any>;
102
+ }
103
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client/client.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,mBAAmB,EACnB,wBAAwB,EAGzB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAEL,WAAW,EACX,oBAAoB,EAErB,MAAM,iBAAiB,CAAC;AAIzB,qBAAa,sBAAuB,SAAQ,mBAAmB;IAC7D,QAAQ,CAAC,SAAS,SAAS;IAC3B,cAAc,CAAC,EAAE,MAAM,CAAC;gBAEZ,EACV,aAAa,EACb,UAAU,EACV,kBAAkB,GACnB,EAAE;QACD,aAAa,EAAE,MAAM,CAAC;QACtB,SAAS,EAAE,MAAM,CAAC;QAClB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,kBAAkB,CAAC,EAAE,MAAM,CAAC;KAC7B;IAQD;;;;;OAKG;IACG,mBAAmB,CAAC,EACxB,wBAAwB,EACxB,QAAoB,EACpB,OAAO,GACR,EAAE;QACD,wBAAwB,EAAE,wBAAwB,CAAC;QACnD,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;KAClC,GAAG,OAAO,CAAC;QACV,cAAc,EAAE,MAAM,CAAC;QACvB,YAAY,EAAE,UAAU,CAAC;QACzB,uBAAuB,EAAE,cAAc,EAAE,CAAC;KAC3C,CAAC;IAkDI,oBAAoB,CAAC,YAAY,EAAE,UAAU;;;IASnD;;;;;;OAMG;IACG,WAAW,CAAC,EAChB,OAAO,EACP,cAAc,EACd,QAAoB,GACrB,EAAE;QACD,OAAO,EAAE,MAAM,CAAC;QAChB,cAAc,EAAE,MAAM,CAAC;QACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB;IA4BK,eAAe,CAAC,EACpB,aAAa,EACb,WAAW,EACX,QAAoB,GACrB,EAAE;QACD,aAAa,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,oBAAoB,GAAG,WAAW,CAAC;QAChD,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC,oBAAoB,GAAG,WAAW,CAAC;IAgD/C;;;;;;OAMG;IACG,gBAAgB,CAAC,EACrB,cAAc,EACd,QAAoB,GACrB,EAAE;QACD,cAAc,EAAE,MAAM,CAAC;QACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB;;;IAaD;;;;;OAKG;IACG,uBAAuB,CAAC,EAC5B,SAAS,EACT,cAAc,GACf,EAAE;QACD,SAAS,EAAE,mBAAmB,EAAE,CAAC;QACjC,cAAc,CAAC,EAAE,MAAM,CAAC;KACzB;;;IASD;;;;;OAKG;IACH,yBAAyB,CAAC,UAAU,EAAE,MAAM;IAM5C,0BAA0B,CAAC,UAAU,EAAE,MAAM;IAQ7C,eAAe,CAAC,SAAS,EAAE,UAAU,GAAG,MAAM;IAM9C;;;;;;;;OAQG;IACG,gBAAgB,CAAC,EACrB,UAAU,EACV,SAAS,EACT,wBAAwB,EACxB,QAAoB,EACpB,OAAO,GACR,EAAE;QACD,UAAU,EAAE,MAAM,CAAC;QACnB,SAAS,EAAE,MAAM,CAAC;QAClB,wBAAwB,EAAE,wBAAwB,CAAC;QACnD,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;KAClC,GAAG,OAAO,CAAC;QACV,cAAc,EAAE,MAAM,CAAC;QACvB,YAAY,EAAE,UAAU,GAAG,SAAS,CAAC;QACrC,uBAAuB,EAAE,cAAc,EAAE,CAAC;KAC3C,CAAC;IAsDI,aAAa;CAOpB"}
@@ -0,0 +1,2 @@
1
+ export declare const ERROR_CREATE_WALLET_ACCOUNT = "Error creating svm wallet account";
2
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/client/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,2BAA2B,sCAAsC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './client';
2
+ export * from './utils';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { PublicKey, Transaction, VersionedTransaction } from '@solana/web3.js';
2
+ export declare function getBalance({ address, rpcUrl, }: {
3
+ address: string;
4
+ rpcUrl?: string;
5
+ }): Promise<number>;
6
+ export declare function createSolanaTransaction({ senderSolanaAddress, amount, to, rpcUrl, }: {
7
+ senderSolanaAddress: string;
8
+ amount: number;
9
+ to: string;
10
+ rpcUrl?: string;
11
+ }): Promise<{
12
+ transaction: Transaction;
13
+ serializedTransaction: Buffer;
14
+ }>;
15
+ export declare const addSignatureToTransaction: ({ transaction, signature, signerPublicKey, }: {
16
+ transaction: Transaction | VersionedTransaction;
17
+ signature: Uint8Array;
18
+ signerPublicKey: PublicKey;
19
+ }) => VersionedTransaction | Transaction;
20
+ export declare function sendTransaction({ signedTransaction, rpcUrl, }: {
21
+ signedTransaction: Uint8Array;
22
+ rpcUrl?: string;
23
+ }): Promise<string>;
24
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/client/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,SAAS,EAET,WAAW,EACX,oBAAoB,EACrB,MAAM,iBAAiB,CAAC;AAEzB,wBAAsB,UAAU,CAAC,EAC/B,OAAO,EACP,MAAuB,GACxB,EAAE;IACD,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,mBAIA;AAED,wBAAsB,uBAAuB,CAAC,EAC5C,mBAAmB,EACnB,MAAM,EACN,EAAE,EACF,MAAwC,GACzC,EAAE;IACD,mBAAmB,EAAE,MAAM,CAAC;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;;;GAwBA;AAED,eAAO,MAAM,yBAAyB,iDAInC;IACD,WAAW,EAAE,WAAW,GAAG,oBAAoB,CAAC;IAChD,SAAS,EAAE,UAAU,CAAC;IACtB,eAAe,EAAE,SAAS,CAAC;CAC5B,KAAG,oBAAoB,GAAG,WAG1B,CAAC;AAEF,wBAAsB,eAAe,CAAC,EACpC,iBAAiB,EACjB,MAAwC,GACzC,EAAE;IACD,iBAAiB,EAAE,UAAU,CAAC;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,mBAMA"}
package/src/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './client/index';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../packages/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}