@dfns/lib-viem 0.1.2

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 +230 -0
  2. package/index.js +85 -0
  3. package/package.json +15 -0
package/index.d.ts ADDED
@@ -0,0 +1,230 @@
1
+ import { DfnsApiClient } from '@dfns/sdk';
2
+ import { Address, SignableMessage, Hash, TypedDataDefinition, TransactionSerializable, SerializeTransactionFn } from 'viem';
3
+ export type DfnsWalletOptions = {
4
+ walletId: string;
5
+ dfnsClient: DfnsApiClient;
6
+ maxRetries?: number;
7
+ retryInterval?: number;
8
+ };
9
+ export declare class DfnsWallet {
10
+ address: Address;
11
+ private options;
12
+ constructor(address: Address, options: DfnsWalletOptions);
13
+ static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
14
+ private waitForSignature;
15
+ private signHash;
16
+ signMessage: ({ message }: {
17
+ 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
+ } | {
228
+ [key: string]: unknown;
229
+ }, TPrimaryType extends string = string>(typedData: TypedDataDefinition<TTypedData, TPrimaryType>) => Promise<Hash>;
230
+ }
package/index.js ADDED
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DfnsWallet = void 0;
4
+ const Wallets_1 = require("@dfns/sdk/codegen/datamodel/Wallets");
5
+ const viem_1 = require("viem");
6
+ const accounts_1 = require("viem/accounts");
7
+ const sleep = (interval = 0) => new Promise((resolve) => setTimeout(resolve, interval));
8
+ 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
+ };
37
+ }
38
+ static async init(options) {
39
+ const { walletId, dfnsClient } = options;
40
+ 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})`);
43
+ }
44
+ let address;
45
+ if (res.address) {
46
+ address = (0, viem_1.getAddress)(res.address);
47
+ }
48
+ else {
49
+ address = (0, accounts_1.publicKeyToAddress)((0, viem_1.toHex)(res.signingKey.publicKey));
50
+ }
51
+ return new DfnsWallet(address, options);
52
+ }
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;
72
+ }
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
+ }
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);
83
+ }
84
+ }
85
+ exports.DfnsWallet = DfnsWallet;
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@dfns/lib-viem",
3
+ "version": "0.1.2",
4
+ "dependencies": {
5
+ "buffer": "6.0.3",
6
+ "cross-fetch": "3.1.6",
7
+ "uuid": "9.0.0",
8
+ "viem": "^1.19.3"
9
+ },
10
+ "peerDependencies": {
11
+ "@dfns/sdk": "0.1.2"
12
+ },
13
+ "main": "./index.js",
14
+ "type": "commonjs"
15
+ }