@dynamic-labs-wallet/svm 0.0.0-preview.112
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 +1 -0
- package/index.cjs.js +287 -0
- package/index.esm.d.ts +1 -0
- package/index.esm.js +285 -0
- package/package.json +31 -0
- package/src/index.d.ts +2 -0
- package/src/index.d.ts.map +1 -0
- package/src/svm/constants.d.ts +2 -0
- package/src/svm/constants.d.ts.map +1 -0
- package/src/svm/index.d.ts +2 -0
- package/src/svm/index.d.ts.map +1 -0
- package/src/svm/svm.d.ts +101 -0
- package/src/svm/svm.d.ts.map +1 -0
- package/src/svm/utils.d.ts +24 -0
- package/src/svm/utils.d.ts.map +1 -0
package/index.cjs.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
package/index.cjs.js
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var browser = require('@dynamic-labs-wallet/browser');
|
|
4
|
+
var bs58 = require('bs58');
|
|
5
|
+
var web3_js = require('@solana/web3.js');
|
|
6
|
+
|
|
7
|
+
function _extends() {
|
|
8
|
+
_extends = Object.assign || function assign(target) {
|
|
9
|
+
for(var i = 1; i < arguments.length; i++){
|
|
10
|
+
var source = arguments[i];
|
|
11
|
+
for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
|
|
12
|
+
}
|
|
13
|
+
return target;
|
|
14
|
+
};
|
|
15
|
+
return _extends.apply(this, arguments);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const addSignatureToTransaction = ({ transaction, signature, signerPublicKey })=>{
|
|
19
|
+
transaction.addSignature(signerPublicKey, Buffer.from(signature));
|
|
20
|
+
return transaction;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const ERROR_CREATE_WALLET_ACCOUNT = 'Error creating svm wallet account';
|
|
24
|
+
|
|
25
|
+
class DynamicSvmWalletClient extends browser.DynamicWalletClient {
|
|
26
|
+
/**
|
|
27
|
+
* Creates a wallet account on the Solana chain
|
|
28
|
+
*
|
|
29
|
+
* @param thresholdSignatureScheme The threshold signature scheme to use
|
|
30
|
+
* @returns The account address, public key hex, raw public key, and client key shares
|
|
31
|
+
*/ async createWalletAccount({ thresholdSignatureScheme, password = undefined }) {
|
|
32
|
+
try {
|
|
33
|
+
const { rawPublicKey, clientKeyShares } = await this.keyGen({
|
|
34
|
+
chainName: this.chainName,
|
|
35
|
+
thresholdSignatureScheme,
|
|
36
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
37
|
+
// update wallet map
|
|
38
|
+
this.walletMap[accountAddress] = _extends({}, this.walletMap[accountAddress] || {}, {
|
|
39
|
+
accountAddress,
|
|
40
|
+
walletId,
|
|
41
|
+
chainName: this.chainName,
|
|
42
|
+
thresholdSignatureScheme,
|
|
43
|
+
clientKeySharesBackupInfo: browser.getClientKeyShareBackupInfo()
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
if (!rawPublicKey || !(rawPublicKey instanceof Uint8Array)) {
|
|
48
|
+
throw new Error('Raw public key is not a Uint8Array');
|
|
49
|
+
}
|
|
50
|
+
if (!clientKeyShares) {
|
|
51
|
+
throw new Error('Error creating wallet account');
|
|
52
|
+
}
|
|
53
|
+
const { accountAddress } = await this.deriveAccountAddress(rawPublicKey);
|
|
54
|
+
// Update client key shares in wallet map
|
|
55
|
+
// warning: this might result in race condition if `onCeremonyComplete` executes at the same time
|
|
56
|
+
// TODO: remove this once iframe handling for secret shares is implemented
|
|
57
|
+
await this.setClientKeySharesToLocalStorage({
|
|
58
|
+
accountAddress,
|
|
59
|
+
clientKeyShares,
|
|
60
|
+
overwriteOrMerge: 'overwrite'
|
|
61
|
+
});
|
|
62
|
+
// Backup the new wallet without waiting for the promise to resolve
|
|
63
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
64
|
+
accountAddress,
|
|
65
|
+
clientKeyShares,
|
|
66
|
+
password
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
accountAddress,
|
|
70
|
+
rawPublicKey: rawPublicKey,
|
|
71
|
+
clientKeyShares
|
|
72
|
+
};
|
|
73
|
+
} catch (error) {
|
|
74
|
+
this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
|
|
75
|
+
throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Function to properly derive account address
|
|
79
|
+
async deriveAccountAddress(rawPublicKey) {
|
|
80
|
+
const accountAddress = bs58.encode(rawPublicKey);
|
|
81
|
+
return {
|
|
82
|
+
accountAddress
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* This function takes a message and returns it after being signed with MPC
|
|
87
|
+
*
|
|
88
|
+
* @param message The message to sign (Uint8Array)
|
|
89
|
+
* @param accountAddress Solana address (base58 encoded)
|
|
90
|
+
* @param password The password for encrypted backup shares
|
|
91
|
+
*/ async signMessage({ message, accountAddress, password = undefined }) {
|
|
92
|
+
await this.verifyPassword({
|
|
93
|
+
accountAddress,
|
|
94
|
+
password,
|
|
95
|
+
walletOperation: browser.WalletOperation.SIGN_MESSAGE
|
|
96
|
+
});
|
|
97
|
+
if (!accountAddress) {
|
|
98
|
+
throw new Error('Account address is required');
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
const signatureEd25519 = await this.sign({
|
|
102
|
+
message,
|
|
103
|
+
accountAddress: accountAddress,
|
|
104
|
+
chainName: this.chainName,
|
|
105
|
+
password
|
|
106
|
+
});
|
|
107
|
+
const base58Signature = bs58.encode(signatureEd25519);
|
|
108
|
+
return base58Signature;
|
|
109
|
+
} catch (error) {
|
|
110
|
+
this.logger.error('Error signing message:', error);
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async signTransaction({ senderAddress, transaction, password = undefined }) {
|
|
115
|
+
await this.verifyPassword({
|
|
116
|
+
accountAddress: senderAddress,
|
|
117
|
+
password,
|
|
118
|
+
walletOperation: browser.WalletOperation.SIGN_TRANSACTION
|
|
119
|
+
});
|
|
120
|
+
try {
|
|
121
|
+
let messageToSign;
|
|
122
|
+
if (transaction instanceof web3_js.VersionedTransaction) {
|
|
123
|
+
// For versioned transactions, we need to sign the message directly
|
|
124
|
+
const messageBytes = transaction.message.serialize();
|
|
125
|
+
messageToSign = Buffer.from(messageBytes).toString('hex');
|
|
126
|
+
} else {
|
|
127
|
+
// For legacy transactions, serialize the message
|
|
128
|
+
const messageBytes = transaction.serializeMessage();
|
|
129
|
+
messageToSign = Buffer.from(messageBytes).toString('hex');
|
|
130
|
+
}
|
|
131
|
+
const signatureEd25519 = await this.sign({
|
|
132
|
+
message: messageToSign,
|
|
133
|
+
accountAddress: senderAddress,
|
|
134
|
+
chainName: this.chainName,
|
|
135
|
+
password
|
|
136
|
+
});
|
|
137
|
+
if (!signatureEd25519) {
|
|
138
|
+
throw new Error('Signature is undefined');
|
|
139
|
+
}
|
|
140
|
+
const senderPublicKey = new web3_js.PublicKey(senderAddress);
|
|
141
|
+
const signedTransaction = addSignatureToTransaction({
|
|
142
|
+
transaction,
|
|
143
|
+
signature: signatureEd25519,
|
|
144
|
+
signerPublicKey: senderPublicKey
|
|
145
|
+
});
|
|
146
|
+
return signedTransaction;
|
|
147
|
+
} catch (error) {
|
|
148
|
+
this.logger.error('Error in signTransaction:', error);
|
|
149
|
+
if (error instanceof Error) {
|
|
150
|
+
this.logger.error('Error details:', error);
|
|
151
|
+
}
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Exports the private key for a given account address
|
|
157
|
+
*
|
|
158
|
+
* @param accountAddress The account address to export the private key for
|
|
159
|
+
* @param password The password for encrypted backup shares
|
|
160
|
+
* @returns The private key
|
|
161
|
+
*/ async exportPrivateKey({ accountAddress, displayContainer, password = undefined }) {
|
|
162
|
+
await this.verifyPassword({
|
|
163
|
+
accountAddress,
|
|
164
|
+
password,
|
|
165
|
+
walletOperation: browser.WalletOperation.EXPORT_PRIVATE_KEY
|
|
166
|
+
});
|
|
167
|
+
const { derivedPrivateKey } = await this.exportKey({
|
|
168
|
+
accountAddress,
|
|
169
|
+
chainName: this.chainName,
|
|
170
|
+
password,
|
|
171
|
+
displayContainer
|
|
172
|
+
});
|
|
173
|
+
if (!derivedPrivateKey) {
|
|
174
|
+
throw new Error('Derived private key is undefined');
|
|
175
|
+
}
|
|
176
|
+
const encodedPrivateKey = bs58.encode(Buffer.from(derivedPrivateKey));
|
|
177
|
+
// Display the private key in the container via iframe
|
|
178
|
+
const { iframeDisplay } = await this.initializeIframeDisplayForContainer({
|
|
179
|
+
container: displayContainer
|
|
180
|
+
});
|
|
181
|
+
iframeDisplay.displayPrivateKey(accountAddress, encodedPrivateKey);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Exports the private key for a given account address
|
|
185
|
+
*
|
|
186
|
+
* @param keyShares The key shares to export the private key for
|
|
187
|
+
* @returns The private key
|
|
188
|
+
*/ async offlineExportPrivateKey({ keyShares, derivationPath }) {
|
|
189
|
+
const { derivedPrivateKey } = await this.offlineExportKey({
|
|
190
|
+
chainName: this.chainName,
|
|
191
|
+
keyShares,
|
|
192
|
+
derivationPath
|
|
193
|
+
});
|
|
194
|
+
return {
|
|
195
|
+
derivedPrivateKey
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Converts the private key to a hex string
|
|
200
|
+
*
|
|
201
|
+
* @param privateKey The private key to convert
|
|
202
|
+
* @returns The hex string
|
|
203
|
+
*/ decodePrivateKeyForSolana(privateKey) {
|
|
204
|
+
const decoded = bs58.decode(privateKey);
|
|
205
|
+
const slicedBytes = decoded.slice(0, 32);
|
|
206
|
+
return Buffer.from(slicedBytes).toString('hex');
|
|
207
|
+
}
|
|
208
|
+
getPublicKeyFromPrivateKey(privateKey) {
|
|
209
|
+
const privateKeyBytes = bs58.decode(privateKey);
|
|
210
|
+
const keypair = web3_js.Keypair.fromSecretKey(privateKeyBytes);
|
|
211
|
+
const publicKeyBase58 = keypair.publicKey.toBase58();
|
|
212
|
+
return publicKeyBase58;
|
|
213
|
+
}
|
|
214
|
+
encodePublicKey(publicKey) {
|
|
215
|
+
return bs58.encode(publicKey);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Imports the private key for a given account address
|
|
219
|
+
*
|
|
220
|
+
* @param privateKey The private key to import
|
|
221
|
+
* @param chainName The chain name to import the private key for
|
|
222
|
+
* @param thresholdSignatureScheme The threshold signature scheme to use
|
|
223
|
+
* @param password The password for encrypted backup shares
|
|
224
|
+
* @returns The account address, raw public key, and client key shares
|
|
225
|
+
*/ async importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password = undefined, onError }) {
|
|
226
|
+
//get public key from private key
|
|
227
|
+
const publicKey = this.getPublicKeyFromPrivateKey(privateKey);
|
|
228
|
+
const formattedPrivateKey = await this.decodePrivateKeyForSolana(privateKey);
|
|
229
|
+
const { rawPublicKey, clientKeyShares } = await this.importRawPrivateKey({
|
|
230
|
+
chainName,
|
|
231
|
+
privateKey: formattedPrivateKey,
|
|
232
|
+
thresholdSignatureScheme,
|
|
233
|
+
onError,
|
|
234
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
235
|
+
// update wallet map
|
|
236
|
+
this.walletMap[accountAddress] = _extends({}, this.walletMap[accountAddress] || {}, {
|
|
237
|
+
accountAddress,
|
|
238
|
+
walletId,
|
|
239
|
+
chainName: this.chainName,
|
|
240
|
+
thresholdSignatureScheme,
|
|
241
|
+
clientKeySharesBackupInfo: browser.getClientKeyShareBackupInfo()
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
if (!rawPublicKey || !clientKeyShares) {
|
|
246
|
+
throw new Error('Error creating wallet account');
|
|
247
|
+
}
|
|
248
|
+
const { accountAddress } = await this.deriveAccountAddress(rawPublicKey);
|
|
249
|
+
if (accountAddress !== publicKey) {
|
|
250
|
+
throw new Error(`Public key mismatch: derived address ${accountAddress} !== public key ${publicKey}`);
|
|
251
|
+
}
|
|
252
|
+
// Update client key shares in wallet map
|
|
253
|
+
// warning: this might result in race condition if `onCeremonyComplete` executes at the same time
|
|
254
|
+
// TODO: remove this once iframe handling for secret shares is implemented
|
|
255
|
+
await this.setClientKeySharesToLocalStorage({
|
|
256
|
+
accountAddress,
|
|
257
|
+
clientKeyShares,
|
|
258
|
+
overwriteOrMerge: 'overwrite'
|
|
259
|
+
});
|
|
260
|
+
// Backup the new wallet without waiting for the promise to resolve
|
|
261
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
262
|
+
accountAddress,
|
|
263
|
+
clientKeyShares,
|
|
264
|
+
password
|
|
265
|
+
});
|
|
266
|
+
return {
|
|
267
|
+
accountAddress,
|
|
268
|
+
rawPublicKey: rawPublicKey,
|
|
269
|
+
clientKeyShares
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
async getSvmWallets() {
|
|
273
|
+
const wallets = await this.getWallets();
|
|
274
|
+
const svmWallets = wallets.filter((wallet)=>wallet.chainName === 'solana');
|
|
275
|
+
return svmWallets;
|
|
276
|
+
}
|
|
277
|
+
constructor({ environmentId, authToken, baseApiUrl, baseMPCRelayApiUrl }){
|
|
278
|
+
super({
|
|
279
|
+
environmentId,
|
|
280
|
+
authToken,
|
|
281
|
+
baseApiUrl,
|
|
282
|
+
baseMPCRelayApiUrl
|
|
283
|
+
}), this.chainName = 'SOL';
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
exports.DynamicSvmWalletClient = DynamicSvmWalletClient;
|
package/index.esm.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { DynamicWalletClient, getClientKeyShareBackupInfo, WalletOperation } from '@dynamic-labs-wallet/browser';
|
|
2
|
+
import bs58 from 'bs58';
|
|
3
|
+
import { VersionedTransaction, PublicKey, Keypair } from '@solana/web3.js';
|
|
4
|
+
|
|
5
|
+
function _extends() {
|
|
6
|
+
_extends = Object.assign || function assign(target) {
|
|
7
|
+
for(var i = 1; i < arguments.length; i++){
|
|
8
|
+
var source = arguments[i];
|
|
9
|
+
for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
|
|
10
|
+
}
|
|
11
|
+
return target;
|
|
12
|
+
};
|
|
13
|
+
return _extends.apply(this, arguments);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const addSignatureToTransaction = ({ transaction, signature, signerPublicKey })=>{
|
|
17
|
+
transaction.addSignature(signerPublicKey, Buffer.from(signature));
|
|
18
|
+
return transaction;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const ERROR_CREATE_WALLET_ACCOUNT = 'Error creating svm wallet account';
|
|
22
|
+
|
|
23
|
+
class DynamicSvmWalletClient extends DynamicWalletClient {
|
|
24
|
+
/**
|
|
25
|
+
* Creates a wallet account on the Solana chain
|
|
26
|
+
*
|
|
27
|
+
* @param thresholdSignatureScheme The threshold signature scheme to use
|
|
28
|
+
* @returns The account address, public key hex, raw public key, and client key shares
|
|
29
|
+
*/ async createWalletAccount({ thresholdSignatureScheme, password = undefined }) {
|
|
30
|
+
try {
|
|
31
|
+
const { rawPublicKey, clientKeyShares } = await this.keyGen({
|
|
32
|
+
chainName: this.chainName,
|
|
33
|
+
thresholdSignatureScheme,
|
|
34
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
35
|
+
// update wallet map
|
|
36
|
+
this.walletMap[accountAddress] = _extends({}, this.walletMap[accountAddress] || {}, {
|
|
37
|
+
accountAddress,
|
|
38
|
+
walletId,
|
|
39
|
+
chainName: this.chainName,
|
|
40
|
+
thresholdSignatureScheme,
|
|
41
|
+
clientKeySharesBackupInfo: getClientKeyShareBackupInfo()
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
if (!rawPublicKey || !(rawPublicKey instanceof Uint8Array)) {
|
|
46
|
+
throw new Error('Raw public key is not a Uint8Array');
|
|
47
|
+
}
|
|
48
|
+
if (!clientKeyShares) {
|
|
49
|
+
throw new Error('Error creating wallet account');
|
|
50
|
+
}
|
|
51
|
+
const { accountAddress } = await this.deriveAccountAddress(rawPublicKey);
|
|
52
|
+
// Update client key shares in wallet map
|
|
53
|
+
// warning: this might result in race condition if `onCeremonyComplete` executes at the same time
|
|
54
|
+
// TODO: remove this once iframe handling for secret shares is implemented
|
|
55
|
+
await this.setClientKeySharesToLocalStorage({
|
|
56
|
+
accountAddress,
|
|
57
|
+
clientKeyShares,
|
|
58
|
+
overwriteOrMerge: 'overwrite'
|
|
59
|
+
});
|
|
60
|
+
// Backup the new wallet without waiting for the promise to resolve
|
|
61
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
62
|
+
accountAddress,
|
|
63
|
+
clientKeyShares,
|
|
64
|
+
password
|
|
65
|
+
});
|
|
66
|
+
return {
|
|
67
|
+
accountAddress,
|
|
68
|
+
rawPublicKey: rawPublicKey,
|
|
69
|
+
clientKeyShares
|
|
70
|
+
};
|
|
71
|
+
} catch (error) {
|
|
72
|
+
this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
|
|
73
|
+
throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// Function to properly derive account address
|
|
77
|
+
async deriveAccountAddress(rawPublicKey) {
|
|
78
|
+
const accountAddress = bs58.encode(rawPublicKey);
|
|
79
|
+
return {
|
|
80
|
+
accountAddress
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* This function takes a message and returns it after being signed with MPC
|
|
85
|
+
*
|
|
86
|
+
* @param message The message to sign (Uint8Array)
|
|
87
|
+
* @param accountAddress Solana address (base58 encoded)
|
|
88
|
+
* @param password The password for encrypted backup shares
|
|
89
|
+
*/ async signMessage({ message, accountAddress, password = undefined }) {
|
|
90
|
+
await this.verifyPassword({
|
|
91
|
+
accountAddress,
|
|
92
|
+
password,
|
|
93
|
+
walletOperation: WalletOperation.SIGN_MESSAGE
|
|
94
|
+
});
|
|
95
|
+
if (!accountAddress) {
|
|
96
|
+
throw new Error('Account address is required');
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
const signatureEd25519 = await this.sign({
|
|
100
|
+
message,
|
|
101
|
+
accountAddress: accountAddress,
|
|
102
|
+
chainName: this.chainName,
|
|
103
|
+
password
|
|
104
|
+
});
|
|
105
|
+
const base58Signature = bs58.encode(signatureEd25519);
|
|
106
|
+
return base58Signature;
|
|
107
|
+
} catch (error) {
|
|
108
|
+
this.logger.error('Error signing message:', error);
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async signTransaction({ senderAddress, transaction, password = undefined }) {
|
|
113
|
+
await this.verifyPassword({
|
|
114
|
+
accountAddress: senderAddress,
|
|
115
|
+
password,
|
|
116
|
+
walletOperation: WalletOperation.SIGN_TRANSACTION
|
|
117
|
+
});
|
|
118
|
+
try {
|
|
119
|
+
let messageToSign;
|
|
120
|
+
if (transaction instanceof VersionedTransaction) {
|
|
121
|
+
// For versioned transactions, we need to sign the message directly
|
|
122
|
+
const messageBytes = transaction.message.serialize();
|
|
123
|
+
messageToSign = Buffer.from(messageBytes).toString('hex');
|
|
124
|
+
} else {
|
|
125
|
+
// For legacy transactions, serialize the message
|
|
126
|
+
const messageBytes = transaction.serializeMessage();
|
|
127
|
+
messageToSign = Buffer.from(messageBytes).toString('hex');
|
|
128
|
+
}
|
|
129
|
+
const signatureEd25519 = await this.sign({
|
|
130
|
+
message: messageToSign,
|
|
131
|
+
accountAddress: senderAddress,
|
|
132
|
+
chainName: this.chainName,
|
|
133
|
+
password
|
|
134
|
+
});
|
|
135
|
+
if (!signatureEd25519) {
|
|
136
|
+
throw new Error('Signature is undefined');
|
|
137
|
+
}
|
|
138
|
+
const senderPublicKey = new PublicKey(senderAddress);
|
|
139
|
+
const signedTransaction = addSignatureToTransaction({
|
|
140
|
+
transaction,
|
|
141
|
+
signature: signatureEd25519,
|
|
142
|
+
signerPublicKey: senderPublicKey
|
|
143
|
+
});
|
|
144
|
+
return signedTransaction;
|
|
145
|
+
} catch (error) {
|
|
146
|
+
this.logger.error('Error in signTransaction:', error);
|
|
147
|
+
if (error instanceof Error) {
|
|
148
|
+
this.logger.error('Error details:', error);
|
|
149
|
+
}
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Exports the private key for a given account address
|
|
155
|
+
*
|
|
156
|
+
* @param accountAddress The account address to export the private key for
|
|
157
|
+
* @param password The password for encrypted backup shares
|
|
158
|
+
* @returns The private key
|
|
159
|
+
*/ async exportPrivateKey({ accountAddress, displayContainer, password = undefined }) {
|
|
160
|
+
await this.verifyPassword({
|
|
161
|
+
accountAddress,
|
|
162
|
+
password,
|
|
163
|
+
walletOperation: WalletOperation.EXPORT_PRIVATE_KEY
|
|
164
|
+
});
|
|
165
|
+
const { derivedPrivateKey } = await this.exportKey({
|
|
166
|
+
accountAddress,
|
|
167
|
+
chainName: this.chainName,
|
|
168
|
+
password,
|
|
169
|
+
displayContainer
|
|
170
|
+
});
|
|
171
|
+
if (!derivedPrivateKey) {
|
|
172
|
+
throw new Error('Derived private key is undefined');
|
|
173
|
+
}
|
|
174
|
+
const encodedPrivateKey = bs58.encode(Buffer.from(derivedPrivateKey));
|
|
175
|
+
// Display the private key in the container via iframe
|
|
176
|
+
const { iframeDisplay } = await this.initializeIframeDisplayForContainer({
|
|
177
|
+
container: displayContainer
|
|
178
|
+
});
|
|
179
|
+
iframeDisplay.displayPrivateKey(accountAddress, encodedPrivateKey);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Exports the private key for a given account address
|
|
183
|
+
*
|
|
184
|
+
* @param keyShares The key shares to export the private key for
|
|
185
|
+
* @returns The private key
|
|
186
|
+
*/ async offlineExportPrivateKey({ keyShares, derivationPath }) {
|
|
187
|
+
const { derivedPrivateKey } = await this.offlineExportKey({
|
|
188
|
+
chainName: this.chainName,
|
|
189
|
+
keyShares,
|
|
190
|
+
derivationPath
|
|
191
|
+
});
|
|
192
|
+
return {
|
|
193
|
+
derivedPrivateKey
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Converts the private key to a hex string
|
|
198
|
+
*
|
|
199
|
+
* @param privateKey The private key to convert
|
|
200
|
+
* @returns The hex string
|
|
201
|
+
*/ decodePrivateKeyForSolana(privateKey) {
|
|
202
|
+
const decoded = bs58.decode(privateKey);
|
|
203
|
+
const slicedBytes = decoded.slice(0, 32);
|
|
204
|
+
return Buffer.from(slicedBytes).toString('hex');
|
|
205
|
+
}
|
|
206
|
+
getPublicKeyFromPrivateKey(privateKey) {
|
|
207
|
+
const privateKeyBytes = bs58.decode(privateKey);
|
|
208
|
+
const keypair = Keypair.fromSecretKey(privateKeyBytes);
|
|
209
|
+
const publicKeyBase58 = keypair.publicKey.toBase58();
|
|
210
|
+
return publicKeyBase58;
|
|
211
|
+
}
|
|
212
|
+
encodePublicKey(publicKey) {
|
|
213
|
+
return bs58.encode(publicKey);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Imports the private key for a given account address
|
|
217
|
+
*
|
|
218
|
+
* @param privateKey The private key to import
|
|
219
|
+
* @param chainName The chain name to import the private key for
|
|
220
|
+
* @param thresholdSignatureScheme The threshold signature scheme to use
|
|
221
|
+
* @param password The password for encrypted backup shares
|
|
222
|
+
* @returns The account address, raw public key, and client key shares
|
|
223
|
+
*/ async importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password = undefined, onError }) {
|
|
224
|
+
//get public key from private key
|
|
225
|
+
const publicKey = this.getPublicKeyFromPrivateKey(privateKey);
|
|
226
|
+
const formattedPrivateKey = await this.decodePrivateKeyForSolana(privateKey);
|
|
227
|
+
const { rawPublicKey, clientKeyShares } = await this.importRawPrivateKey({
|
|
228
|
+
chainName,
|
|
229
|
+
privateKey: formattedPrivateKey,
|
|
230
|
+
thresholdSignatureScheme,
|
|
231
|
+
onError,
|
|
232
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
233
|
+
// update wallet map
|
|
234
|
+
this.walletMap[accountAddress] = _extends({}, this.walletMap[accountAddress] || {}, {
|
|
235
|
+
accountAddress,
|
|
236
|
+
walletId,
|
|
237
|
+
chainName: this.chainName,
|
|
238
|
+
thresholdSignatureScheme,
|
|
239
|
+
clientKeySharesBackupInfo: getClientKeyShareBackupInfo()
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
if (!rawPublicKey || !clientKeyShares) {
|
|
244
|
+
throw new Error('Error creating wallet account');
|
|
245
|
+
}
|
|
246
|
+
const { accountAddress } = await this.deriveAccountAddress(rawPublicKey);
|
|
247
|
+
if (accountAddress !== publicKey) {
|
|
248
|
+
throw new Error(`Public key mismatch: derived address ${accountAddress} !== public key ${publicKey}`);
|
|
249
|
+
}
|
|
250
|
+
// Update client key shares in wallet map
|
|
251
|
+
// warning: this might result in race condition if `onCeremonyComplete` executes at the same time
|
|
252
|
+
// TODO: remove this once iframe handling for secret shares is implemented
|
|
253
|
+
await this.setClientKeySharesToLocalStorage({
|
|
254
|
+
accountAddress,
|
|
255
|
+
clientKeyShares,
|
|
256
|
+
overwriteOrMerge: 'overwrite'
|
|
257
|
+
});
|
|
258
|
+
// Backup the new wallet without waiting for the promise to resolve
|
|
259
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
260
|
+
accountAddress,
|
|
261
|
+
clientKeyShares,
|
|
262
|
+
password
|
|
263
|
+
});
|
|
264
|
+
return {
|
|
265
|
+
accountAddress,
|
|
266
|
+
rawPublicKey: rawPublicKey,
|
|
267
|
+
clientKeyShares
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
async getSvmWallets() {
|
|
271
|
+
const wallets = await this.getWallets();
|
|
272
|
+
const svmWallets = wallets.filter((wallet)=>wallet.chainName === 'solana');
|
|
273
|
+
return svmWallets;
|
|
274
|
+
}
|
|
275
|
+
constructor({ environmentId, authToken, baseApiUrl, baseMPCRelayApiUrl }){
|
|
276
|
+
super({
|
|
277
|
+
environmentId,
|
|
278
|
+
authToken,
|
|
279
|
+
baseApiUrl,
|
|
280
|
+
baseMPCRelayApiUrl
|
|
281
|
+
}), this.chainName = 'SOL';
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export { DynamicSvmWalletClient };
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dynamic-labs-wallet/svm",
|
|
3
|
+
"version": "0.0.0-preview.112",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@dynamic-labs-wallet/browser": "0.0.0-preview.112",
|
|
7
|
+
"@solana/web3.js": "^1.98.0",
|
|
8
|
+
"bs58": "^6.0.0"
|
|
9
|
+
},
|
|
10
|
+
"nx": {
|
|
11
|
+
"sourceRoot": "packages/svm/src",
|
|
12
|
+
"projectType": "library",
|
|
13
|
+
"name": "svm",
|
|
14
|
+
"targets": {
|
|
15
|
+
"build": {}
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "./index.cjs.js",
|
|
20
|
+
"module": "./index.esm.js",
|
|
21
|
+
"types": "./index.esm.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
"./package.json": "./package.json",
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./index.esm.d.ts",
|
|
26
|
+
"import": "./index.esm.js",
|
|
27
|
+
"require": "./index.cjs.js",
|
|
28
|
+
"default": "./index.cjs.js"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../packages/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/svm/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,2BAA2B,sCAAsC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/svm/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC"}
|
package/src/svm/svm.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { ClientKeyShare, DynamicWalletClient, Ed25519KeygenResult, ThresholdSignatureScheme } from '@dynamic-labs-wallet/browser';
|
|
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, authToken, 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, }: {
|
|
19
|
+
thresholdSignatureScheme: ThresholdSignatureScheme;
|
|
20
|
+
password?: string;
|
|
21
|
+
}): Promise<{
|
|
22
|
+
accountAddress: string;
|
|
23
|
+
rawPublicKey: Uint8Array;
|
|
24
|
+
clientKeyShares: ClientKeyShare[];
|
|
25
|
+
}>;
|
|
26
|
+
deriveAccountAddress(rawPublicKey: Uint8Array): Promise<{
|
|
27
|
+
accountAddress: string;
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* This function takes a message and returns it after being signed with MPC
|
|
31
|
+
*
|
|
32
|
+
* @param message The message to sign (Uint8Array)
|
|
33
|
+
* @param accountAddress Solana address (base58 encoded)
|
|
34
|
+
* @param password The password for encrypted backup shares
|
|
35
|
+
*/
|
|
36
|
+
signMessage({ message, accountAddress, password, }: {
|
|
37
|
+
message: string;
|
|
38
|
+
accountAddress: string;
|
|
39
|
+
password?: string;
|
|
40
|
+
}): Promise<string>;
|
|
41
|
+
signTransaction({ senderAddress, transaction, password, }: {
|
|
42
|
+
senderAddress: string;
|
|
43
|
+
transaction: VersionedTransaction | Transaction;
|
|
44
|
+
password?: string;
|
|
45
|
+
}): Promise<VersionedTransaction | Transaction>;
|
|
46
|
+
/**
|
|
47
|
+
* Exports the private key for a given account address
|
|
48
|
+
*
|
|
49
|
+
* @param accountAddress The account address to export the private key for
|
|
50
|
+
* @param password The password for encrypted backup shares
|
|
51
|
+
* @returns The private key
|
|
52
|
+
*/
|
|
53
|
+
exportPrivateKey({ accountAddress, displayContainer, password, }: {
|
|
54
|
+
accountAddress: string;
|
|
55
|
+
displayContainer: HTMLElement;
|
|
56
|
+
password?: string;
|
|
57
|
+
}): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Exports the private key for a given account address
|
|
60
|
+
*
|
|
61
|
+
* @param keyShares The key shares to export the private key for
|
|
62
|
+
* @returns The private key
|
|
63
|
+
*/
|
|
64
|
+
offlineExportPrivateKey({ keyShares, derivationPath, }: {
|
|
65
|
+
keyShares: Ed25519KeygenResult[];
|
|
66
|
+
derivationPath?: string;
|
|
67
|
+
}): Promise<{
|
|
68
|
+
derivedPrivateKey: string | undefined;
|
|
69
|
+
}>;
|
|
70
|
+
/**
|
|
71
|
+
* Converts the private key to a hex string
|
|
72
|
+
*
|
|
73
|
+
* @param privateKey The private key to convert
|
|
74
|
+
* @returns The hex string
|
|
75
|
+
*/
|
|
76
|
+
decodePrivateKeyForSolana(privateKey: string): string;
|
|
77
|
+
getPublicKeyFromPrivateKey(privateKey: string): string;
|
|
78
|
+
encodePublicKey(publicKey: Uint8Array): string;
|
|
79
|
+
/**
|
|
80
|
+
* Imports the private key for a given account address
|
|
81
|
+
*
|
|
82
|
+
* @param privateKey The private key to import
|
|
83
|
+
* @param chainName The chain name to import the private key for
|
|
84
|
+
* @param thresholdSignatureScheme The threshold signature scheme to use
|
|
85
|
+
* @param password The password for encrypted backup shares
|
|
86
|
+
* @returns The account address, raw public key, and client key shares
|
|
87
|
+
*/
|
|
88
|
+
importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password, onError, }: {
|
|
89
|
+
privateKey: string;
|
|
90
|
+
chainName: string;
|
|
91
|
+
thresholdSignatureScheme: ThresholdSignatureScheme;
|
|
92
|
+
password?: string;
|
|
93
|
+
onError?: (error: Error) => void;
|
|
94
|
+
}): Promise<{
|
|
95
|
+
accountAddress: string;
|
|
96
|
+
rawPublicKey: Uint8Array | undefined;
|
|
97
|
+
clientKeyShares: ClientKeyShare[];
|
|
98
|
+
}>;
|
|
99
|
+
getSvmWallets(): Promise<any>;
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=svm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"svm.d.ts","sourceRoot":"","sources":["../../src/svm/svm.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,mBAAmB,EACnB,wBAAwB,EAGzB,MAAM,8BAA8B,CAAC;AAEtC,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,SAAS,EACT,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;IASD;;;;;OAKG;IACG,mBAAmB,CAAC,EACxB,wBAAwB,EACxB,QAAoB,GACrB,EAAE;QACD,wBAAwB,EAAE,wBAAwB,CAAC;QACnD,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC;QACV,cAAc,EAAE,MAAM,CAAC;QACvB,YAAY,EAAE,UAAU,CAAC;QACzB,eAAe,EAAE,cAAc,EAAE,CAAC;KACnC,CAAC;IA0DI,oBAAoB,CAAC,YAAY,EAAE,UAAU;;;IAOnD;;;;;;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;IA2BK,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,gBAAgB,EAChB,QAAoB,GACrB,EAAE;QACD,cAAc,EAAE,MAAM,CAAC;QACvB,gBAAgB,EAAE,WAAW,CAAC;QAC9B,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB;IAyBD;;;;;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;IAI9C;;;;;;;;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,eAAe,EAAE,cAAc,EAAE,CAAC;KACnC,CAAC;IA6DI,aAAa;CAOpB"}
|
|
@@ -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/svm/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"}
|