@dynamic-labs-wallet/evm 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 +282 -0
- package/index.esm.d.ts +1 -0
- package/index.esm.js +280 -0
- package/package.json +32 -0
- package/src/client/client.d.ts +66 -0
- package/src/client/client.d.ts.map +1 -0
- package/src/client/constants.d.ts +7 -0
- package/src/client/constants.d.ts.map +1 -0
- package/src/client/index.d.ts +2 -0
- package/src/client/index.d.ts.map +1 -0
- package/src/index.d.ts +2 -0
- package/src/index.d.ts.map +1 -0
- package/src/utils.d.ts +4 -0
- package/src/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,282 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var browser = require('@dynamic-labs-wallet/browser');
|
|
4
|
+
var viem = require('viem');
|
|
5
|
+
var chains = require('viem/chains');
|
|
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 EVM_SIGN_MESSAGE_PREFIX = `\x19Ethereum Signed Message:\n`;
|
|
19
|
+
// Error messages
|
|
20
|
+
const ERROR_KEYGEN_FAILED = 'Error with keygen';
|
|
21
|
+
const ERROR_CREATE_WALLET_ACCOUNT = 'Error creating evm wallet account';
|
|
22
|
+
const ERROR_SIGN_MESSAGE = 'Error signing message';
|
|
23
|
+
const ERROR_ACCOUNT_ADDRESS_REQUIRED = 'Account address is required';
|
|
24
|
+
const ERROR_VERIFY_MESSAGE_SIGNATURE = 'Error verifying message signature';
|
|
25
|
+
|
|
26
|
+
const formatEVMMessage = (message)=>{
|
|
27
|
+
return `${EVM_SIGN_MESSAGE_PREFIX}${message.length}${message}`;
|
|
28
|
+
};
|
|
29
|
+
const serializeECDSASignature = (signature)=>{
|
|
30
|
+
return viem.serializeSignature({
|
|
31
|
+
r: `0x${Buffer.from(signature.r).toString('hex')}`,
|
|
32
|
+
s: `0x${Buffer.from(signature.s).toString('hex')}`,
|
|
33
|
+
v: BigInt(signature.v)
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
class DynamicEvmWalletClient extends browser.DynamicWalletClient {
|
|
38
|
+
createViemPublicClient({ chain, rpcUrl }) {
|
|
39
|
+
return viem.createPublicClient({
|
|
40
|
+
chain,
|
|
41
|
+
transport: viem.http(rpcUrl)
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
async createWalletAccount({ thresholdSignatureScheme, password = undefined, onError }) {
|
|
45
|
+
try {
|
|
46
|
+
// Generate key shares for given threshold signature scheme (TSS)
|
|
47
|
+
const { rawPublicKey, clientKeyShares } = await this.keyGen({
|
|
48
|
+
chainName: this.chainName,
|
|
49
|
+
thresholdSignatureScheme,
|
|
50
|
+
onError,
|
|
51
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
52
|
+
// update wallet map
|
|
53
|
+
const checksumAddress = viem.getAddress(accountAddress);
|
|
54
|
+
this.walletMap[checksumAddress] = _extends({}, this.walletMap[checksumAddress] || {}, {
|
|
55
|
+
accountAddress: checksumAddress,
|
|
56
|
+
walletId,
|
|
57
|
+
chainName: this.chainName,
|
|
58
|
+
thresholdSignatureScheme,
|
|
59
|
+
clientKeySharesBackupInfo: browser.getClientKeyShareBackupInfo()
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
if (!rawPublicKey || !clientKeyShares) {
|
|
64
|
+
throw new Error(ERROR_KEYGEN_FAILED);
|
|
65
|
+
}
|
|
66
|
+
const { accountAddress, publicKeyHex } = await this.deriveAccountAddress({
|
|
67
|
+
rawPublicKey: rawPublicKey
|
|
68
|
+
});
|
|
69
|
+
// Update client key shares in wallet map
|
|
70
|
+
// warning: this might result in race condition if `onCeremonyComplete` executes at the same time
|
|
71
|
+
// TODO: remove this once iframe handling for secret shares is implemented
|
|
72
|
+
await this.setClientKeySharesToLocalStorage({
|
|
73
|
+
accountAddress,
|
|
74
|
+
clientKeyShares,
|
|
75
|
+
overwriteOrMerge: 'overwrite'
|
|
76
|
+
});
|
|
77
|
+
// Backup the new wallet without waiting for the promise to resolve
|
|
78
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
79
|
+
accountAddress,
|
|
80
|
+
clientKeyShares,
|
|
81
|
+
password
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
accountAddress,
|
|
85
|
+
rawPublicKey,
|
|
86
|
+
publicKeyHex,
|
|
87
|
+
clientKeyShares
|
|
88
|
+
};
|
|
89
|
+
} catch (error) {
|
|
90
|
+
this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
|
|
91
|
+
throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async signMessage({ message, accountAddress, password = undefined }) {
|
|
95
|
+
await this.verifyPassword({
|
|
96
|
+
accountAddress,
|
|
97
|
+
password,
|
|
98
|
+
walletOperation: browser.WalletOperation.SIGN_MESSAGE
|
|
99
|
+
});
|
|
100
|
+
await this.getWallet({
|
|
101
|
+
accountAddress,
|
|
102
|
+
walletOperation: browser.WalletOperation.SIGN_MESSAGE
|
|
103
|
+
});
|
|
104
|
+
try {
|
|
105
|
+
if (!accountAddress) {
|
|
106
|
+
throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
|
|
107
|
+
}
|
|
108
|
+
// Format the message for EVM signing
|
|
109
|
+
const formattedMessage = formatEVMMessage(message);
|
|
110
|
+
// Sign the message using MPC
|
|
111
|
+
const signatureEcdsa = await this.sign({
|
|
112
|
+
message: formattedMessage,
|
|
113
|
+
accountAddress: accountAddress,
|
|
114
|
+
chainName: this.chainName,
|
|
115
|
+
password
|
|
116
|
+
});
|
|
117
|
+
// Serialize the signature
|
|
118
|
+
const serializedSignature = serializeECDSASignature(signatureEcdsa);
|
|
119
|
+
return serializedSignature;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
this.logger.error(ERROR_SIGN_MESSAGE, error);
|
|
122
|
+
throw new Error(ERROR_SIGN_MESSAGE);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async verifyMessageSignature({ accountAddress, message, signature }) {
|
|
126
|
+
try {
|
|
127
|
+
// Verify the signature using the public client
|
|
128
|
+
const publicClient = this.createViemPublicClient({
|
|
129
|
+
chain: chains.mainnet
|
|
130
|
+
});
|
|
131
|
+
const verified = await publicClient.verifyMessage({
|
|
132
|
+
address: accountAddress,
|
|
133
|
+
message,
|
|
134
|
+
signature: signature
|
|
135
|
+
});
|
|
136
|
+
return verified;
|
|
137
|
+
} catch (error) {
|
|
138
|
+
this.logger.error(ERROR_VERIFY_MESSAGE_SIGNATURE, error);
|
|
139
|
+
throw new Error(ERROR_VERIFY_MESSAGE_SIGNATURE);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async signTransaction({ senderAddress, transaction, password = undefined }) {
|
|
143
|
+
await this.verifyPassword({
|
|
144
|
+
accountAddress: senderAddress,
|
|
145
|
+
password,
|
|
146
|
+
walletOperation: browser.WalletOperation.SIGN_TRANSACTION
|
|
147
|
+
});
|
|
148
|
+
const serializedTx = viem.serializeTransaction(transaction);
|
|
149
|
+
const serializedTxBytes = Uint8Array.from(Buffer.from(serializedTx.slice(2), 'hex'));
|
|
150
|
+
if (!(serializedTxBytes instanceof Uint8Array)) {
|
|
151
|
+
throw new Error('Invalid serializedTxBytes');
|
|
152
|
+
}
|
|
153
|
+
// Get signature using MPC (this will coordinate with server party)
|
|
154
|
+
const signatureEcdsa = await this.sign({
|
|
155
|
+
message: serializedTxBytes,
|
|
156
|
+
accountAddress: senderAddress,
|
|
157
|
+
chainName: this.chainName,
|
|
158
|
+
password
|
|
159
|
+
});
|
|
160
|
+
if (!('r' in signatureEcdsa && 's' in signatureEcdsa && 'v' in signatureEcdsa)) {
|
|
161
|
+
throw new Error('Invalid signature format returned from MPC signing');
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const r = `0x${Buffer.from(signatureEcdsa.r).toString('hex')}`;
|
|
165
|
+
const s = `0x${Buffer.from(signatureEcdsa.s).toString('hex')}`;
|
|
166
|
+
const v = BigInt(signatureEcdsa.v);
|
|
167
|
+
const signedTx = _extends({}, transaction, {
|
|
168
|
+
r: r,
|
|
169
|
+
s: s,
|
|
170
|
+
v: v
|
|
171
|
+
});
|
|
172
|
+
const serializedSignedTx = viem.serializeTransaction(signedTx);
|
|
173
|
+
return serializedSignedTx;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
this.logger.error('Error signing transaction:', error);
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
deriveAccountAddress({ rawPublicKey }) {
|
|
180
|
+
const serializedUncompressed = rawPublicKey.serializeUncompressed();
|
|
181
|
+
const firstByteRemoved = serializedUncompressed.slice(1);
|
|
182
|
+
const hashed = browser.MessageHash.keccak256(firstByteRemoved).bytes;
|
|
183
|
+
const lastTwentyBytes = hashed.slice(-20);
|
|
184
|
+
const accountAddress = '0x' + Buffer.from(lastTwentyBytes).toString('hex');
|
|
185
|
+
const publicKeyHex = rawPublicKey.pubKeyAsHex();
|
|
186
|
+
return {
|
|
187
|
+
accountAddress: viem.getAddress(accountAddress),
|
|
188
|
+
publicKeyHex
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
async exportPrivateKey({ accountAddress, displayContainer, password = undefined }) {
|
|
192
|
+
await this.verifyPassword({
|
|
193
|
+
accountAddress,
|
|
194
|
+
password,
|
|
195
|
+
walletOperation: browser.WalletOperation.EXPORT_PRIVATE_KEY
|
|
196
|
+
});
|
|
197
|
+
const { derivedPrivateKey } = await this.exportKey({
|
|
198
|
+
accountAddress,
|
|
199
|
+
displayContainer,
|
|
200
|
+
chainName: this.chainName,
|
|
201
|
+
password
|
|
202
|
+
});
|
|
203
|
+
// Display the private key in the container via iframe
|
|
204
|
+
const { iframeDisplay } = await this.initializeIframeDisplayForContainer({
|
|
205
|
+
container: displayContainer
|
|
206
|
+
});
|
|
207
|
+
iframeDisplay.displayPrivateKey(accountAddress, derivedPrivateKey);
|
|
208
|
+
}
|
|
209
|
+
async offlineExportPrivateKey({ keyShares, derivationPath }) {
|
|
210
|
+
const { derivedPrivateKey } = await this.offlineExportKey({
|
|
211
|
+
chainName: this.chainName,
|
|
212
|
+
keyShares,
|
|
213
|
+
derivationPath
|
|
214
|
+
});
|
|
215
|
+
return {
|
|
216
|
+
derivedPrivateKey
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
async importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password = undefined, onError }) {
|
|
220
|
+
// TODO: validate private key for EVM
|
|
221
|
+
const { rawPublicKey, clientKeyShares } = await this.importRawPrivateKey({
|
|
222
|
+
chainName,
|
|
223
|
+
privateKey,
|
|
224
|
+
thresholdSignatureScheme,
|
|
225
|
+
onError,
|
|
226
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
227
|
+
// update wallet map
|
|
228
|
+
const checksumAddress = viem.getAddress(accountAddress);
|
|
229
|
+
this.walletMap[checksumAddress] = _extends({}, this.walletMap[checksumAddress] || {}, {
|
|
230
|
+
accountAddress: checksumAddress,
|
|
231
|
+
walletId,
|
|
232
|
+
chainName: this.chainName,
|
|
233
|
+
thresholdSignatureScheme,
|
|
234
|
+
clientKeySharesBackupInfo: browser.getClientKeyShareBackupInfo()
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
if (!rawPublicKey || !clientKeyShares) {
|
|
239
|
+
throw new Error('Error creating wallet account');
|
|
240
|
+
}
|
|
241
|
+
const { accountAddress, publicKeyHex } = await this.deriveAccountAddress({
|
|
242
|
+
rawPublicKey: rawPublicKey
|
|
243
|
+
});
|
|
244
|
+
// Update client key shares in wallet map
|
|
245
|
+
// warning: this might result in race condition if `onCeremonyComplete` executes at the same time
|
|
246
|
+
// TODO: remove this once iframe handling for secret shares is implemented
|
|
247
|
+
await this.setClientKeySharesToLocalStorage({
|
|
248
|
+
accountAddress,
|
|
249
|
+
clientKeyShares,
|
|
250
|
+
overwriteOrMerge: 'overwrite'
|
|
251
|
+
});
|
|
252
|
+
// Backup the new wallet without waiting for the promise to resolve
|
|
253
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
254
|
+
accountAddress,
|
|
255
|
+
clientKeyShares,
|
|
256
|
+
password
|
|
257
|
+
});
|
|
258
|
+
return {
|
|
259
|
+
accountAddress,
|
|
260
|
+
rawPublicKey,
|
|
261
|
+
publicKeyHex,
|
|
262
|
+
clientKeyShares
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
async getEvmWallets() {
|
|
266
|
+
const wallets = await this.getWallets();
|
|
267
|
+
const evmWallets = wallets.filter((wallet)=>wallet.chainName === 'eip155');
|
|
268
|
+
return evmWallets;
|
|
269
|
+
}
|
|
270
|
+
constructor({ environmentId, authToken, baseApiUrl, baseMPCRelayApiUrl, storageKey, debug }){
|
|
271
|
+
super({
|
|
272
|
+
environmentId,
|
|
273
|
+
authToken,
|
|
274
|
+
baseApiUrl,
|
|
275
|
+
baseMPCRelayApiUrl,
|
|
276
|
+
storageKey,
|
|
277
|
+
debug
|
|
278
|
+
}), this.chainName = 'EVM';
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
exports.DynamicEvmWalletClient = DynamicEvmWalletClient;
|
package/index.esm.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { DynamicWalletClient, getClientKeyShareBackupInfo, WalletOperation, MessageHash } from '@dynamic-labs-wallet/browser';
|
|
2
|
+
import { serializeSignature, createPublicClient, http, getAddress, serializeTransaction } from 'viem';
|
|
3
|
+
import { mainnet } from 'viem/chains';
|
|
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 EVM_SIGN_MESSAGE_PREFIX = `\x19Ethereum Signed Message:\n`;
|
|
17
|
+
// Error messages
|
|
18
|
+
const ERROR_KEYGEN_FAILED = 'Error with keygen';
|
|
19
|
+
const ERROR_CREATE_WALLET_ACCOUNT = 'Error creating evm wallet account';
|
|
20
|
+
const ERROR_SIGN_MESSAGE = 'Error signing message';
|
|
21
|
+
const ERROR_ACCOUNT_ADDRESS_REQUIRED = 'Account address is required';
|
|
22
|
+
const ERROR_VERIFY_MESSAGE_SIGNATURE = 'Error verifying message signature';
|
|
23
|
+
|
|
24
|
+
const formatEVMMessage = (message)=>{
|
|
25
|
+
return `${EVM_SIGN_MESSAGE_PREFIX}${message.length}${message}`;
|
|
26
|
+
};
|
|
27
|
+
const serializeECDSASignature = (signature)=>{
|
|
28
|
+
return serializeSignature({
|
|
29
|
+
r: `0x${Buffer.from(signature.r).toString('hex')}`,
|
|
30
|
+
s: `0x${Buffer.from(signature.s).toString('hex')}`,
|
|
31
|
+
v: BigInt(signature.v)
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
class DynamicEvmWalletClient extends DynamicWalletClient {
|
|
36
|
+
createViemPublicClient({ chain, rpcUrl }) {
|
|
37
|
+
return createPublicClient({
|
|
38
|
+
chain,
|
|
39
|
+
transport: http(rpcUrl)
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
async createWalletAccount({ thresholdSignatureScheme, password = undefined, onError }) {
|
|
43
|
+
try {
|
|
44
|
+
// Generate key shares for given threshold signature scheme (TSS)
|
|
45
|
+
const { rawPublicKey, clientKeyShares } = await this.keyGen({
|
|
46
|
+
chainName: this.chainName,
|
|
47
|
+
thresholdSignatureScheme,
|
|
48
|
+
onError,
|
|
49
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
50
|
+
// update wallet map
|
|
51
|
+
const checksumAddress = getAddress(accountAddress);
|
|
52
|
+
this.walletMap[checksumAddress] = _extends({}, this.walletMap[checksumAddress] || {}, {
|
|
53
|
+
accountAddress: checksumAddress,
|
|
54
|
+
walletId,
|
|
55
|
+
chainName: this.chainName,
|
|
56
|
+
thresholdSignatureScheme,
|
|
57
|
+
clientKeySharesBackupInfo: getClientKeyShareBackupInfo()
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
if (!rawPublicKey || !clientKeyShares) {
|
|
62
|
+
throw new Error(ERROR_KEYGEN_FAILED);
|
|
63
|
+
}
|
|
64
|
+
const { accountAddress, publicKeyHex } = await this.deriveAccountAddress({
|
|
65
|
+
rawPublicKey: rawPublicKey
|
|
66
|
+
});
|
|
67
|
+
// Update client key shares in wallet map
|
|
68
|
+
// warning: this might result in race condition if `onCeremonyComplete` executes at the same time
|
|
69
|
+
// TODO: remove this once iframe handling for secret shares is implemented
|
|
70
|
+
await this.setClientKeySharesToLocalStorage({
|
|
71
|
+
accountAddress,
|
|
72
|
+
clientKeyShares,
|
|
73
|
+
overwriteOrMerge: 'overwrite'
|
|
74
|
+
});
|
|
75
|
+
// Backup the new wallet without waiting for the promise to resolve
|
|
76
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
77
|
+
accountAddress,
|
|
78
|
+
clientKeyShares,
|
|
79
|
+
password
|
|
80
|
+
});
|
|
81
|
+
return {
|
|
82
|
+
accountAddress,
|
|
83
|
+
rawPublicKey,
|
|
84
|
+
publicKeyHex,
|
|
85
|
+
clientKeyShares
|
|
86
|
+
};
|
|
87
|
+
} catch (error) {
|
|
88
|
+
this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
|
|
89
|
+
throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async signMessage({ message, accountAddress, password = undefined }) {
|
|
93
|
+
await this.verifyPassword({
|
|
94
|
+
accountAddress,
|
|
95
|
+
password,
|
|
96
|
+
walletOperation: WalletOperation.SIGN_MESSAGE
|
|
97
|
+
});
|
|
98
|
+
await this.getWallet({
|
|
99
|
+
accountAddress,
|
|
100
|
+
walletOperation: WalletOperation.SIGN_MESSAGE
|
|
101
|
+
});
|
|
102
|
+
try {
|
|
103
|
+
if (!accountAddress) {
|
|
104
|
+
throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
|
|
105
|
+
}
|
|
106
|
+
// Format the message for EVM signing
|
|
107
|
+
const formattedMessage = formatEVMMessage(message);
|
|
108
|
+
// Sign the message using MPC
|
|
109
|
+
const signatureEcdsa = await this.sign({
|
|
110
|
+
message: formattedMessage,
|
|
111
|
+
accountAddress: accountAddress,
|
|
112
|
+
chainName: this.chainName,
|
|
113
|
+
password
|
|
114
|
+
});
|
|
115
|
+
// Serialize the signature
|
|
116
|
+
const serializedSignature = serializeECDSASignature(signatureEcdsa);
|
|
117
|
+
return serializedSignature;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
this.logger.error(ERROR_SIGN_MESSAGE, error);
|
|
120
|
+
throw new Error(ERROR_SIGN_MESSAGE);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async verifyMessageSignature({ accountAddress, message, signature }) {
|
|
124
|
+
try {
|
|
125
|
+
// Verify the signature using the public client
|
|
126
|
+
const publicClient = this.createViemPublicClient({
|
|
127
|
+
chain: mainnet
|
|
128
|
+
});
|
|
129
|
+
const verified = await publicClient.verifyMessage({
|
|
130
|
+
address: accountAddress,
|
|
131
|
+
message,
|
|
132
|
+
signature: signature
|
|
133
|
+
});
|
|
134
|
+
return verified;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
this.logger.error(ERROR_VERIFY_MESSAGE_SIGNATURE, error);
|
|
137
|
+
throw new Error(ERROR_VERIFY_MESSAGE_SIGNATURE);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
async signTransaction({ senderAddress, transaction, password = undefined }) {
|
|
141
|
+
await this.verifyPassword({
|
|
142
|
+
accountAddress: senderAddress,
|
|
143
|
+
password,
|
|
144
|
+
walletOperation: WalletOperation.SIGN_TRANSACTION
|
|
145
|
+
});
|
|
146
|
+
const serializedTx = serializeTransaction(transaction);
|
|
147
|
+
const serializedTxBytes = Uint8Array.from(Buffer.from(serializedTx.slice(2), 'hex'));
|
|
148
|
+
if (!(serializedTxBytes instanceof Uint8Array)) {
|
|
149
|
+
throw new Error('Invalid serializedTxBytes');
|
|
150
|
+
}
|
|
151
|
+
// Get signature using MPC (this will coordinate with server party)
|
|
152
|
+
const signatureEcdsa = await this.sign({
|
|
153
|
+
message: serializedTxBytes,
|
|
154
|
+
accountAddress: senderAddress,
|
|
155
|
+
chainName: this.chainName,
|
|
156
|
+
password
|
|
157
|
+
});
|
|
158
|
+
if (!('r' in signatureEcdsa && 's' in signatureEcdsa && 'v' in signatureEcdsa)) {
|
|
159
|
+
throw new Error('Invalid signature format returned from MPC signing');
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
const r = `0x${Buffer.from(signatureEcdsa.r).toString('hex')}`;
|
|
163
|
+
const s = `0x${Buffer.from(signatureEcdsa.s).toString('hex')}`;
|
|
164
|
+
const v = BigInt(signatureEcdsa.v);
|
|
165
|
+
const signedTx = _extends({}, transaction, {
|
|
166
|
+
r: r,
|
|
167
|
+
s: s,
|
|
168
|
+
v: v
|
|
169
|
+
});
|
|
170
|
+
const serializedSignedTx = serializeTransaction(signedTx);
|
|
171
|
+
return serializedSignedTx;
|
|
172
|
+
} catch (error) {
|
|
173
|
+
this.logger.error('Error signing transaction:', error);
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
deriveAccountAddress({ rawPublicKey }) {
|
|
178
|
+
const serializedUncompressed = rawPublicKey.serializeUncompressed();
|
|
179
|
+
const firstByteRemoved = serializedUncompressed.slice(1);
|
|
180
|
+
const hashed = MessageHash.keccak256(firstByteRemoved).bytes;
|
|
181
|
+
const lastTwentyBytes = hashed.slice(-20);
|
|
182
|
+
const accountAddress = '0x' + Buffer.from(lastTwentyBytes).toString('hex');
|
|
183
|
+
const publicKeyHex = rawPublicKey.pubKeyAsHex();
|
|
184
|
+
return {
|
|
185
|
+
accountAddress: getAddress(accountAddress),
|
|
186
|
+
publicKeyHex
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
async exportPrivateKey({ accountAddress, displayContainer, password = undefined }) {
|
|
190
|
+
await this.verifyPassword({
|
|
191
|
+
accountAddress,
|
|
192
|
+
password,
|
|
193
|
+
walletOperation: WalletOperation.EXPORT_PRIVATE_KEY
|
|
194
|
+
});
|
|
195
|
+
const { derivedPrivateKey } = await this.exportKey({
|
|
196
|
+
accountAddress,
|
|
197
|
+
displayContainer,
|
|
198
|
+
chainName: this.chainName,
|
|
199
|
+
password
|
|
200
|
+
});
|
|
201
|
+
// Display the private key in the container via iframe
|
|
202
|
+
const { iframeDisplay } = await this.initializeIframeDisplayForContainer({
|
|
203
|
+
container: displayContainer
|
|
204
|
+
});
|
|
205
|
+
iframeDisplay.displayPrivateKey(accountAddress, derivedPrivateKey);
|
|
206
|
+
}
|
|
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
|
+
async importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password = undefined, onError }) {
|
|
218
|
+
// TODO: validate private key for EVM
|
|
219
|
+
const { rawPublicKey, clientKeyShares } = await this.importRawPrivateKey({
|
|
220
|
+
chainName,
|
|
221
|
+
privateKey,
|
|
222
|
+
thresholdSignatureScheme,
|
|
223
|
+
onError,
|
|
224
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
225
|
+
// update wallet map
|
|
226
|
+
const checksumAddress = getAddress(accountAddress);
|
|
227
|
+
this.walletMap[checksumAddress] = _extends({}, this.walletMap[checksumAddress] || {}, {
|
|
228
|
+
accountAddress: checksumAddress,
|
|
229
|
+
walletId,
|
|
230
|
+
chainName: this.chainName,
|
|
231
|
+
thresholdSignatureScheme,
|
|
232
|
+
clientKeySharesBackupInfo: getClientKeyShareBackupInfo()
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
if (!rawPublicKey || !clientKeyShares) {
|
|
237
|
+
throw new Error('Error creating wallet account');
|
|
238
|
+
}
|
|
239
|
+
const { accountAddress, publicKeyHex } = await this.deriveAccountAddress({
|
|
240
|
+
rawPublicKey: rawPublicKey
|
|
241
|
+
});
|
|
242
|
+
// Update client key shares in wallet map
|
|
243
|
+
// warning: this might result in race condition if `onCeremonyComplete` executes at the same time
|
|
244
|
+
// TODO: remove this once iframe handling for secret shares is implemented
|
|
245
|
+
await this.setClientKeySharesToLocalStorage({
|
|
246
|
+
accountAddress,
|
|
247
|
+
clientKeyShares,
|
|
248
|
+
overwriteOrMerge: 'overwrite'
|
|
249
|
+
});
|
|
250
|
+
// Backup the new wallet without waiting for the promise to resolve
|
|
251
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
252
|
+
accountAddress,
|
|
253
|
+
clientKeyShares,
|
|
254
|
+
password
|
|
255
|
+
});
|
|
256
|
+
return {
|
|
257
|
+
accountAddress,
|
|
258
|
+
rawPublicKey,
|
|
259
|
+
publicKeyHex,
|
|
260
|
+
clientKeyShares
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
async getEvmWallets() {
|
|
264
|
+
const wallets = await this.getWallets();
|
|
265
|
+
const evmWallets = wallets.filter((wallet)=>wallet.chainName === 'eip155');
|
|
266
|
+
return evmWallets;
|
|
267
|
+
}
|
|
268
|
+
constructor({ environmentId, authToken, baseApiUrl, baseMPCRelayApiUrl, storageKey, debug }){
|
|
269
|
+
super({
|
|
270
|
+
environmentId,
|
|
271
|
+
authToken,
|
|
272
|
+
baseApiUrl,
|
|
273
|
+
baseMPCRelayApiUrl,
|
|
274
|
+
storageKey,
|
|
275
|
+
debug
|
|
276
|
+
}), this.chainName = 'EVM';
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export { DynamicEvmWalletClient };
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dynamic-labs-wallet/evm",
|
|
3
|
+
"version": "0.0.0-preview.112",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@dynamic-labs-wallet/browser": "0.0.0-preview.112"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"viem": "^2.22.1"
|
|
10
|
+
},
|
|
11
|
+
"nx": {
|
|
12
|
+
"sourceRoot": "packages/evm/src",
|
|
13
|
+
"projectType": "library",
|
|
14
|
+
"name": "evm",
|
|
15
|
+
"targets": {
|
|
16
|
+
"build": {}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "./index.cjs.js",
|
|
21
|
+
"module": "./index.esm.js",
|
|
22
|
+
"types": "./index.esm.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
"./package.json": "./package.json",
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./index.esm.d.ts",
|
|
27
|
+
"import": "./index.esm.js",
|
|
28
|
+
"require": "./index.cjs.js",
|
|
29
|
+
"default": "./index.cjs.js"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ClientKeyShare, DynamicWalletClient, EcdsaKeygenResult, EcdsaPublicKey, Ed25519KeygenResult, ThresholdSignatureScheme, DynamicWalletClientProps } from '@dynamic-labs-wallet/browser';
|
|
2
|
+
import { type PublicClient, type Chain, type SignableMessage, type TransactionSerializable } from 'viem';
|
|
3
|
+
export declare class DynamicEvmWalletClient extends DynamicWalletClient {
|
|
4
|
+
readonly chainName = "EVM";
|
|
5
|
+
constructor({ environmentId, authToken, baseApiUrl, baseMPCRelayApiUrl, storageKey, debug, }: DynamicWalletClientProps);
|
|
6
|
+
createViemPublicClient({ chain, rpcUrl, }: {
|
|
7
|
+
chain: Chain;
|
|
8
|
+
rpcUrl?: string;
|
|
9
|
+
}): PublicClient;
|
|
10
|
+
createWalletAccount({ thresholdSignatureScheme, password, onError, }: {
|
|
11
|
+
thresholdSignatureScheme: ThresholdSignatureScheme;
|
|
12
|
+
password?: string;
|
|
13
|
+
onError?: (error: Error) => void;
|
|
14
|
+
}): Promise<{
|
|
15
|
+
accountAddress: string;
|
|
16
|
+
publicKeyHex: string;
|
|
17
|
+
rawPublicKey: EcdsaPublicKey | Uint8Array | undefined;
|
|
18
|
+
clientKeyShares: ClientKeyShare[];
|
|
19
|
+
}>;
|
|
20
|
+
signMessage({ message, accountAddress, password, }: {
|
|
21
|
+
message: string;
|
|
22
|
+
accountAddress: string;
|
|
23
|
+
password?: string;
|
|
24
|
+
}): Promise<`0x${string}`>;
|
|
25
|
+
verifyMessageSignature({ accountAddress, message, signature, }: {
|
|
26
|
+
accountAddress: string;
|
|
27
|
+
message: SignableMessage;
|
|
28
|
+
signature: any;
|
|
29
|
+
}): Promise<boolean>;
|
|
30
|
+
signTransaction({ senderAddress, transaction, password, }: {
|
|
31
|
+
senderAddress: string;
|
|
32
|
+
transaction: TransactionSerializable;
|
|
33
|
+
password?: string;
|
|
34
|
+
}): Promise<string>;
|
|
35
|
+
deriveAccountAddress({ rawPublicKey }: {
|
|
36
|
+
rawPublicKey: EcdsaPublicKey;
|
|
37
|
+
}): {
|
|
38
|
+
accountAddress: `0x${string}`;
|
|
39
|
+
publicKeyHex: any;
|
|
40
|
+
};
|
|
41
|
+
exportPrivateKey({ accountAddress, displayContainer, password, }: {
|
|
42
|
+
accountAddress: string;
|
|
43
|
+
displayContainer: HTMLElement;
|
|
44
|
+
password?: string;
|
|
45
|
+
}): Promise<void>;
|
|
46
|
+
offlineExportPrivateKey({ keyShares, derivationPath, }: {
|
|
47
|
+
keyShares: (EcdsaKeygenResult | Ed25519KeygenResult)[];
|
|
48
|
+
derivationPath?: string;
|
|
49
|
+
}): Promise<{
|
|
50
|
+
derivedPrivateKey: string | undefined;
|
|
51
|
+
}>;
|
|
52
|
+
importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password, onError, }: {
|
|
53
|
+
privateKey: string;
|
|
54
|
+
chainName: string;
|
|
55
|
+
thresholdSignatureScheme: ThresholdSignatureScheme;
|
|
56
|
+
password?: string;
|
|
57
|
+
onError?: (error: Error) => void;
|
|
58
|
+
}): Promise<{
|
|
59
|
+
accountAddress: string;
|
|
60
|
+
publicKeyHex: string;
|
|
61
|
+
rawPublicKey: EcdsaPublicKey | Uint8Array | undefined;
|
|
62
|
+
clientKeyShares: ClientKeyShare[];
|
|
63
|
+
}>;
|
|
64
|
+
getEvmWallets(): Promise<any>;
|
|
65
|
+
}
|
|
66
|
+
//# 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,E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:\n";
|
|
2
|
+
export declare const ERROR_KEYGEN_FAILED = "Error with keygen";
|
|
3
|
+
export declare const ERROR_CREATE_WALLET_ACCOUNT = "Error creating evm wallet account";
|
|
4
|
+
export declare const ERROR_SIGN_MESSAGE = "Error signing message";
|
|
5
|
+
export declare const ERROR_ACCOUNT_ADDRESS_REQUIRED = "Account address is required";
|
|
6
|
+
export declare const ERROR_VERIFY_MESSAGE_SIGNATURE = "Error verifying message signature";
|
|
7
|
+
//# 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,uBAAuB,qCAAmC,CAAC;AAGxE,eAAO,MAAM,mBAAmB,sBAAsB,CAAC;AAEvD,eAAO,MAAM,2BAA2B,sCAAsC,CAAC;AAE/E,eAAO,MAAM,kBAAkB,0BAA0B,CAAC;AAE1D,eAAO,MAAM,8BAA8B,gCAAgC,CAAC;AAE5E,eAAO,MAAM,8BAA8B,sCACN,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
|
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,UAAU,CAAC"}
|
package/src/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../packages/src/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAE9D,eAAO,MAAM,gBAAgB,YAAa,MAAM,WAE/C,CAAC;AAEF,eAAO,MAAM,uBAAuB,cAAe,cAAc,kBAMhE,CAAC"}
|