@dfns/lib-viem 0.1.2 → 0.1.3-rc.1

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.
Files changed (3) hide show
  1. package/index.d.ts +19 -217
  2. package/index.js +90 -65
  3. package/package.json +3 -3
package/index.d.ts CHANGED
@@ -1,230 +1,32 @@
1
1
  import { DfnsApiClient } from '@dfns/sdk';
2
- import { Address, SignableMessage, Hash, TypedDataDefinition, TransactionSerializable, SerializeTransactionFn } from 'viem';
2
+ import { GetWalletResponse } from '@dfns/sdk/types/wallets';
3
+ import { Address, GetTransactionType, Hash, SerializeTransactionFn, SignableMessage, TransactionSerializable, TransactionSerialized, TypedData, TypedDataDefinition } from 'viem';
3
4
  export type DfnsWalletOptions = {
4
5
  walletId: string;
5
6
  dfnsClient: DfnsApiClient;
7
+ /** @deprecated transaction signing is now synchronous. polling is deprecated. */
6
8
  maxRetries?: number;
9
+ /** @deprecated transaction signing is now synchronous. polling is deprecated. */
7
10
  retryInterval?: number;
8
11
  };
12
+ type WalletMetadata = GetWalletResponse & {
13
+ boundToEvmNetwork: boolean;
14
+ };
9
15
  export declare class DfnsWallet {
10
- address: Address;
11
- private options;
12
- constructor(address: Address, options: DfnsWalletOptions);
16
+ private metadata;
17
+ readonly address: Address;
18
+ private readonly dfnsClient;
19
+ constructor(metadata: WalletMetadata, options: DfnsWalletOptions);
13
20
  static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
14
- private waitForSignature;
15
21
  private signHash;
16
- signMessage: ({ message }: {
22
+ signMessage({ message }: {
17
23
  message: SignableMessage;
18
- }) => Promise<Hash>;
19
- signTransaction: <TTransactionSerializable extends TransactionSerializable>(transaction: TTransactionSerializable, args?: {
20
- serializer?: SerializeTransactionFn<TTransactionSerializable> | undefined;
21
- } | undefined) => Promise<Hash>;
22
- signTypedData: <const TTypedData extends {
23
- [x: string]: readonly import("viem").TypedDataParameter[];
24
- [x: `string[${string}]`]: undefined;
25
- [x: `function[${string}]`]: undefined;
26
- [x: `address[${string}]`]: undefined;
27
- [x: `bool[${string}]`]: undefined;
28
- [x: `bytes[${string}]`]: undefined;
29
- [x: `bytes4[${string}]`]: undefined;
30
- [x: `bytes2[${string}]`]: undefined;
31
- [x: `bytes1[${string}]`]: undefined;
32
- [x: `bytes6[${string}]`]: undefined;
33
- [x: `bytes18[${string}]`]: undefined;
34
- [x: `bytes8[${string}]`]: undefined;
35
- [x: `bytes24[${string}]`]: undefined;
36
- [x: `bytes10[${string}]`]: undefined;
37
- [x: `bytes12[${string}]`]: undefined;
38
- [x: `bytes7[${string}]`]: undefined;
39
- [x: `bytes9[${string}]`]: undefined;
40
- [x: `bytes11[${string}]`]: undefined;
41
- [x: `bytes5[${string}]`]: undefined;
42
- [x: `bytes3[${string}]`]: undefined;
43
- [x: `bytes17[${string}]`]: undefined;
44
- [x: `bytes16[${string}]`]: undefined;
45
- [x: `bytes15[${string}]`]: undefined;
46
- [x: `bytes14[${string}]`]: undefined;
47
- [x: `bytes13[${string}]`]: undefined;
48
- [x: `bytes19[${string}]`]: undefined;
49
- [x: `bytes20[${string}]`]: undefined;
50
- [x: `bytes21[${string}]`]: undefined;
51
- [x: `bytes22[${string}]`]: undefined;
52
- [x: `bytes23[${string}]`]: undefined;
53
- [x: `bytes25[${string}]`]: undefined;
54
- [x: `bytes26[${string}]`]: undefined;
55
- [x: `bytes27[${string}]`]: undefined;
56
- [x: `bytes28[${string}]`]: undefined;
57
- [x: `bytes29[${string}]`]: undefined;
58
- [x: `bytes30[${string}]`]: undefined;
59
- [x: `bytes31[${string}]`]: undefined;
60
- [x: `bytes32[${string}]`]: undefined;
61
- [x: `int[${string}]`]: undefined;
62
- [x: `int8[${string}]`]: undefined;
63
- [x: `int24[${string}]`]: undefined;
64
- [x: `int16[${string}]`]: undefined;
65
- [x: `int32[${string}]`]: undefined;
66
- [x: `int40[${string}]`]: undefined;
67
- [x: `int48[${string}]`]: undefined;
68
- [x: `int56[${string}]`]: undefined;
69
- [x: `int64[${string}]`]: undefined;
70
- [x: `int72[${string}]`]: undefined;
71
- [x: `int80[${string}]`]: undefined;
72
- [x: `int88[${string}]`]: undefined;
73
- [x: `int96[${string}]`]: undefined;
74
- [x: `int104[${string}]`]: undefined;
75
- [x: `int112[${string}]`]: undefined;
76
- [x: `int120[${string}]`]: undefined;
77
- [x: `int128[${string}]`]: undefined;
78
- [x: `int136[${string}]`]: undefined;
79
- [x: `int144[${string}]`]: undefined;
80
- [x: `int152[${string}]`]: undefined;
81
- [x: `int160[${string}]`]: undefined;
82
- [x: `int168[${string}]`]: undefined;
83
- [x: `int176[${string}]`]: undefined;
84
- [x: `int184[${string}]`]: undefined;
85
- [x: `int192[${string}]`]: undefined;
86
- [x: `int200[${string}]`]: undefined;
87
- [x: `int208[${string}]`]: undefined;
88
- [x: `int216[${string}]`]: undefined;
89
- [x: `int224[${string}]`]: undefined;
90
- [x: `int232[${string}]`]: undefined;
91
- [x: `int240[${string}]`]: undefined;
92
- [x: `int248[${string}]`]: undefined;
93
- [x: `int256[${string}]`]: undefined;
94
- [x: `uint[${string}]`]: undefined;
95
- [x: `uint8[${string}]`]: undefined;
96
- [x: `uint24[${string}]`]: undefined;
97
- [x: `uint16[${string}]`]: undefined;
98
- [x: `uint32[${string}]`]: undefined;
99
- [x: `uint40[${string}]`]: undefined;
100
- [x: `uint48[${string}]`]: undefined;
101
- [x: `uint56[${string}]`]: undefined;
102
- [x: `uint64[${string}]`]: undefined;
103
- [x: `uint72[${string}]`]: undefined;
104
- [x: `uint80[${string}]`]: undefined;
105
- [x: `uint88[${string}]`]: undefined;
106
- [x: `uint96[${string}]`]: undefined;
107
- [x: `uint104[${string}]`]: undefined;
108
- [x: `uint112[${string}]`]: undefined;
109
- [x: `uint120[${string}]`]: undefined;
110
- [x: `uint128[${string}]`]: undefined;
111
- [x: `uint136[${string}]`]: undefined;
112
- [x: `uint144[${string}]`]: undefined;
113
- [x: `uint152[${string}]`]: undefined;
114
- [x: `uint160[${string}]`]: undefined;
115
- [x: `uint168[${string}]`]: undefined;
116
- [x: `uint176[${string}]`]: undefined;
117
- [x: `uint184[${string}]`]: undefined;
118
- [x: `uint192[${string}]`]: undefined;
119
- [x: `uint200[${string}]`]: undefined;
120
- [x: `uint208[${string}]`]: undefined;
121
- [x: `uint216[${string}]`]: undefined;
122
- [x: `uint224[${string}]`]: undefined;
123
- [x: `uint232[${string}]`]: undefined;
124
- [x: `uint240[${string}]`]: undefined;
125
- [x: `uint248[${string}]`]: undefined;
126
- [x: `uint256[${string}]`]: undefined;
127
- string?: undefined;
128
- address?: undefined;
129
- bool?: undefined;
130
- bytes?: undefined;
131
- bytes4?: undefined;
132
- bytes2?: undefined;
133
- bytes1?: undefined;
134
- bytes6?: undefined;
135
- bytes18?: undefined;
136
- bytes8?: undefined;
137
- bytes24?: undefined;
138
- bytes10?: undefined;
139
- bytes12?: undefined;
140
- bytes7?: undefined;
141
- bytes9?: undefined;
142
- bytes11?: undefined;
143
- bytes5?: undefined;
144
- bytes3?: undefined;
145
- bytes17?: undefined;
146
- bytes16?: undefined;
147
- bytes15?: undefined;
148
- bytes14?: undefined;
149
- bytes13?: undefined;
150
- bytes19?: undefined;
151
- bytes20?: undefined;
152
- bytes21?: undefined;
153
- bytes22?: undefined;
154
- bytes23?: undefined;
155
- bytes25?: undefined;
156
- bytes26?: undefined;
157
- bytes27?: undefined;
158
- bytes28?: undefined;
159
- bytes29?: undefined;
160
- bytes30?: undefined;
161
- bytes31?: undefined;
162
- bytes32?: undefined;
163
- int8?: undefined;
164
- int24?: undefined;
165
- int16?: undefined;
166
- int32?: undefined;
167
- int40?: undefined;
168
- int48?: undefined;
169
- int56?: undefined;
170
- int64?: undefined;
171
- int72?: undefined;
172
- int80?: undefined;
173
- int88?: undefined;
174
- int96?: undefined;
175
- int104?: undefined;
176
- int112?: undefined;
177
- int120?: undefined;
178
- int128?: undefined;
179
- int136?: undefined;
180
- int144?: undefined;
181
- int152?: undefined;
182
- int160?: undefined;
183
- int168?: undefined;
184
- int176?: undefined;
185
- int184?: undefined;
186
- int192?: undefined;
187
- int200?: undefined;
188
- int208?: undefined;
189
- int216?: undefined;
190
- int224?: undefined;
191
- int232?: undefined;
192
- int240?: undefined;
193
- int248?: undefined;
194
- int256?: undefined;
195
- uint8?: undefined;
196
- uint24?: undefined;
197
- uint16?: undefined;
198
- uint32?: undefined;
199
- uint40?: undefined;
200
- uint48?: undefined;
201
- uint56?: undefined;
202
- uint64?: undefined;
203
- uint72?: undefined;
204
- uint80?: undefined;
205
- uint88?: undefined;
206
- uint96?: undefined;
207
- uint104?: undefined;
208
- uint112?: undefined;
209
- uint120?: undefined;
210
- uint128?: undefined;
211
- uint136?: undefined;
212
- uint144?: undefined;
213
- uint152?: undefined;
214
- uint160?: undefined;
215
- uint168?: undefined;
216
- uint176?: undefined;
217
- uint184?: undefined;
218
- uint192?: undefined;
219
- uint200?: undefined;
220
- uint208?: undefined;
221
- uint216?: undefined;
222
- uint224?: undefined;
223
- uint232?: undefined;
224
- uint240?: undefined;
225
- uint248?: undefined;
226
- uint256?: undefined;
227
- } | {
24
+ }): Promise<Hash>;
25
+ signTransaction<TTransactionSerializable extends TransactionSerializable>(transaction: TTransactionSerializable, args?: {
26
+ serializer?: SerializeTransactionFn<TTransactionSerializable>;
27
+ }): Promise<TransactionSerialized<GetTransactionType<TTransactionSerializable>>>;
28
+ signTypedData<const TTypedData extends TypedData | {
228
29
  [key: string]: unknown;
229
- }, TPrimaryType extends string = string>(typedData: TypedDataDefinition<TTypedData, TPrimaryType>) => Promise<Hash>;
30
+ }, TPrimaryType extends string = string>(typedData: TypedDataDefinition<TTypedData, TPrimaryType>): Promise<Hash>;
230
31
  }
