@frequency-chain/ethereum-utils 0.0.0-0eb70a
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 +20302 -0
- package/browser/frequency-ethereum-utils.umd.min.js +11548 -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/cjs/signature.js
ADDED
|
@@ -0,0 +1,555 @@
|
|
|
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
|
+
/**
|
|
251
|
+
* Build AddAction payload for Itemized storage.
|
|
252
|
+
*
|
|
253
|
+
* @param data The data to be persisted on the Frequency chain
|
|
254
|
+
*/
|
|
255
|
+
function createItemizedAddAction(data) {
|
|
256
|
+
const parsedData = typeof data === 'object' ? (0, util_1.u8aToHex)(data) : data;
|
|
257
|
+
(0, utils_js_1.assert)((0, utils_js_1.isHexString)(parsedData), 'itemized data should be valid hex');
|
|
258
|
+
// since Metamask does not support union types, we have to include all fields and have to set the `index` value to zero
|
|
259
|
+
// even though it is not used for Add action
|
|
260
|
+
return { actionType: 'Add', data, index: 0 };
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Build DeleteAction payload for Itemized storage.
|
|
264
|
+
*
|
|
265
|
+
* @param index The index of the item that we want to remove from the Frequency chain
|
|
266
|
+
*/
|
|
267
|
+
function createItemizedDeleteAction(index) {
|
|
268
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(index), 'itemized index should be a valid uint16');
|
|
269
|
+
// since Metamask does not support union types, we have to include all fields and have to set the `data` value to 0x
|
|
270
|
+
// even though it is not used for Delete action
|
|
271
|
+
return { actionType: 'Delete', data: '0x', index };
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Build an ItemizedSignaturePayloadV2 for signing.
|
|
275
|
+
*
|
|
276
|
+
* @param schemaId uint16 schema identifier
|
|
277
|
+
* @param targetHash uint32 page hash
|
|
278
|
+
* @param expiration uint32 expiration block
|
|
279
|
+
* @param actions Array of Add/Delete itemized actions
|
|
280
|
+
*/
|
|
281
|
+
function createItemizedSignaturePayloadV2(schemaId, targetHash, expiration, actions) {
|
|
282
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(schemaId), 'schemaId should be a valid uint16');
|
|
283
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(targetHash), 'targetHash should be a valid uint32');
|
|
284
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(expiration), 'expiration should be a valid uint32');
|
|
285
|
+
(0, utils_js_1.assert)(actions.length > 0, 'At least one action is required for ItemizedSignaturePayloadV2');
|
|
286
|
+
return {
|
|
287
|
+
type: 'ItemizedSignaturePayloadV2',
|
|
288
|
+
schemaId,
|
|
289
|
+
targetHash,
|
|
290
|
+
expiration,
|
|
291
|
+
actions,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Build a PaginatedDeleteSignaturePayloadV2 for signing.
|
|
296
|
+
*
|
|
297
|
+
* @param schemaId uint16 schema identifier
|
|
298
|
+
* @param pageId uint16 page identifier
|
|
299
|
+
* @param targetHash uint32 page hash
|
|
300
|
+
* @param expiration uint32 expiration block
|
|
301
|
+
*/
|
|
302
|
+
function createPaginatedDeleteSignaturePayloadV2(schemaId, pageId, targetHash, expiration) {
|
|
303
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(schemaId), 'schemaId should be a valid uint16');
|
|
304
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(pageId), 'pageId should be a valid uint16');
|
|
305
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(targetHash), 'targetHash should be a valid uint32');
|
|
306
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(expiration), 'expiration should be a valid uint32');
|
|
307
|
+
return {
|
|
308
|
+
type: 'PaginatedDeleteSignaturePayloadV2',
|
|
309
|
+
schemaId,
|
|
310
|
+
pageId,
|
|
311
|
+
targetHash,
|
|
312
|
+
expiration,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Build a PaginatedUpsertSignaturePayloadV2 for signing.
|
|
317
|
+
*
|
|
318
|
+
* @param schemaId uint16 schema identifier
|
|
319
|
+
* @param pageId uint16 page identifier
|
|
320
|
+
* @param targetHash uint32 page hash
|
|
321
|
+
* @param expiration uint32 expiration block
|
|
322
|
+
* @param payload HexString or Uint8Array data to upsert
|
|
323
|
+
*/
|
|
324
|
+
function createPaginatedUpsertSignaturePayloadV2(schemaId, pageId, targetHash, expiration, payload) {
|
|
325
|
+
const parsedPayload = typeof payload === 'object' ? (0, util_1.u8aToHex)(payload) : payload;
|
|
326
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(schemaId), 'schemaId should be a valid uint16');
|
|
327
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(pageId), 'pageId should be a valid uint16');
|
|
328
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(targetHash), 'targetHash should be a valid uint32');
|
|
329
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint32)(expiration), 'expiration should be a valid uint32');
|
|
330
|
+
(0, utils_js_1.assert)((0, utils_js_1.isHexString)(parsedPayload), 'payload should be valid hex');
|
|
331
|
+
return {
|
|
332
|
+
type: 'PaginatedUpsertSignaturePayloadV2',
|
|
333
|
+
schemaId,
|
|
334
|
+
pageId,
|
|
335
|
+
targetHash,
|
|
336
|
+
expiration,
|
|
337
|
+
payload: parsedPayload,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Build an SiwfSignedRequestPayload payload for signature.
|
|
342
|
+
*
|
|
343
|
+
* @param callback Callback URL for login
|
|
344
|
+
* @param permissions One or more schema IDs (uint16) the provider may use
|
|
345
|
+
* @param userIdentifierAdminUrl Only used for custom integration situations.
|
|
346
|
+
*/
|
|
347
|
+
function createSiwfSignedRequestPayload(callback, permissions, userIdentifierAdminUrl) {
|
|
348
|
+
permissions.forEach((schemaId) => {
|
|
349
|
+
(0, utils_js_1.assert)((0, utils_js_1.isValidUint16)(schemaId), 'permission should be a valid uint16');
|
|
350
|
+
});
|
|
351
|
+
return {
|
|
352
|
+
type: 'SiwfSignedRequestPayload',
|
|
353
|
+
callback: callback,
|
|
354
|
+
permissions,
|
|
355
|
+
userIdentifierAdminUrl,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Build an SiwfLoginRequestPayload payload for signature.
|
|
360
|
+
*
|
|
361
|
+
* @param message login message
|
|
362
|
+
*/
|
|
363
|
+
function createSiwfLoginRequestPayload(message) {
|
|
364
|
+
return {
|
|
365
|
+
type: 'SiwfLoginRequestPayload',
|
|
366
|
+
message,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Returns the EIP-712 browser request for a AddKeyData for signing.
|
|
371
|
+
*
|
|
372
|
+
* @param msaId MSA ID (uint64) to add the key
|
|
373
|
+
* @param newPublicKey 32 bytes public key to add in hex or Uint8Array
|
|
374
|
+
* @param expirationBlock Block number after which this payload is invalid
|
|
375
|
+
* @param domain
|
|
376
|
+
*/
|
|
377
|
+
function getEip712BrowserRequestAddKeyData(msaId, newPublicKey, expirationBlock, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
|
|
378
|
+
const message = createAddKeyData(msaId, newPublicKey, expirationBlock);
|
|
379
|
+
const normalized = normalizePayload(message);
|
|
380
|
+
return createEip712Payload(signature_definitions_js_1.ADD_KEY_DATA_DEFINITION, message.type, domain, normalized);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Returns the EIP-712 browser request for a AuthorizedKeyData for signing.
|
|
384
|
+
*
|
|
385
|
+
* @param msaId MSA ID (uint64) to add the key
|
|
386
|
+
* @param authorizedPublicKey 32 bytes public key to add in hex or Uint8Array
|
|
387
|
+
* @param expirationBlock Block number after which this payload is invalid
|
|
388
|
+
* @param domain
|
|
389
|
+
*/
|
|
390
|
+
function getEip712BrowserRequestAuthorizedKeyData(msaId, authorizedPublicKey, expirationBlock, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
|
|
391
|
+
const message = createAuthorizedKeyData(msaId, authorizedPublicKey, expirationBlock);
|
|
392
|
+
const normalized = normalizePayload(message);
|
|
393
|
+
return createEip712Payload(signature_definitions_js_1.AUTHORIZED_KEY_DATA_DEFINITION, message.type, domain, normalized);
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Returns the EIP-712 browser request for a AddProvider for signing.
|
|
397
|
+
*
|
|
398
|
+
* @param authorizedMsaId MSA ID (uint64) that will be granted provider rights
|
|
399
|
+
* @param schemaIds One or more schema IDs (uint16) the provider may use
|
|
400
|
+
* @param expirationBlock Block number after which this payload is invalid
|
|
401
|
+
* @param domain
|
|
402
|
+
*/
|
|
403
|
+
function getEip712BrowserRequestAddProvider(authorizedMsaId, schemaIds, expirationBlock, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
|
|
404
|
+
const message = createAddProvider(authorizedMsaId, schemaIds, expirationBlock);
|
|
405
|
+
const normalized = normalizePayload(message);
|
|
406
|
+
return createEip712Payload(signature_definitions_js_1.ADD_PROVIDER_DEFINITION, message.type, domain, normalized);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Returns the EIP-712 browser request for a RecoveryCommitmentPayload for signing.
|
|
410
|
+
*
|
|
411
|
+
* @param recoveryCommitment The recovery commitment data as a Uint8Array
|
|
412
|
+
* @param expirationBlock Block number after which this payload is invalid
|
|
413
|
+
* @param domain
|
|
414
|
+
*/
|
|
415
|
+
function getEip712BrowserRequestRecoveryCommitmentPayload(recoveryCommitment, expirationBlock, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
|
|
416
|
+
const message = createRecoveryCommitmentPayload(recoveryCommitment, expirationBlock);
|
|
417
|
+
const normalized = normalizePayload(message);
|
|
418
|
+
return createEip712Payload(signature_definitions_js_1.RECOVERY_COMMITMENT_PAYLOAD_DEFINITION, message.type, domain, normalized);
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Returns the EIP-712 browser request for a PaginatedUpsertSignaturePayloadV2 for signing.
|
|
422
|
+
*
|
|
423
|
+
* @param schemaId uint16 schema identifier
|
|
424
|
+
* @param pageId uint16 page identifier
|
|
425
|
+
* @param targetHash uint32 page hash
|
|
426
|
+
* @param expiration uint32 expiration block
|
|
427
|
+
* @param payload HexString or Uint8Array data to upsert
|
|
428
|
+
* @param domain
|
|
429
|
+
*/
|
|
430
|
+
function getEip712BrowserRequestPaginatedUpsertSignaturePayloadV2(schemaId, pageId, targetHash, expiration, payload, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
|
|
431
|
+
const message = createPaginatedUpsertSignaturePayloadV2(schemaId, pageId, targetHash, expiration, payload);
|
|
432
|
+
const normalized = normalizePayload(message);
|
|
433
|
+
return createEip712Payload(signature_definitions_js_1.PAGINATED_UPSERT_SIGNATURE_PAYLOAD_DEFINITION_V2, message.type, domain, normalized);
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Returns the EIP-712 browser request for a PaginatedDeleteSignaturePayloadV2 for signing.
|
|
437
|
+
*
|
|
438
|
+
* @param schemaId uint16 schema identifier
|
|
439
|
+
* @param pageId uint16 page identifier
|
|
440
|
+
* @param targetHash uint32 page hash
|
|
441
|
+
* @param expiration uint32 expiration block
|
|
442
|
+
* @param domain
|
|
443
|
+
*/
|
|
444
|
+
function getEip712BrowserRequestPaginatedDeleteSignaturePayloadV2(schemaId, pageId, targetHash, expiration, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
|
|
445
|
+
const message = createPaginatedDeleteSignaturePayloadV2(schemaId, pageId, targetHash, expiration);
|
|
446
|
+
const normalized = normalizePayload(message);
|
|
447
|
+
return createEip712Payload(signature_definitions_js_1.PAGINATED_DELETE_SIGNATURE_PAYLOAD_DEFINITION_V2, message.type, domain, normalized);
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Returns the EIP-712 browser request for a ItemizedSignaturePayloadV2 for signing.
|
|
451
|
+
*
|
|
452
|
+
* @param schemaId uint16 schema identifier
|
|
453
|
+
* @param targetHash uint32 page hash
|
|
454
|
+
* @param expiration uint32 expiration block
|
|
455
|
+
* @param actions Array of Add/Delete itemized actions
|
|
456
|
+
* @param domain
|
|
457
|
+
*/
|
|
458
|
+
function getEip712BrowserRequestItemizedSignaturePayloadV2(schemaId, targetHash, expiration, actions, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
|
|
459
|
+
const message = createItemizedSignaturePayloadV2(schemaId, targetHash, expiration, actions);
|
|
460
|
+
const normalized = normalizePayload(message);
|
|
461
|
+
return createEip712Payload(signature_definitions_js_1.ITEMIZED_SIGNATURE_PAYLOAD_DEFINITION_V2, message.type, domain, normalized);
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Returns the EIP-712 browser request for a ClaimHandlePayload for signing.
|
|
465
|
+
*
|
|
466
|
+
* @param handle The handle the user wishes to claim
|
|
467
|
+
* @param expirationBlock Block number after which this payload is invalid
|
|
468
|
+
* @param domain
|
|
469
|
+
*/
|
|
470
|
+
function getEip712BrowserRequestClaimHandlePayload(handle, expirationBlock, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
|
|
471
|
+
const message = createClaimHandlePayload(handle, expirationBlock);
|
|
472
|
+
const normalized = normalizePayload(message);
|
|
473
|
+
return createEip712Payload(signature_definitions_js_1.CLAIM_HANDLE_PAYLOAD_DEFINITION, message.type, domain, normalized);
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Returns the EIP-712 browser request for a PasskeyPublicKey for signing.
|
|
477
|
+
*
|
|
478
|
+
* @param publicKey The passkey’s public key (hex string or raw bytes)
|
|
479
|
+
* @param domain
|
|
480
|
+
*/
|
|
481
|
+
function getEip712BrowserRequestPasskeyPublicKey(publicKey, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
|
|
482
|
+
const message = createPasskeyPublicKey(publicKey);
|
|
483
|
+
const normalized = normalizePayload(message);
|
|
484
|
+
return createEip712Payload(signature_definitions_js_1.PASSKEY_PUBLIC_KEY_DEFINITION, message.type, domain, normalized);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Returns the EIP-712 browser request for a SiwfSignedRequestPayload for signing.
|
|
488
|
+
*
|
|
489
|
+
* @param callback Callback URL for login
|
|
490
|
+
* @param permissions One or more schema IDs (uint16) the provider may use
|
|
491
|
+
* @param userIdentifierAdminUrl Only used for custom integration situations.
|
|
492
|
+
* @param domain
|
|
493
|
+
*/
|
|
494
|
+
function getEip712BrowserRequestSiwfSignedRequestPayload(callback, permissions, userIdentifierAdminUrl, domain = signature_definitions_js_1.EIP712_DOMAIN_MAINNET) {
|
|
495
|
+
const message = createSiwfSignedRequestPayload(callback, permissions, userIdentifierAdminUrl);
|
|
496
|
+
const normalized = normalizePayload(message);
|
|
497
|
+
return createEip712Payload(signature_definitions_js_1.SIWF_SIGNED_REQUEST_PAYLOAD_DEFINITION, message.type, domain, normalized);
|
|
498
|
+
}
|
|
499
|
+
function createEip712Payload(typeDefinition, primaryType, domain, message) {
|
|
500
|
+
return {
|
|
501
|
+
types: {
|
|
502
|
+
...signature_definitions_js_1.EIP712_DOMAIN_DEFINITION,
|
|
503
|
+
...typeDefinition,
|
|
504
|
+
},
|
|
505
|
+
primaryType,
|
|
506
|
+
domain,
|
|
507
|
+
message,
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Returns An ethereum compatible signature for the extrinsic
|
|
512
|
+
* @param ethereumPair
|
|
513
|
+
*/
|
|
514
|
+
function getEthereumRegularSigner(ethereumPair) {
|
|
515
|
+
return {
|
|
516
|
+
signRaw: async (payload) => {
|
|
517
|
+
const sig = ethereumPair.sign(payload.data);
|
|
518
|
+
const prefixedSignature = new Uint8Array(sig.length + 1);
|
|
519
|
+
prefixedSignature[0] = 2;
|
|
520
|
+
prefixedSignature.set(sig, 1);
|
|
521
|
+
const hex = (0, util_1.u8aToHex)(prefixedSignature);
|
|
522
|
+
return {
|
|
523
|
+
signature: hex,
|
|
524
|
+
};
|
|
525
|
+
},
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* This custom signer can get used to mimic EIP-191 message signing. By replacing the `ethereumPair.sign` with
|
|
530
|
+
* any wallet call we can sign any extrinsic with any wallet
|
|
531
|
+
* @param ethereumPair
|
|
532
|
+
*/
|
|
533
|
+
function getEthereumMessageSigner(ethereumPair) {
|
|
534
|
+
return {
|
|
535
|
+
signRaw: async (payload) => {
|
|
536
|
+
const sig = ethereumPair.sign(prefixEthereumTags(payload.data));
|
|
537
|
+
const prefixedSignature = new Uint8Array(sig.length + 1);
|
|
538
|
+
prefixedSignature[0] = 2;
|
|
539
|
+
prefixedSignature.set(sig, 1);
|
|
540
|
+
const hex = (0, util_1.u8aToHex)(prefixedSignature);
|
|
541
|
+
return {
|
|
542
|
+
signature: hex,
|
|
543
|
+
};
|
|
544
|
+
},
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* prefixing with the EIP-191 for personal_sign messages (this gets wrapped automatically in Metamask)
|
|
549
|
+
* @param hexPayload
|
|
550
|
+
*/
|
|
551
|
+
function prefixEthereumTags(hexPayload) {
|
|
552
|
+
const wrapped = `\x19Ethereum Signed Message:\n${hexPayload.length}${hexPayload}`;
|
|
553
|
+
const buffer = Buffer.from(wrapped, 'utf-8');
|
|
554
|
+
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.length);
|
|
555
|
+
}
|
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
|
+
}
|