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