@frequency-chain/ethereum-utils 0.0.0-fe548b

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.
@@ -0,0 +1,541 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sign = sign;
4
+ exports.verifySignature = verifySignature;
5
+ exports.createAddKeyData = createAddKeyData;
6
+ exports.createAuthorizedKeyData = createAuthorizedKeyData;
7
+ exports.createAddProvider = createAddProvider;
8
+ exports.createRecoveryCommitmentPayload = createRecoveryCommitmentPayload;
9
+ exports.createClaimHandlePayload = createClaimHandlePayload;
10
+ exports.createPasskeyPublicKey = createPasskeyPublicKey;
11
+ exports.createItemizedAddAction = createItemizedAddAction;
12
+ exports.createItemizedDeleteAction = createItemizedDeleteAction;
13
+ exports.createItemizedSignaturePayloadV2 = createItemizedSignaturePayloadV2;
14
+ exports.createPaginatedDeleteSignaturePayloadV2 = createPaginatedDeleteSignaturePayloadV2;
15
+ exports.createPaginatedUpsertSignaturePayloadV2 = createPaginatedUpsertSignaturePayloadV2;
16
+ exports.createSiwfSignedRequestPayload = createSiwfSignedRequestPayload;
17
+ exports.createSiwfLoginRequestPayload = createSiwfLoginRequestPayload;
18
+ exports.getEip712BrowserRequestAddKeyData = getEip712BrowserRequestAddKeyData;
19
+ exports.getEip712BrowserRequestAuthorizedKeyData = getEip712BrowserRequestAuthorizedKeyData;
20
+ exports.getEip712BrowserRequestAddProvider = getEip712BrowserRequestAddProvider;
21
+ exports.getEip712BrowserRequestRecoveryCommitmentPayload = getEip712BrowserRequestRecoveryCommitmentPayload;
22
+ exports.getEip712BrowserRequestPaginatedUpsertSignaturePayloadV2 = getEip712BrowserRequestPaginatedUpsertSignaturePayloadV2;
23
+ exports.getEip712BrowserRequestPaginatedDeleteSignaturePayloadV2 = getEip712BrowserRequestPaginatedDeleteSignaturePayloadV2;
24
+ exports.getEip712BrowserRequestItemizedSignaturePayloadV2 = getEip712BrowserRequestItemizedSignaturePayloadV2;
25
+ exports.getEip712BrowserRequestClaimHandlePayload = getEip712BrowserRequestClaimHandlePayload;
26
+ exports.getEip712BrowserRequestPasskeyPublicKey = getEip712BrowserRequestPasskeyPublicKey;
27
+ exports.getEip712BrowserRequestSiwfSignedRequestPayload = getEip712BrowserRequestSiwfSignedRequestPayload;
28
+ exports.getEthereumRegularSigner = getEthereumRegularSigner;
29
+ exports.getEthereumMessageSigner = getEthereumMessageSigner;
30
+ const utils_js_1 = require("./utils.js");
31
+ const address_js_1 = require("./address.js");
32
+ const ethers_1 = require("ethers");
33
+ const util_1 = require("@polkadot/util");
34
+ const signature_definitions_js_1 = require("./signature.definitions.js");
35
+ /**
36
+ * Signing EIP-712 or ERC-191 compatible signature based on payload
37
+ * @param secretKey
38
+ * @param payload
39
+ * @param chain
40
+ */
41
+ async function sign(secretKey, payload, chain) {
42
+ const signatureType = getSignatureType(payload.type);
43
+ const normalizedPayload = normalizePayload(payload);
44
+ const wallet = new ethers_1.ethers.Wallet(secretKey);
45
+ let signature;
46
+ switch (signatureType) {
47
+ case 'EIP-712':
48
+ // TODO: use correct contract address for different payloads
49
+ signature = await wallet.signTypedData(chain === 'Mainnet-Frequency' ? signature_definitions_js_1.EIP712_DOMAIN_MAINNET : signature_definitions_js_1.EIP712_DOMAIN_TESTNET, getTypesFor(payload.type), normalizedPayload);
50
+ break;
51
+ case 'EIP-191':
52
+ signature = await wallet.signMessage(payload.message);
53
+ break;
54
+ default:
55
+ throw new Error(`Unsupported signature type : ${signatureType}`);
56
+ }
57
+ return { Ecdsa: signature };
58
+ }
59
+ /**
60
+ * Verify EIP-712 and ERC-191 signatures based on payload
61
+ * @param ethereumAddress
62
+ * @param signature
63
+ * @param payload
64
+ * @param chain
65
+ */
66
+ function verifySignature(ethereumAddress, signature, payload, chain) {
67
+ const signatureType = getSignatureType(payload.type);
68
+ const normalizedPayload = normalizePayload(payload);
69
+ let recoveredAddress;
70
+ switch (signatureType) {
71
+ case 'EIP-712':
72
+ // TODO: use correct contract address for different payloads
73
+ recoveredAddress = ethers_1.ethers.verifyTypedData(chain === 'Mainnet-Frequency' ? signature_definitions_js_1.EIP712_DOMAIN_MAINNET : signature_definitions_js_1.EIP712_DOMAIN_TESTNET, getTypesFor(payload.type), normalizedPayload, signature);
74
+ break;
75
+ case 'EIP-191':
76
+ recoveredAddress = ethers_1.ethers.verifyMessage(payload.message, signature);
77
+ break;
78
+ default:
79
+ throw new Error(`Unsupported signature type : ${signatureType}`);
80
+ }
81
+ return recoveredAddress.toLowerCase() === ethereumAddress.toLowerCase();
82
+ }
83
+ function normalizePayload(payload) {
84
+ const clonedPayload = Object.assign({}, payload);
85
+ switch (clonedPayload.type) {
86
+ case 'PaginatedUpsertSignaturePayloadV2':
87
+ case 'PaginatedDeleteSignaturePayloadV2':
88
+ case 'ItemizedSignaturePayloadV2':
89
+ case 'PasskeyPublicKey':
90
+ case 'ClaimHandlePayload':
91
+ case 'AddProvider':
92
+ case 'RecoveryCommitmentPayload':
93
+ case 'SiwfLoginRequestPayload':
94
+ break;
95
+ case 'AddKeyData':
96
+ // convert to 20 bytes ethereum address for signature
97
+ if (clonedPayload.newPublicKey.length !== 42) {
98
+ clonedPayload.newPublicKey = (0, address_js_1.reverseUnifiedAddressToEthereumAddress)(payload.newPublicKey);
99
+ }
100
+ clonedPayload.newPublicKey = clonedPayload.newPublicKey.toLowerCase();
101
+ break;
102
+ case 'AuthorizedKeyData':
103
+ // convert to 20 bytes ethereum address for signature
104
+ if (clonedPayload.authorizedPublicKey.length !== 42) {
105
+ clonedPayload.authorizedPublicKey = (0, address_js_1.reverseUnifiedAddressToEthereumAddress)(payload.authorizedPublicKey);
106
+ }
107
+ clonedPayload.authorizedPublicKey = clonedPayload.authorizedPublicKey.toLowerCase();
108
+ break;
109
+ case 'SiwfSignedRequestPayload':
110
+ if (clonedPayload.userIdentifierAdminUrl == null) {
111
+ clonedPayload.userIdentifierAdminUrl = '';
112
+ }
113
+ break;
114
+ default:
115
+ throw new Error(`Unsupported payload type: ${JSON.stringify(payload)}`);
116
+ }
117
+ // Remove the type field
118
+ const { type, ...payloadWithoutType } = clonedPayload;
119
+ return payloadWithoutType;
120
+ }
121
+ function getTypesFor(payloadType) {
122
+ const PAYLOAD_TYPE_DEFINITIONS = {
123
+ PaginatedUpsertSignaturePayloadV2: signature_definitions_js_1.PAGINATED_UPSERT_SIGNATURE_PAYLOAD_DEFINITION_V2,
124
+ PaginatedDeleteSignaturePayloadV2: signature_definitions_js_1.PAGINATED_DELETE_SIGNATURE_PAYLOAD_DEFINITION_V2,
125
+ ItemizedSignaturePayloadV2: signature_definitions_js_1.ITEMIZED_SIGNATURE_PAYLOAD_DEFINITION_V2,
126
+ PasskeyPublicKey: signature_definitions_js_1.PASSKEY_PUBLIC_KEY_DEFINITION,
127
+ ClaimHandlePayload: signature_definitions_js_1.CLAIM_HANDLE_PAYLOAD_DEFINITION,
128
+ AddKeyData: signature_definitions_js_1.ADD_KEY_DATA_DEFINITION,
129
+ AuthorizedKeyData: signature_definitions_js_1.AUTHORIZED_KEY_DATA_DEFINITION,
130
+ AddProvider: signature_definitions_js_1.ADD_PROVIDER_DEFINITION,
131
+ RecoveryCommitmentPayload: signature_definitions_js_1.RECOVERY_COMMITMENT_PAYLOAD_DEFINITION,
132
+ // offchain signatures
133
+ SiwfSignedRequestPayload: signature_definitions_js_1.SIWF_SIGNED_REQUEST_PAYLOAD_DEFINITION,
134
+ };
135
+ const definition = PAYLOAD_TYPE_DEFINITIONS[payloadType];
136
+ if (!definition) {
137
+ throw new Error(`Unsupported payload type: ${payloadType}`);
138
+ }
139
+ return definition;
140
+ }
141
+ function getSignatureType(payloadType) {
142
+ if (payloadType === 'SiwfLoginRequestPayload') {
143
+ return 'EIP-191';
144
+ }
145
+ return 'EIP-712';
146
+ }
147
+ /**
148
+ * Build an AddKeyData payload for signature.
149
+ *
150
+ * @param msaId MSA ID (uint64) to add the key
151
+ * @param newPublicKey 32 bytes public key to add in hex or Uint8Array
152
+ * @param expirationBlock Block number after which this payload is invalid
153
+ */
154
+ function createAddKeyData(msaId, newPublicKey, expirationBlock) {
155
+ const parsedMsaId = typeof msaId === 'string' ? msaId : `${msaId}`;
156
+ const parsedNewPublicKey = typeof newPublicKey === 'object' ? (0, util_1.u8aToHex)(newPublicKey) : newPublicKey;
157
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint64String)(parsedMsaId), 'msaId should be a valid uint64');
158
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(expirationBlock), 'expiration should be a valid uint32');
159
+ (0, utils_js_1.assert)((0, utils_js_1.isHexString)(parsedNewPublicKey), 'newPublicKey should be valid hex');
160
+ return {
161
+ type: 'AddKeyData',
162
+ msaId: parsedMsaId,
163
+ expiration: expirationBlock,
164
+ newPublicKey: parsedNewPublicKey,
165
+ };
166
+ }
167
+ /**
168
+ * Build an AuthorizedKeyData payload for signature.
169
+ *
170
+ * @param msaId MSA ID (uint64) to add the key
171
+ * @param authorizedPublicKey 32 bytes public key to authorize in hex or Uint8Array
172
+ * @param expirationBlock Block number after which this payload is invalid
173
+ */
174
+ function createAuthorizedKeyData(msaId, newPublicKey, expirationBlock) {
175
+ const parsedMsaId = typeof msaId === 'string' ? msaId : `${msaId}`;
176
+ const parsedNewPublicKey = typeof newPublicKey === 'object' ? (0, util_1.u8aToHex)(newPublicKey) : newPublicKey;
177
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint64String)(parsedMsaId), 'msaId should be a valid uint64');
178
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(expirationBlock), 'expiration should be a valid uint32');
179
+ (0, utils_js_1.assert)((0, utils_js_1.isHexString)(parsedNewPublicKey), 'newPublicKey should be valid hex');
180
+ return {
181
+ type: 'AuthorizedKeyData',
182
+ msaId: parsedMsaId,
183
+ expiration: expirationBlock,
184
+ authorizedPublicKey: parsedNewPublicKey,
185
+ };
186
+ }
187
+ /**
188
+ * Build an AddProvider payload for signature.
189
+ *
190
+ * @param authorizedMsaId MSA ID (uint64) that will be granted provider rights
191
+ * @param schemaIds One or more schema IDs (uint16) the provider may use
192
+ * @param expirationBlock Block number after which this payload is invalid
193
+ */
194
+ function createAddProvider(authorizedMsaId, schemaIds, expirationBlock) {
195
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint64String)(authorizedMsaId), 'authorizedMsaId should be a valid uint64');
196
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(expirationBlock), 'expiration should be a valid uint32');
197
+ schemaIds.forEach((schemaId) => {
198
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(schemaId), 'schemaId should be a valid uint16');
199
+ });
200
+ return {
201
+ type: 'AddProvider',
202
+ authorizedMsaId: authorizedMsaId.toString(),
203
+ schemaIds,
204
+ expiration: expirationBlock,
205
+ };
206
+ }
207
+ /**
208
+ * Build a RecoveryCommitmentPayload for signature.
209
+ *
210
+ * @param recoveryCommitment The recovery commitment data as a HexString
211
+ * @param expirationBlock Block number after which this payload is invalid
212
+ */
213
+ function createRecoveryCommitmentPayload(recoveryCommitment, expirationBlock) {
214
+ (0, utils_js_1.assert)((0, utils_js_1.isHexString)(recoveryCommitment), 'recoveryCommitment should be a valid hex string');
215
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(expirationBlock), 'expiration should be a valid uint32');
216
+ return {
217
+ type: 'RecoveryCommitmentPayload',
218
+ recoveryCommitment: recoveryCommitment,
219
+ expiration: expirationBlock,
220
+ };
221
+ }
222
+ /**
223
+ * Build a ClaimHandlePayload for signature.
224
+ *
225
+ * @param handle The handle the user wishes to claim
226
+ * @param expirationBlock Block number after which this payload is invalid
227
+ */
228
+ function createClaimHandlePayload(handle, expirationBlock) {
229
+ (0, utils_js_1.assert)(handle.length > 0, 'handle should be a valid string');
230
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(expirationBlock), 'expiration should be a valid uint32');
231
+ return {
232
+ type: 'ClaimHandlePayload',
233
+ handle,
234
+ expiration: expirationBlock,
235
+ };
236
+ }
237
+ /**
238
+ * Build a PasskeyPublicKey payload for signature.
239
+ *
240
+ * @param publicKey The passkey’s public key (hex string or raw bytes)
241
+ */
242
+ function createPasskeyPublicKey(publicKey) {
243
+ const parsedNewPublicKey = typeof publicKey === 'object' ? (0, util_1.u8aToHex)(publicKey) : publicKey;
244
+ (0, utils_js_1.assert)((0, utils_js_1.isHexString)(parsedNewPublicKey), 'publicKey should be valid hex');
245
+ return {
246
+ type: 'PasskeyPublicKey',
247
+ publicKey: parsedNewPublicKey,
248
+ };
249
+ }
250
+ function createItemizedAddAction(data) {
251
+ const parsedData = typeof data === 'object' ? (0, util_1.u8aToHex)(data) : data;
252
+ (0, utils_js_1.assert)((0, utils_js_1.isHexString)(parsedData), 'itemized data should be valid hex');
253
+ return { actionType: 'Add', data, index: 0 };
254
+ }
255
+ function createItemizedDeleteAction(index) {
256
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(index), 'itemized index should be a valid uint16');
257
+ return { actionType: 'Delete', data: '0x', index };
258
+ }
259
+ /**
260
+ * Build an ItemizedSignaturePayloadV2 for signing.
261
+ *
262
+ * @param schemaId uint16 schema identifier
263
+ * @param targetHash uint32 page hash
264
+ * @param expiration uint32 expiration block
265
+ * @param actions Array of Add/Delete itemized actions
266
+ */
267
+ function createItemizedSignaturePayloadV2(schemaId, targetHash, expiration, actions) {
268
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(schemaId), 'schemaId should be a valid uint16');
269
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(targetHash), 'targetHash should be a valid uint32');
270
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(expiration), 'expiration should be a valid uint32');
271
+ (0, utils_js_1.assert)(actions.length > 0, 'At least one action is required for ItemizedSignaturePayloadV2');
272
+ return {
273
+ type: 'ItemizedSignaturePayloadV2',
274
+ schemaId,
275
+ targetHash,
276
+ expiration,
277
+ actions,
278
+ };
279
+ }
280
+ /**
281
+ * Build a PaginatedDeleteSignaturePayloadV2 for signing.
282
+ *
283
+ * @param schemaId uint16 schema identifier
284
+ * @param pageId uint16 page identifier
285
+ * @param targetHash uint32 page hash
286
+ * @param expiration uint32 expiration block
287
+ */
288
+ function createPaginatedDeleteSignaturePayloadV2(schemaId, pageId, targetHash, expiration) {
289
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(schemaId), 'schemaId should be a valid uint16');
290
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(pageId), 'pageId should be a valid uint16');
291
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(targetHash), 'targetHash should be a valid uint32');
292
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(expiration), 'expiration should be a valid uint32');
293
+ return {
294
+ type: 'PaginatedDeleteSignaturePayloadV2',
295
+ schemaId,
296
+ pageId,
297
+ targetHash,
298
+ expiration,
299
+ };
300
+ }
301
+ /**
302
+ * Build a PaginatedUpsertSignaturePayloadV2 for signing.
303
+ *
304
+ * @param schemaId uint16 schema identifier
305
+ * @param pageId uint16 page identifier
306
+ * @param targetHash uint32 page hash
307
+ * @param expiration uint32 expiration block
308
+ * @param payload HexString or Uint8Array data to upsert
309
+ */
310
+ function createPaginatedUpsertSignaturePayloadV2(schemaId, pageId, targetHash, expiration, payload) {
311
+ const parsedPayload = typeof payload === 'object' ? (0, util_1.u8aToHex)(payload) : payload;
312
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(schemaId), 'schemaId should be a valid uint16');
313
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(pageId), 'pageId should be a valid uint16');
314
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(targetHash), 'targetHash should be a valid uint32');
315
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(expiration), 'expiration should be a valid uint32');
316
+ (0, utils_js_1.assert)((0, utils_js_1.isHexString)(parsedPayload), 'payload should be valid hex');
317
+ return {
318
+ type: 'PaginatedUpsertSignaturePayloadV2',
319
+ schemaId,
320
+ pageId,
321
+ targetHash,
322
+ expiration,
323
+ payload: parsedPayload,
324
+ };
325
+ }
326
+ /**
327
+ * Build an SiwfSignedRequestPayload payload for signature.
328
+ *
329
+ * @param callback Callback URL for login
330
+ * @param permissions One or more schema IDs (uint16) the provider may use
331
+ * @param userIdentifierAdminUrl Only used for custom integration situations.
332
+ */
333
+ function createSiwfSignedRequestPayload(callback, permissions, userIdentifierAdminUrl) {
334
+ permissions.forEach((schemaId) => {
335
+ (0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(schemaId), 'permission should be a valid uint16');
336
+ });
337
+ return {
338
+ type: 'SiwfSignedRequestPayload',
339
+ callback: callback,
340
+ permissions,
341
+ userIdentifierAdminUrl,
342
+ };
343
+ }
344
+ /**
345
+ * Build an SiwfLoginRequestPayload payload for signature.
346
+ *
347
+ * @param message login message
348
+ */
349
+ function createSiwfLoginRequestPayload(message) {
350
+ return {
351
+ type: 'SiwfLoginRequestPayload',
352
+ message,
353
+ };
354
+ }
355
+ /**
356
+ * Returns the EIP-712 browser request for a AddKeyData for signing.
357
+ *
358
+ * @param msaId MSA ID (uint64) to add the key
359
+ * @param newPublicKey 32 bytes public key to add in hex or Uint8Array
360
+ * @param expirationBlock Block number after which this payload is invalid
361
+ * @param domain
362
+ */
363
+ function getEip712BrowserRequestAddKeyData(msaId, newPublicKey, expirationBlock, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
364
+ const message = createAddKeyData(msaId, newPublicKey, expirationBlock);
365
+ const normalized = normalizePayload(message);
366
+ return createEip712Payload(signature_definitions_js_1.ADD_KEY_DATA_DEFINITION, message.type, domain, normalized);
367
+ }
368
+ /**
369
+ * Returns the EIP-712 browser request for a AuthorizedKeyData for signing.
370
+ *
371
+ * @param msaId MSA ID (uint64) to add the key
372
+ * @param authorizedPublicKey 32 bytes public key to add in hex or Uint8Array
373
+ * @param expirationBlock Block number after which this payload is invalid
374
+ * @param domain
375
+ */
376
+ function getEip712BrowserRequestAuthorizedKeyData(msaId, authorizedPublicKey, expirationBlock, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
377
+ const message = createAuthorizedKeyData(msaId, authorizedPublicKey, expirationBlock);
378
+ const normalized = normalizePayload(message);
379
+ return createEip712Payload(signature_definitions_js_1.AUTHORIZED_KEY_DATA_DEFINITION, message.type, domain, normalized);
380
+ }
381
+ /**
382
+ * Returns the EIP-712 browser request for a AddProvider for signing.
383
+ *
384
+ * @param authorizedMsaId MSA ID (uint64) that will be granted provider rights
385
+ * @param schemaIds One or more schema IDs (uint16) the provider may use
386
+ * @param expirationBlock Block number after which this payload is invalid
387
+ * @param domain
388
+ */
389
+ function getEip712BrowserRequestAddProvider(authorizedMsaId, schemaIds, expirationBlock, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
390
+ const message = createAddProvider(authorizedMsaId, schemaIds, expirationBlock);
391
+ const normalized = normalizePayload(message);
392
+ return createEip712Payload(signature_definitions_js_1.ADD_PROVIDER_DEFINITION, message.type, domain, normalized);
393
+ }
394
+ /**
395
+ * Returns the EIP-712 browser request for a RecoveryCommitmentPayload for signing.
396
+ *
397
+ * @param recoveryCommitment The recovery commitment data as a Uint8Array
398
+ * @param expirationBlock Block number after which this payload is invalid
399
+ * @param domain
400
+ */
401
+ function getEip712BrowserRequestRecoveryCommitmentPayload(recoveryCommitment, expirationBlock, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
402
+ const message = createRecoveryCommitmentPayload(recoveryCommitment, expirationBlock);
403
+ const normalized = normalizePayload(message);
404
+ return createEip712Payload(signature_definitions_js_1.RECOVERY_COMMITMENT_PAYLOAD_DEFINITION, message.type, domain, normalized);
405
+ }
406
+ /**
407
+ * Returns the EIP-712 browser request for a PaginatedUpsertSignaturePayloadV2 for signing.
408
+ *
409
+ * @param schemaId uint16 schema identifier
410
+ * @param pageId uint16 page identifier
411
+ * @param targetHash uint32 page hash
412
+ * @param expiration uint32 expiration block
413
+ * @param payload HexString or Uint8Array data to upsert
414
+ * @param domain
415
+ */
416
+ function getEip712BrowserRequestPaginatedUpsertSignaturePayloadV2(schemaId, pageId, targetHash, expiration, payload, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
417
+ const message = createPaginatedUpsertSignaturePayloadV2(schemaId, pageId, targetHash, expiration, payload);
418
+ const normalized = normalizePayload(message);
419
+ return createEip712Payload(signature_definitions_js_1.PAGINATED_UPSERT_SIGNATURE_PAYLOAD_DEFINITION_V2, message.type, domain, normalized);
420
+ }
421
+ /**
422
+ * Returns the EIP-712 browser request for a PaginatedDeleteSignaturePayloadV2 for signing.
423
+ *
424
+ * @param schemaId uint16 schema identifier
425
+ * @param pageId uint16 page identifier
426
+ * @param targetHash uint32 page hash
427
+ * @param expiration uint32 expiration block
428
+ * @param domain
429
+ */
430
+ function getEip712BrowserRequestPaginatedDeleteSignaturePayloadV2(schemaId, pageId, targetHash, expiration, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
431
+ const message = createPaginatedDeleteSignaturePayloadV2(schemaId, pageId, targetHash, expiration);
432
+ const normalized = normalizePayload(message);
433
+ return createEip712Payload(signature_definitions_js_1.PAGINATED_DELETE_SIGNATURE_PAYLOAD_DEFINITION_V2, message.type, domain, normalized);
434
+ }
435
+ /**
436
+ * Returns the EIP-712 browser request for a ItemizedSignaturePayloadV2 for signing.
437
+ *
438
+ * @param schemaId uint16 schema identifier
439
+ * @param targetHash uint32 page hash
440
+ * @param expiration uint32 expiration block
441
+ * @param actions Array of Add/Delete itemized actions
442
+ * @param domain
443
+ */
444
+ function getEip712BrowserRequestItemizedSignaturePayloadV2(schemaId, targetHash, expiration, actions, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
445
+ const message = createItemizedSignaturePayloadV2(schemaId, targetHash, expiration, actions);
446
+ const normalized = normalizePayload(message);
447
+ return createEip712Payload(signature_definitions_js_1.ITEMIZED_SIGNATURE_PAYLOAD_DEFINITION_V2, message.type, domain, normalized);
448
+ }
449
+ /**
450
+ * Returns the EIP-712 browser request for a ClaimHandlePayload for signing.
451
+ *
452
+ * @param handle The handle the user wishes to claim
453
+ * @param expirationBlock Block number after which this payload is invalid
454
+ * @param domain
455
+ */
456
+ function getEip712BrowserRequestClaimHandlePayload(handle, expirationBlock, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
457
+ const message = createClaimHandlePayload(handle, expirationBlock);
458
+ const normalized = normalizePayload(message);
459
+ return createEip712Payload(signature_definitions_js_1.CLAIM_HANDLE_PAYLOAD_DEFINITION, message.type, domain, normalized);
460
+ }
461
+ /**
462
+ * Returns the EIP-712 browser request for a PasskeyPublicKey for signing.
463
+ *
464
+ * @param publicKey The passkey’s public key (hex string or raw bytes)
465
+ * @param domain
466
+ */
467
+ function getEip712BrowserRequestPasskeyPublicKey(publicKey, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
468
+ const message = createPasskeyPublicKey(publicKey);
469
+ const normalized = normalizePayload(message);
470
+ return createEip712Payload(signature_definitions_js_1.PASSKEY_PUBLIC_KEY_DEFINITION, message.type, domain, normalized);
471
+ }
472
+ /**
473
+ * Returns the EIP-712 browser request for a SiwfSignedRequestPayload for signing.
474
+ *
475
+ * @param callback Callback URL for login
476
+ * @param permissions One or more schema IDs (uint16) the provider may use
477
+ * @param userIdentifierAdminUrl Only used for custom integration situations.
478
+ * @param domain
479
+ */
480
+ function getEip712BrowserRequestSiwfSignedRequestPayload(callback, permissions, userIdentifierAdminUrl, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
481
+ const message = createSiwfSignedRequestPayload(callback, permissions, userIdentifierAdminUrl);
482
+ const normalized = normalizePayload(message);
483
+ return createEip712Payload(signature_definitions_js_1.SIWF_SIGNED_REQUEST_PAYLOAD_DEFINITION, message.type, domain, normalized);
484
+ }
485
+ function createEip712Payload(typeDefinition, primaryType, domain, message) {
486
+ return {
487
+ types: {
488
+ ...signature_definitions_js_1.EIP712_DOMAIN_DEFINITION,
489
+ ...typeDefinition,
490
+ },
491
+ primaryType,
492
+ domain,
493
+ message,
494
+ };
495
+ }
496
+ /**
497
+ * Returns An ethereum compatible signature for the extrinsic
498
+ * @param ethereumPair
499
+ */
500
+ function getEthereumRegularSigner(ethereumPair) {
501
+ return {
502
+ signRaw: async (payload) => {
503
+ const sig = ethereumPair.sign(payload.data);
504
+ const prefixedSignature = new Uint8Array(sig.length + 1);
505
+ prefixedSignature[0] = 2;
506
+ prefixedSignature.set(sig, 1);
507
+ const hex = (0, util_1.u8aToHex)(prefixedSignature);
508
+ return {
509
+ signature: hex,
510
+ };
511
+ },
512
+ };
513
+ }
514
+ /**
515
+ * This custom signer can get used to mimic EIP-191 message signing. By replacing the `ethereumPair.sign` with
516
+ * any wallet call we can sign any extrinsic with any wallet
517
+ * @param ethereumPair
518
+ */
519
+ function getEthereumMessageSigner(ethereumPair) {
520
+ return {
521
+ signRaw: async (payload) => {
522
+ const sig = ethereumPair.sign(prefixEthereumTags(payload.data));
523
+ const prefixedSignature = new Uint8Array(sig.length + 1);
524
+ prefixedSignature[0] = 2;
525
+ prefixedSignature.set(sig, 1);
526
+ const hex = (0, util_1.u8aToHex)(prefixedSignature);
527
+ return {
528
+ signature: hex,
529
+ };
530
+ },
531
+ };
532
+ }
533
+ /**
534
+ * prefixing with the EIP-191 for personal_sign messages (this gets wrapped automatically in Metamask)
535
+ * @param hexPayload
536
+ */
537
+ function prefixEthereumTags(hexPayload) {
538
+ const wrapped = `\x19Ethereum Signed Message:\n${hexPayload.length}${hexPayload}`;
539
+ const buffer = Buffer.from(wrapped, 'utf-8');
540
+ return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.length);
541
+ }
package/cjs/utils.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidUint16 = isValidUint16;
4
+ exports.isValidUint32 = isValidUint32;
5
+ exports.isValidUint64String = isValidUint64String;
6
+ exports.isHexString = isHexString;
7
+ exports.assert = assert;
8
+ /**
9
+ * Validate that a number is a valid uint16 (0 to 65535)
10
+ */
11
+ function isValidUint16(value) {
12
+ return Number.isInteger(value) && value >= 0 && value <= 65535;
13
+ }
14
+ /**
15
+ * Validate that a number is a valid uint32 (0 to 4294967295)
16
+ */
17
+ function isValidUint32(value) {
18
+ return Number.isInteger(value) && value >= 0 && value <= 4294967295;
19
+ }
20
+ /**
21
+ * Validate that a number is a valid uint64 (0 to 18446744073709551615n)
22
+ */
23
+ function isValidUint64String(value) {
24
+ const bigIntValue = typeof value === 'string' ? BigInt(value) : value;
25
+ return bigIntValue >= 0 && bigIntValue <= 18446744073709551615n;
26
+ }
27
+ /**
28
+ * Validate that a string is a valid hex string
29
+ */
30
+ function isHexString(value) {
31
+ // Check if string starts with '0x' and contains only hex characters
32
+ const hexRegex = /^0[xX][0-9a-fA-F]*$/;
33
+ const isHex = hexRegex.test(value);
34
+ return isHex && value.length % 2 === 0;
35
+ }
36
+ /**
37
+ * Universal assert function
38
+ * @param condition
39
+ * @param message
40
+ */
41
+ function assert(condition, message) {
42
+ if (!condition) {
43
+ throw new Error(message || ' Assertion failed');
44
+ }
45
+ }