@dynamic-labs-wallet/node-evm 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 +1 -0
- package/index.cjs.js +260 -0
- package/index.esm.d.ts +1 -0
- package/index.esm.js +258 -0
- package/package.json +39 -0
- package/src/client/client.d.ts +61 -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 +10 -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,260 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node = require('@dynamic-labs-wallet/node');
|
|
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
|
+
const deriveAccountAddress = ({ rawPublicKey })=>{
|
|
37
|
+
const serializedUncompressed = rawPublicKey.serializeUncompressed();
|
|
38
|
+
const firstByteRemoved = serializedUncompressed.slice(1);
|
|
39
|
+
const hashed = node.MessageHash.keccak256(firstByteRemoved).bytes;
|
|
40
|
+
const lastTwentyBytes = hashed.slice(-20);
|
|
41
|
+
const accountAddress = '0x' + Buffer.from(lastTwentyBytes).toString('hex');
|
|
42
|
+
const publicKeyHex = rawPublicKey.pubKeyAsHex();
|
|
43
|
+
return {
|
|
44
|
+
accountAddress: viem.getAddress(accountAddress),
|
|
45
|
+
publicKeyHex
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
50
|
+
createViemPublicClient({ chain, rpcUrl }) {
|
|
51
|
+
return viem.createPublicClient({
|
|
52
|
+
chain,
|
|
53
|
+
transport: viem.http(rpcUrl)
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
async createWalletAccount({ thresholdSignatureScheme, password = undefined, onError }) {
|
|
57
|
+
try {
|
|
58
|
+
// Generate key shares for given threshold signature scheme (TSS)
|
|
59
|
+
const { rawPublicKey, externalServerKeyShares } = await this.keyGen({
|
|
60
|
+
chainName: this.chainName,
|
|
61
|
+
thresholdSignatureScheme,
|
|
62
|
+
onError,
|
|
63
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
64
|
+
// update wallet map
|
|
65
|
+
const checksumAddress = viem.getAddress(accountAddress);
|
|
66
|
+
this.walletMap[checksumAddress] = _extends({}, this.walletMap[checksumAddress] || {}, {
|
|
67
|
+
accountAddress: checksumAddress,
|
|
68
|
+
walletId,
|
|
69
|
+
chainName: this.chainName,
|
|
70
|
+
thresholdSignatureScheme,
|
|
71
|
+
externalServerKeySharesBackupInfo: node.getExternalServerKeyShareBackupInfo()
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
if (!rawPublicKey || !externalServerKeyShares) {
|
|
76
|
+
throw new Error(ERROR_KEYGEN_FAILED);
|
|
77
|
+
}
|
|
78
|
+
// Get EVM address from public key
|
|
79
|
+
const { accountAddress, publicKeyHex } = deriveAccountAddress({
|
|
80
|
+
rawPublicKey: rawPublicKey
|
|
81
|
+
});
|
|
82
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
83
|
+
accountAddress,
|
|
84
|
+
externalServerKeyShares,
|
|
85
|
+
password
|
|
86
|
+
});
|
|
87
|
+
return {
|
|
88
|
+
accountAddress,
|
|
89
|
+
rawPublicKey,
|
|
90
|
+
publicKeyHex,
|
|
91
|
+
externalServerKeyShares
|
|
92
|
+
};
|
|
93
|
+
} catch (error) {
|
|
94
|
+
// this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
|
|
95
|
+
throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async signMessage({ message, accountAddress, password = undefined }) {
|
|
99
|
+
await this.verifyPassword({
|
|
100
|
+
accountAddress,
|
|
101
|
+
password,
|
|
102
|
+
walletOperation: node.WalletOperation.SIGN_MESSAGE
|
|
103
|
+
});
|
|
104
|
+
await this.getWallet({
|
|
105
|
+
accountAddress,
|
|
106
|
+
walletOperation: node.WalletOperation.SIGN_MESSAGE
|
|
107
|
+
});
|
|
108
|
+
try {
|
|
109
|
+
if (!accountAddress) {
|
|
110
|
+
throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
|
|
111
|
+
}
|
|
112
|
+
// Format the message for EVM signing
|
|
113
|
+
const formattedMessage = formatEVMMessage(message);
|
|
114
|
+
// Sign the message using MPC
|
|
115
|
+
const signatureEcdsa = await this.sign({
|
|
116
|
+
message: formattedMessage,
|
|
117
|
+
accountAddress: accountAddress,
|
|
118
|
+
chainName: this.chainName,
|
|
119
|
+
password
|
|
120
|
+
});
|
|
121
|
+
// Serialize the signature
|
|
122
|
+
const serializedSignature = serializeECDSASignature(signatureEcdsa);
|
|
123
|
+
return serializedSignature;
|
|
124
|
+
} catch (error) {
|
|
125
|
+
this.logger.error(ERROR_SIGN_MESSAGE, error);
|
|
126
|
+
throw new Error(ERROR_SIGN_MESSAGE);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async verifyMessageSignature({ accountAddress, message, signature }) {
|
|
130
|
+
try {
|
|
131
|
+
// Verify the signature using the public client
|
|
132
|
+
const publicClient = this.createViemPublicClient({
|
|
133
|
+
chain: chains.mainnet
|
|
134
|
+
});
|
|
135
|
+
const verified = await publicClient.verifyMessage({
|
|
136
|
+
address: accountAddress,
|
|
137
|
+
message,
|
|
138
|
+
signature: signature
|
|
139
|
+
});
|
|
140
|
+
return verified;
|
|
141
|
+
} catch (error) {
|
|
142
|
+
this.logger.error(ERROR_VERIFY_MESSAGE_SIGNATURE, error);
|
|
143
|
+
throw new Error(ERROR_VERIFY_MESSAGE_SIGNATURE);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async signTransaction({ senderAddress, transaction, password = undefined }) {
|
|
147
|
+
await this.verifyPassword({
|
|
148
|
+
accountAddress: senderAddress,
|
|
149
|
+
password,
|
|
150
|
+
walletOperation: node.WalletOperation.SIGN_TRANSACTION
|
|
151
|
+
});
|
|
152
|
+
const serializedTx = viem.serializeTransaction(transaction);
|
|
153
|
+
const serializedTxBytes = Uint8Array.from(Buffer.from(serializedTx.slice(2), 'hex'));
|
|
154
|
+
if (!(serializedTxBytes instanceof Uint8Array)) {
|
|
155
|
+
throw new Error('Invalid serializedTxBytes');
|
|
156
|
+
}
|
|
157
|
+
// Get signature using MPC (this will coordinate with server party)
|
|
158
|
+
const signatureEcdsa = await this.sign({
|
|
159
|
+
message: serializedTxBytes,
|
|
160
|
+
accountAddress: senderAddress,
|
|
161
|
+
chainName: this.chainName,
|
|
162
|
+
password
|
|
163
|
+
});
|
|
164
|
+
if (!('r' in signatureEcdsa && 's' in signatureEcdsa && 'v' in signatureEcdsa)) {
|
|
165
|
+
throw new Error('Invalid signature format returned from MPC signing');
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
const r = `0x${Buffer.from(signatureEcdsa.r).toString('hex')}`;
|
|
169
|
+
const s = `0x${Buffer.from(signatureEcdsa.s).toString('hex')}`;
|
|
170
|
+
const v = BigInt(signatureEcdsa.v);
|
|
171
|
+
const signedTx = _extends({}, transaction, {
|
|
172
|
+
r: r,
|
|
173
|
+
s: s,
|
|
174
|
+
v: v
|
|
175
|
+
});
|
|
176
|
+
const serializedSignedTx = viem.serializeTransaction(signedTx);
|
|
177
|
+
return serializedSignedTx;
|
|
178
|
+
} catch (error) {
|
|
179
|
+
this.logger.error('Error signing transaction:', error);
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async exportPrivateKey({ accountAddress, password = undefined }) {
|
|
184
|
+
await this.verifyPassword({
|
|
185
|
+
accountAddress,
|
|
186
|
+
password,
|
|
187
|
+
walletOperation: node.WalletOperation.EXPORT_PRIVATE_KEY
|
|
188
|
+
});
|
|
189
|
+
const { derivedPrivateKey } = await this.exportKey({
|
|
190
|
+
accountAddress,
|
|
191
|
+
chainName: this.chainName,
|
|
192
|
+
password
|
|
193
|
+
});
|
|
194
|
+
return {
|
|
195
|
+
derivedPrivateKey
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
async offlineExportPrivateKey({ keyShares, derivationPath }) {
|
|
199
|
+
const { derivedPrivateKey } = await this.offlineExportKey({
|
|
200
|
+
chainName: this.chainName,
|
|
201
|
+
keyShares,
|
|
202
|
+
derivationPath
|
|
203
|
+
});
|
|
204
|
+
return {
|
|
205
|
+
derivedPrivateKey
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
async importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password = undefined, onError }) {
|
|
209
|
+
// TODO: validate private key for EVM
|
|
210
|
+
const { rawPublicKey, externalServerKeyShares } = await this.importRawPrivateKey({
|
|
211
|
+
chainName,
|
|
212
|
+
privateKey,
|
|
213
|
+
thresholdSignatureScheme,
|
|
214
|
+
onError,
|
|
215
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
216
|
+
// update wallet map
|
|
217
|
+
const checksumAddress = viem.getAddress(accountAddress);
|
|
218
|
+
this.walletMap[checksumAddress] = _extends({}, this.walletMap[checksumAddress] || {}, {
|
|
219
|
+
accountAddress: checksumAddress,
|
|
220
|
+
walletId,
|
|
221
|
+
chainName: this.chainName,
|
|
222
|
+
thresholdSignatureScheme,
|
|
223
|
+
externalServerKeySharesBackupInfo: node.getExternalServerKeyShareBackupInfo()
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
if (!rawPublicKey || !externalServerKeyShares) {
|
|
228
|
+
throw new Error('Error creating wallet account');
|
|
229
|
+
}
|
|
230
|
+
const { accountAddress, publicKeyHex } = deriveAccountAddress({
|
|
231
|
+
rawPublicKey: rawPublicKey
|
|
232
|
+
});
|
|
233
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
234
|
+
accountAddress,
|
|
235
|
+
externalServerKeyShares,
|
|
236
|
+
password
|
|
237
|
+
});
|
|
238
|
+
return {
|
|
239
|
+
accountAddress,
|
|
240
|
+
rawPublicKey,
|
|
241
|
+
publicKeyHex,
|
|
242
|
+
externalServerKeyShares
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
async getEvmWallets() {
|
|
246
|
+
const wallets = await this.getWallets();
|
|
247
|
+
const evmWallets = wallets.filter((wallet)=>wallet.chainName === 'eip155');
|
|
248
|
+
return evmWallets;
|
|
249
|
+
}
|
|
250
|
+
constructor({ environmentId, baseApiUrl, baseMPCRelayApiUrl, debug }){
|
|
251
|
+
super({
|
|
252
|
+
environmentId,
|
|
253
|
+
baseApiUrl,
|
|
254
|
+
baseMPCRelayApiUrl,
|
|
255
|
+
debug
|
|
256
|
+
}), this.chainName = 'EVM';
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
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,258 @@
|
|
|
1
|
+
import { MessageHash, DynamicWalletClient, getExternalServerKeyShareBackupInfo, WalletOperation } from '@dynamic-labs-wallet/node';
|
|
2
|
+
import { getAddress, serializeSignature, createPublicClient, http, 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
|
+
const deriveAccountAddress = ({ rawPublicKey })=>{
|
|
35
|
+
const serializedUncompressed = rawPublicKey.serializeUncompressed();
|
|
36
|
+
const firstByteRemoved = serializedUncompressed.slice(1);
|
|
37
|
+
const hashed = MessageHash.keccak256(firstByteRemoved).bytes;
|
|
38
|
+
const lastTwentyBytes = hashed.slice(-20);
|
|
39
|
+
const accountAddress = '0x' + Buffer.from(lastTwentyBytes).toString('hex');
|
|
40
|
+
const publicKeyHex = rawPublicKey.pubKeyAsHex();
|
|
41
|
+
return {
|
|
42
|
+
accountAddress: getAddress(accountAddress),
|
|
43
|
+
publicKeyHex
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
class DynamicEvmWalletClient extends DynamicWalletClient {
|
|
48
|
+
createViemPublicClient({ chain, rpcUrl }) {
|
|
49
|
+
return createPublicClient({
|
|
50
|
+
chain,
|
|
51
|
+
transport: http(rpcUrl)
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
async createWalletAccount({ thresholdSignatureScheme, password = undefined, onError }) {
|
|
55
|
+
try {
|
|
56
|
+
// Generate key shares for given threshold signature scheme (TSS)
|
|
57
|
+
const { rawPublicKey, externalServerKeyShares } = await this.keyGen({
|
|
58
|
+
chainName: this.chainName,
|
|
59
|
+
thresholdSignatureScheme,
|
|
60
|
+
onError,
|
|
61
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
62
|
+
// update wallet map
|
|
63
|
+
const checksumAddress = getAddress(accountAddress);
|
|
64
|
+
this.walletMap[checksumAddress] = _extends({}, this.walletMap[checksumAddress] || {}, {
|
|
65
|
+
accountAddress: checksumAddress,
|
|
66
|
+
walletId,
|
|
67
|
+
chainName: this.chainName,
|
|
68
|
+
thresholdSignatureScheme,
|
|
69
|
+
externalServerKeySharesBackupInfo: getExternalServerKeyShareBackupInfo()
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
if (!rawPublicKey || !externalServerKeyShares) {
|
|
74
|
+
throw new Error(ERROR_KEYGEN_FAILED);
|
|
75
|
+
}
|
|
76
|
+
// Get EVM address from public key
|
|
77
|
+
const { accountAddress, publicKeyHex } = deriveAccountAddress({
|
|
78
|
+
rawPublicKey: rawPublicKey
|
|
79
|
+
});
|
|
80
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
81
|
+
accountAddress,
|
|
82
|
+
externalServerKeyShares,
|
|
83
|
+
password
|
|
84
|
+
});
|
|
85
|
+
return {
|
|
86
|
+
accountAddress,
|
|
87
|
+
rawPublicKey,
|
|
88
|
+
publicKeyHex,
|
|
89
|
+
externalServerKeyShares
|
|
90
|
+
};
|
|
91
|
+
} catch (error) {
|
|
92
|
+
// this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
|
|
93
|
+
throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async signMessage({ message, accountAddress, password = undefined }) {
|
|
97
|
+
await this.verifyPassword({
|
|
98
|
+
accountAddress,
|
|
99
|
+
password,
|
|
100
|
+
walletOperation: WalletOperation.SIGN_MESSAGE
|
|
101
|
+
});
|
|
102
|
+
await this.getWallet({
|
|
103
|
+
accountAddress,
|
|
104
|
+
walletOperation: WalletOperation.SIGN_MESSAGE
|
|
105
|
+
});
|
|
106
|
+
try {
|
|
107
|
+
if (!accountAddress) {
|
|
108
|
+
throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
|
|
109
|
+
}
|
|
110
|
+
// Format the message for EVM signing
|
|
111
|
+
const formattedMessage = formatEVMMessage(message);
|
|
112
|
+
// Sign the message using MPC
|
|
113
|
+
const signatureEcdsa = await this.sign({
|
|
114
|
+
message: formattedMessage,
|
|
115
|
+
accountAddress: accountAddress,
|
|
116
|
+
chainName: this.chainName,
|
|
117
|
+
password
|
|
118
|
+
});
|
|
119
|
+
// Serialize the signature
|
|
120
|
+
const serializedSignature = serializeECDSASignature(signatureEcdsa);
|
|
121
|
+
return serializedSignature;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
this.logger.error(ERROR_SIGN_MESSAGE, error);
|
|
124
|
+
throw new Error(ERROR_SIGN_MESSAGE);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async verifyMessageSignature({ accountAddress, message, signature }) {
|
|
128
|
+
try {
|
|
129
|
+
// Verify the signature using the public client
|
|
130
|
+
const publicClient = this.createViemPublicClient({
|
|
131
|
+
chain: mainnet
|
|
132
|
+
});
|
|
133
|
+
const verified = await publicClient.verifyMessage({
|
|
134
|
+
address: accountAddress,
|
|
135
|
+
message,
|
|
136
|
+
signature: signature
|
|
137
|
+
});
|
|
138
|
+
return verified;
|
|
139
|
+
} catch (error) {
|
|
140
|
+
this.logger.error(ERROR_VERIFY_MESSAGE_SIGNATURE, error);
|
|
141
|
+
throw new Error(ERROR_VERIFY_MESSAGE_SIGNATURE);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async signTransaction({ senderAddress, transaction, password = undefined }) {
|
|
145
|
+
await this.verifyPassword({
|
|
146
|
+
accountAddress: senderAddress,
|
|
147
|
+
password,
|
|
148
|
+
walletOperation: WalletOperation.SIGN_TRANSACTION
|
|
149
|
+
});
|
|
150
|
+
const serializedTx = serializeTransaction(transaction);
|
|
151
|
+
const serializedTxBytes = Uint8Array.from(Buffer.from(serializedTx.slice(2), 'hex'));
|
|
152
|
+
if (!(serializedTxBytes instanceof Uint8Array)) {
|
|
153
|
+
throw new Error('Invalid serializedTxBytes');
|
|
154
|
+
}
|
|
155
|
+
// Get signature using MPC (this will coordinate with server party)
|
|
156
|
+
const signatureEcdsa = await this.sign({
|
|
157
|
+
message: serializedTxBytes,
|
|
158
|
+
accountAddress: senderAddress,
|
|
159
|
+
chainName: this.chainName,
|
|
160
|
+
password
|
|
161
|
+
});
|
|
162
|
+
if (!('r' in signatureEcdsa && 's' in signatureEcdsa && 'v' in signatureEcdsa)) {
|
|
163
|
+
throw new Error('Invalid signature format returned from MPC signing');
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
const r = `0x${Buffer.from(signatureEcdsa.r).toString('hex')}`;
|
|
167
|
+
const s = `0x${Buffer.from(signatureEcdsa.s).toString('hex')}`;
|
|
168
|
+
const v = BigInt(signatureEcdsa.v);
|
|
169
|
+
const signedTx = _extends({}, transaction, {
|
|
170
|
+
r: r,
|
|
171
|
+
s: s,
|
|
172
|
+
v: v
|
|
173
|
+
});
|
|
174
|
+
const serializedSignedTx = serializeTransaction(signedTx);
|
|
175
|
+
return serializedSignedTx;
|
|
176
|
+
} catch (error) {
|
|
177
|
+
this.logger.error('Error signing transaction:', error);
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async exportPrivateKey({ accountAddress, password = undefined }) {
|
|
182
|
+
await this.verifyPassword({
|
|
183
|
+
accountAddress,
|
|
184
|
+
password,
|
|
185
|
+
walletOperation: WalletOperation.EXPORT_PRIVATE_KEY
|
|
186
|
+
});
|
|
187
|
+
const { derivedPrivateKey } = await this.exportKey({
|
|
188
|
+
accountAddress,
|
|
189
|
+
chainName: this.chainName,
|
|
190
|
+
password
|
|
191
|
+
});
|
|
192
|
+
return {
|
|
193
|
+
derivedPrivateKey
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
async offlineExportPrivateKey({ keyShares, derivationPath }) {
|
|
197
|
+
const { derivedPrivateKey } = await this.offlineExportKey({
|
|
198
|
+
chainName: this.chainName,
|
|
199
|
+
keyShares,
|
|
200
|
+
derivationPath
|
|
201
|
+
});
|
|
202
|
+
return {
|
|
203
|
+
derivedPrivateKey
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
async importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password = undefined, onError }) {
|
|
207
|
+
// TODO: validate private key for EVM
|
|
208
|
+
const { rawPublicKey, externalServerKeyShares } = await this.importRawPrivateKey({
|
|
209
|
+
chainName,
|
|
210
|
+
privateKey,
|
|
211
|
+
thresholdSignatureScheme,
|
|
212
|
+
onError,
|
|
213
|
+
onCeremonyComplete: (accountAddress, walletId)=>{
|
|
214
|
+
// update wallet map
|
|
215
|
+
const checksumAddress = getAddress(accountAddress);
|
|
216
|
+
this.walletMap[checksumAddress] = _extends({}, this.walletMap[checksumAddress] || {}, {
|
|
217
|
+
accountAddress: checksumAddress,
|
|
218
|
+
walletId,
|
|
219
|
+
chainName: this.chainName,
|
|
220
|
+
thresholdSignatureScheme,
|
|
221
|
+
externalServerKeySharesBackupInfo: getExternalServerKeyShareBackupInfo()
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
if (!rawPublicKey || !externalServerKeyShares) {
|
|
226
|
+
throw new Error('Error creating wallet account');
|
|
227
|
+
}
|
|
228
|
+
const { accountAddress, publicKeyHex } = deriveAccountAddress({
|
|
229
|
+
rawPublicKey: rawPublicKey
|
|
230
|
+
});
|
|
231
|
+
void this.storeEncryptedBackupByWalletWithRetry({
|
|
232
|
+
accountAddress,
|
|
233
|
+
externalServerKeyShares,
|
|
234
|
+
password
|
|
235
|
+
});
|
|
236
|
+
return {
|
|
237
|
+
accountAddress,
|
|
238
|
+
rawPublicKey,
|
|
239
|
+
publicKeyHex,
|
|
240
|
+
externalServerKeyShares
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
async getEvmWallets() {
|
|
244
|
+
const wallets = await this.getWallets();
|
|
245
|
+
const evmWallets = wallets.filter((wallet)=>wallet.chainName === 'eip155');
|
|
246
|
+
return evmWallets;
|
|
247
|
+
}
|
|
248
|
+
constructor({ environmentId, baseApiUrl, baseMPCRelayApiUrl, debug }){
|
|
249
|
+
super({
|
|
250
|
+
environmentId,
|
|
251
|
+
baseApiUrl,
|
|
252
|
+
baseMPCRelayApiUrl,
|
|
253
|
+
debug
|
|
254
|
+
}), this.chainName = 'EVM';
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export { DynamicEvmWalletClient };
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dynamic-labs-wallet/node-evm",
|
|
3
|
+
"version": "0.0.48",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@dynamic-labs-wallet/node": "0.0.48"
|
|
7
|
+
},
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"viem": "^2.22.1"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"typescript": "^5.0.0",
|
|
16
|
+
"@types/node": "^20.0.0"
|
|
17
|
+
},
|
|
18
|
+
"nx": {
|
|
19
|
+
"sourceRoot": "packages/node-evm/src",
|
|
20
|
+
"projectType": "library",
|
|
21
|
+
"name": "node-evm",
|
|
22
|
+
"targets": {
|
|
23
|
+
"build": {}
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "./index.cjs.js",
|
|
28
|
+
"module": "./index.esm.js",
|
|
29
|
+
"types": "./index.esm.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
"./package.json": "./package.json",
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./index.esm.d.ts",
|
|
34
|
+
"import": "./index.esm.js",
|
|
35
|
+
"require": "./index.cjs.js",
|
|
36
|
+
"default": "./index.cjs.js"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { ServerKeyShare, DynamicWalletClient, EcdsaKeygenResult, EcdsaPublicKey, Ed25519KeygenResult, ThresholdSignatureScheme, DynamicWalletClientProps } from '@dynamic-labs-wallet/node';
|
|
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, baseApiUrl, baseMPCRelayApiUrl, 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
|
+
externalServerKeyShares: ServerKeyShare[];
|
|
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
|
+
exportPrivateKey({ accountAddress, password, }: {
|
|
36
|
+
accountAddress: string;
|
|
37
|
+
password?: string;
|
|
38
|
+
}): Promise<{
|
|
39
|
+
derivedPrivateKey: string | undefined;
|
|
40
|
+
}>;
|
|
41
|
+
offlineExportPrivateKey({ keyShares, derivationPath, }: {
|
|
42
|
+
keyShares: (EcdsaKeygenResult | Ed25519KeygenResult)[];
|
|
43
|
+
derivationPath?: string;
|
|
44
|
+
}): Promise<{
|
|
45
|
+
derivedPrivateKey: string | undefined;
|
|
46
|
+
}>;
|
|
47
|
+
importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password, onError, }: {
|
|
48
|
+
privateKey: string;
|
|
49
|
+
chainName: string;
|
|
50
|
+
thresholdSignatureScheme: ThresholdSignatureScheme;
|
|
51
|
+
password?: string;
|
|
52
|
+
onError?: (error: Error) => void;
|
|
53
|
+
}): Promise<{
|
|
54
|
+
accountAddress: string;
|
|
55
|
+
publicKeyHex: string;
|
|
56
|
+
rawPublicKey: EcdsaPublicKey | Uint8Array | undefined;
|
|
57
|
+
externalServerKeyShares: ServerKeyShare[];
|
|
58
|
+
}>;
|
|
59
|
+
getEvmWallets(): Promise<any>;
|
|
60
|
+
}
|
|
61
|
+
//# 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,iBAAiB,EACjB,cAAc,EAEd,mBAAmB,EACnB,wBAAwB,EACxB,wBAAwB,EAGzB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAGL,KAAK,YAAY,EACjB,KAAK,KAAK,EAEV,KAAK,eAAe,EACpB,KAAK,uBAAuB,EAE7B,MAAM,MAAM,CAAC;AAed,qBAAa,sBAAuB,SAAQ,mBAAmB;IAC7D,QAAQ,CAAC,SAAS,SAAS;gBAEf,EACV,aAAa,EACb,UAAU,EACV,kBAAkB,EAClB,KAAK,GACN,EAAE,wBAAwB;IAS3B,sBAAsB,CAAC,EACrB,KAAK,EACL,MAAM,GACP,EAAE;QACD,KAAK,EAAE,KAAK,CAAC;QACb,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,GAAG,YAAY;IAOV,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,MAAM,CAAC;QACrB,YAAY,EAAE,cAAc,GAAG,UAAU,GAAG,SAAS,CAAC;QACtD,uBAAuB,EAAE,cAAc,EAAE,CAAC;KAC3C,CAAC;IAiDI,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;IAqCK,sBAAsB,CAAC,EAC3B,cAAc,EACd,OAAO,EACP,SAAS,GACV,EAAE;QACD,cAAc,EAAE,MAAM,CAAC;QACvB,OAAO,EAAE,eAAe,CAAC;QACzB,SAAS,EAAE,GAAG,CAAC;KAChB;IAmBK,eAAe,CAAC,EACpB,aAAa,EACb,WAAW,EACX,QAAoB,GACrB,EAAE;QACD,aAAa,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,uBAAuB,CAAC;QACrC,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC,MAAM,CAAC;IAmDb,gBAAgB,CAAC,EACrB,cAAc,EACd,QAAoB,GACrB,EAAE;QACD,cAAc,EAAE,MAAM,CAAC;QACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB;;;IAeK,uBAAuB,CAAC,EAC5B,SAAS,EACT,cAAc,GACf,EAAE;QACD,SAAS,EAAE,CAAC,iBAAiB,GAAG,mBAAmB,CAAC,EAAE,CAAC;QACvD,cAAc,CAAC,EAAE,MAAM,CAAC;KACzB;;;IASK,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,MAAM,CAAC;QACrB,YAAY,EAAE,cAAc,GAAG,UAAU,GAAG,SAAS,CAAC;QACtD,uBAAuB,EAAE,cAAc,EAAE,CAAC;KAC3C,CAAC;IA4CI,aAAa;CAOpB"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const EVM_SIGN_MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\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,sCAAsC,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,10 @@
|
|
|
1
|
+
import { EcdsaSignature, EcdsaPublicKey } from '@dynamic-labs-wallet/node';
|
|
2
|
+
export declare const formatEVMMessage: (message: string) => string;
|
|
3
|
+
export declare const serializeECDSASignature: (signature: EcdsaSignature) => `0x${string}`;
|
|
4
|
+
export declare const deriveAccountAddress: ({ rawPublicKey, }: {
|
|
5
|
+
rawPublicKey: EcdsaPublicKey;
|
|
6
|
+
}) => {
|
|
7
|
+
accountAddress: `0x${string}`;
|
|
8
|
+
publicKeyHex: any;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../packages/src/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,cAAc,EACd,cAAc,EAEf,MAAM,2BAA2B,CAAC;AAEnC,eAAO,MAAM,gBAAgB,YAAa,MAAM,WAE/C,CAAC;AAEF,eAAO,MAAM,uBAAuB,cAAe,cAAc,kBAMhE,CAAC;AAEF,eAAO,MAAM,oBAAoB,sBAE9B;IACD,YAAY,EAAE,cAAc,CAAC;CAC9B;;;CAQA,CAAC"}
|