32
+ export {};
package/index.js CHANGED
@@ -1,85 +1,110 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DfnsWallet = void 0;
4
- const Wallets_1 = require("@dfns/sdk/codegen/datamodel/Wallets");
4
+ const sdk_1 = require("@dfns/sdk");
5
5
  const viem_1 = require("viem");
6
6
  const accounts_1 = require("viem/accounts");
7
- const sleep = (interval = 0) => new Promise((resolve) => setTimeout(resolve, interval));
7
+ const assertSigned = (res) => {
8
+ if (res.status === 'Failed') {
9
+ throw new sdk_1.DfnsError(-1, 'signing failed', res);
10
+ }
11
+ else if (res.status !== 'Signed') {
12
+ throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
13
+ }
14
+ };
15
+ const extractSignature = (res) => {
16
+ if (!res.signature) {
17
+ throw new sdk_1.DfnsError(-1, 'signature missing', res);
18
+ }
19
+ return {
20
+ r: res.signature.r,
21
+ s: res.signature.s,
22
+ v: res.signature.recid ? 28n : 27n,
23
+ };
24
+ };
25
+ const boundToEvmNetwork = (wallet) => {
26
+ // if the address is evm format, it's a wallet bound to evm network. prefer to
27
+ // sign the full transaction instead of the hash of the transaction
28
+ return wallet.address ? !!wallet.address.match(/^0x[0-9a-fA-F]{40}$/) : false;
29
+ };
8
30
  class DfnsWallet {
9
- constructor(address, options) {
10
- // must use the arrow syntax to bind `this` to all public methods, otherwise, after
11
- // viem's `toAccount`, the returned LocalAccount will not be able to resolve `this`
12
- // on method calls to `signMessage` and `signTypedData`
13
- this.signMessage = async ({ message }) => {
14
- const signature = await this.signHash((0, viem_1.hashMessage)(message));
15
- return (0, viem_1.signatureToHex)(signature);
16
- };
17
- this.signTransaction = async (transaction, args) => {
18
- let serializer = args?.serializer;
19
- if (!serializer) {
20
- serializer = viem_1.serializeTransaction;
21
- }
22
- const hash = (0, viem_1.keccak256)(serializer(transaction));
23
- const signature = await this.signHash(hash);
24
- return serializer(transaction, signature);
25
- };
26
- this.signTypedData = async (typedData) => {
27
- const hash = (0, viem_1.hashTypedData)(typedData);
28
- const signature = await this.signHash(hash);
29
- return (0, viem_1.signatureToHex)(signature);
30
- };
31
- this.address = address;
32
- this.options = {
33
- ...options,
34
- maxRetries: options.maxRetries ?? 3,
35
- retryInterval: options.retryInterval ?? 1000,
36
- };
31
+ constructor(metadata, options) {
32
+ this.metadata = metadata;
33
+ this.address = this.metadata.boundToEvmNetwork
34
+ ? (0, viem_1.getAddress)(this.metadata.address)
35
+ : (0, accounts_1.publicKeyToAddress)((0, viem_1.toHex)(this.metadata.signingKey.publicKey));
36
+ this.dfnsClient = options.dfnsClient;
37
+ // must scope these three functions. otherwise the `LocalAccount` returned with
38
+ // `toAccount` will not be able to resolve `this` when calling sign functions
39
+ this.signMessage = this.signMessage.bind(this);
40
+ this.signTransaction = this.signTransaction.bind(this);
41
+ this.signTypedData = this.signTypedData.bind(this);
37
42
  }
38
43
  static async init(options) {
39
44
  const { walletId, dfnsClient } = options;
40
45
  const res = await dfnsClient.wallets.getWallet({ walletId });
41
- if (!res.signingKey || res.signingKey.scheme !== Wallets_1.KeyScheme.ECDSA || res.signingKey.curve !== Wallets_1.KeyCurve.secp256k1) {
42
- throw new Error(`wallet ${walletId} has incompatible scheme (${res.signingKey?.scheme}) or curve (${res.signingKey?.curve})`);
46
+ if (res.status !== 'Active') {
47
+ throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
48
+ }
49
+ const { scheme, curve } = res.signingKey;
50
+ if (scheme !== 'ECDSA') {
51
+ throw new sdk_1.DfnsError(-1, 'key scheme is not ECDSA', { walletId, scheme });
43
52
  }
44
- let address;
45
- if (res.address) {
46
- address = (0, viem_1.getAddress)(res.address);
53
+ if (curve !== 'secp256k1') {
54
+ throw new sdk_1.DfnsError(-1, 'key curve is not secp256k1', { walletId, curve });
55
+ }
56
+ const metadata = {
57
+ boundToEvmNetwork: boundToEvmNetwork(res),
58
+ ...res,
59
+ };
60
+ return new DfnsWallet(metadata, options);
61
+ }
62
+ async signHash(hash) {
63
+ const res = await this.dfnsClient.wallets.generateSignature({
64
+ walletId: this.metadata.id,
65
+ body: { kind: 'Hash', hash },
66
+ });
67
+ assertSigned(res);
68
+ return extractSignature(res);
69
+ }
70
+ async signMessage({ message }) {
71
+ if (this.metadata.boundToEvmNetwork) {
72
+ const hex = typeof message === 'string'
73
+ ? (0, viem_1.stringToHex)(message)
74
+ : message.raw instanceof Uint8Array
75
+ ? (0, viem_1.bytesToHex)(message.raw)
76
+ : message.raw;
77
+ const res = await this.dfnsClient.wallets.generateSignature({
78
+ walletId: this.metadata.id,
79
+ body: { kind: 'Message', message: hex },
80
+ });
81
+ assertSigned(res);
82
+ return (0, viem_1.signatureToHex)(extractSignature(res));
47
83
  }
48
84
  else {
49
- address = (0, accounts_1.publicKeyToAddress)((0, viem_1.toHex)(res.signingKey.publicKey));
85
+ return (0, viem_1.signatureToHex)(await this.signHash((0, viem_1.hashMessage)(message)));
50
86
  }
51
- return new DfnsWallet(address, options);
52
87
  }
53
- async waitForSignature(signatureId) {
54
- const { walletId, dfnsClient, retryInterval } = this.options;
55
- let maxRetries = this.options.maxRetries;
56
- while (maxRetries > 0) {
57
- await sleep(retryInterval);
58
- const res = await dfnsClient.wallets.getSignature({ walletId, signatureId });
59
- if (res.status === Wallets_1.SignatureStatus.Signed) {
60
- if (!res.signature)
61
- break;
62
- return {
63
- r: res.signature.r,
64
- s: res.signature.s,
65
- v: res.signature.recid ? 28n : 27n,
66
- };
67
- }
68
- else if (res.status === Wallets_1.SignatureStatus.Failed) {
69
- break;
70
- }
71
- maxRetries -= 1;
88
+ async signTransaction(transaction, args) {
89
+ const serializer = args?.serializer ?? viem_1.serializeTransaction;
90
+ if (this.metadata.boundToEvmNetwork) {
91
+ const res = await this.dfnsClient.wallets.generateSignature({
92
+ walletId: this.metadata.id,
93
+ body: { kind: 'Transaction', transaction: serializer(transaction) },
94
+ });
95
+ assertSigned(res);
96
+ return serializer(transaction, extractSignature(res));
97
+ }
98
+ else {
99
+ const hash = (0, viem_1.keccak256)(serializer(transaction));
100
+ const signature = await this.signHash(hash);
101
+ return serializer(transaction, signature);
72
102
  }
73
- const waitedSeconds = Math.floor((this.options.maxRetries * retryInterval) / 1000);
74
- throw new Error(`Signature request ${signatureId} took more than ${waitedSeconds}s to complete, stopping polling. Please update options "maxRetries" or "retryIntervals" to wait longer.`);
75
103
  }
76
- async signHash(hash) {
77
- const { walletId, dfnsClient } = this.options;
78
- const res = await dfnsClient.wallets.generateSignature({
79
- walletId,
80
- body: { kind: Wallets_1.SignatureKind.Hash, hash },
81
- });
82
- return this.waitForSignature(res.id);
104
+ async signTypedData(typedData) {
105
+ const hash = (0, viem_1.hashTypedData)(typedData);
106
+ const signature = await this.signHash(hash);
107
+ return (0, viem_1.signatureToHex)(signature);
83
108
  }
84
109
  }
85
110
  exports.DfnsWallet = DfnsWallet;
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@dfns/lib-viem",
3
- "version": "0.1.2",
3
+ "version": "0.1.3-rc.1",
4
4
  "dependencies": {
5
5
  "buffer": "6.0.3",
6
6
  "cross-fetch": "3.1.6",
7
7
  "uuid": "9.0.0",
8
- "viem": "^1.19.3"
8
+ "viem": "1.19.11"
9
9
  },
10
10
  "peerDependencies": {
11
- "@dfns/sdk": "0.1.2"
11
+ "@dfns/sdk": "0.1.3-rc.1"
12
12
  },
13
13
  "main": "./index.js",
14
14
  "type": "commonjs"