@frequency-chain/ethereum-utils 1.17.0-rc4

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/cjs/address.js ADDED
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRandomKey = createRandomKey;
4
+ exports.ethereumAddressToKeyringPair = ethereumAddressToKeyringPair;
5
+ exports.getUnifiedAddress = getUnifiedAddress;
6
+ exports.getUnifiedPublicKey = getUnifiedPublicKey;
7
+ exports.reverseUnifiedAddressToEthereumAddress = reverseUnifiedAddressToEthereumAddress;
8
+ exports.getSS58AccountFromEthereumAccount = getSS58AccountFromEthereumAccount;
9
+ const util_crypto_1 = require("@polkadot/util-crypto");
10
+ const util_1 = require("@polkadot/util");
11
+ const ethers_1 = require("ethers");
12
+ /**
13
+ * Creates a Random Ethereum key
14
+ */
15
+ function createRandomKey() {
16
+ const k = ethers_1.ethers.Wallet.createRandom();
17
+ return {
18
+ publicKey: k.publicKey,
19
+ privateKey: k.privateKey,
20
+ address: {
21
+ ethereumAddress: k.address,
22
+ unifiedAddress: getUnified32BytesAddress(k.address),
23
+ unifiedAddressSS58: getSS58AccountFromEthereumAccount(k.address),
24
+ },
25
+ mnemonic: k.mnemonic?.phrase ?? '',
26
+ };
27
+ }
28
+ /**
29
+ * Create a partial KeyringPair from an Ethereum address
30
+ */
31
+ function ethereumAddressToKeyringPair(ethereumAddress) {
32
+ return {
33
+ type: 'ethereum',
34
+ address: ethereumAddress.toHex(),
35
+ addressRaw: ethereumAddress,
36
+ };
37
+ }
38
+ /**
39
+ * Returns unified 32 bytes SS58 accountId
40
+ * @param pair
41
+ */
42
+ function getUnifiedAddress(pair) {
43
+ if ('ethereum' === pair.type) {
44
+ const etheAddressHex = (0, util_crypto_1.ethereumEncode)(pair.publicKey || pair.address);
45
+ return getSS58AccountFromEthereumAccount(etheAddressHex);
46
+ }
47
+ if (pair.type === 'ecdsa') {
48
+ throw new Error('Ecdsa key type is not supported and it should be replaced with ethereum ones!');
49
+ }
50
+ return pair.address;
51
+ }
52
+ /**
53
+ * Returns ethereum style public key with suffixed 0xee example: 0x19a701d23f0ee1748b5d5f883cb833943096c6c4eeeeeeeeeeeeeeeeeeeeeeee
54
+ * @param pair
55
+ */
56
+ function getUnifiedPublicKey(pair) {
57
+ if ('ethereum' === pair.type) {
58
+ const unifiedHex = getUnified32BytesAddress((0, util_1.u8aToHex)(pair.publicKey));
59
+ return (0, util_1.hexToU8a)(unifiedHex);
60
+ }
61
+ if (pair.type === 'ecdsa') {
62
+ throw new Error('Ecdsa key type is not supported and it should be replaced with ethereum ones!');
63
+ }
64
+ return pair.publicKey;
65
+ }
66
+ function reverseUnifiedAddressToEthereumAddress(unifiedAddress) {
67
+ if (!unifiedAddress.toLowerCase().endsWith('ee'.repeat(12))) {
68
+ throw new Error(`Address ${unifiedAddress} is not reversible!`);
69
+ }
70
+ return `${unifiedAddress.substring(0, 42)}`;
71
+ }
72
+ /**
73
+ * converts an ethereum account to SS58 format
74
+ * @param accountId20Hex
75
+ */
76
+ function getSS58AccountFromEthereumAccount(accountId20Hex) {
77
+ const addressBytes = (0, util_1.hexToU8a)(accountId20Hex);
78
+ const suffix = new Uint8Array(12).fill(0xee);
79
+ const result = new Uint8Array(32);
80
+ result.set(addressBytes, 0);
81
+ result.set(suffix, 20);
82
+ return (0, util_crypto_1.encodeAddress)(result);
83
+ }
84
+ function getUnified32BytesAddress(ethAddressOrPublicKey) {
85
+ const ethAddressBytes = (0, util_1.hexToU8a)((0, util_crypto_1.ethereumEncode)(ethAddressOrPublicKey));
86
+ const suffix = new Uint8Array(12).fill(0xee);
87
+ const result = new Uint8Array(32);
88
+ result.set(ethAddressBytes, 0);
89
+ result.set(suffix, 20);
90
+ return (0, util_1.u8aToHex)(result);
91
+ }
package/cjs/index.js ADDED
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ const address = __importStar(require("./address"));
40
+ const payloads = __importStar(require("./payloads"));
41
+ const signature = __importStar(require("./signature"));
42
+ const signatureDefinitions = __importStar(require("./signature.definitions"));
43
+ __exportStar(require("./payloads"), exports);
44
+ __exportStar(require("./signature"), exports);
45
+ __exportStar(require("./signature.definitions"), exports);
46
+ __exportStar(require("./address"), exports);
47
+ exports.default = { ...payloads, ...address, ...signatureDefinitions, ...signature };
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ITEMIZED_SIGNATURE_PAYLOAD_DEFINITION_V2 = exports.PAGINATED_UPSERT_SIGNATURE_PAYLOAD_DEFINITION_V2 = exports.PAGINATED_DELETE_SIGNATURE_PAYLOAD_DEFINITION_V2 = exports.PASSKEY_PUBLIC_KEY_DEFINITION = exports.CLAIM_HANDLE_PAYLOAD_DEFINITION = exports.ADD_KEY_DATA_DEFINITION = exports.ADD_PROVIDER_DEFINITION = exports.EIP712_DOMAIN_DEFAULT = exports.EIP712_DOMAIN_DEFINITION = void 0;
4
+ exports.EIP712_DOMAIN_DEFINITION = {
5
+ EIP712Domain: [
6
+ {
7
+ name: 'name',
8
+ type: 'string',
9
+ },
10
+ {
11
+ name: 'version',
12
+ type: 'string',
13
+ },
14
+ {
15
+ name: 'chainId',
16
+ type: 'uint256',
17
+ },
18
+ {
19
+ name: 'verifyingContract',
20
+ type: 'address',
21
+ },
22
+ ],
23
+ };
24
+ // using pallet_revive test chain ID for now.
25
+ exports.EIP712_DOMAIN_DEFAULT = {
26
+ name: 'Frequency',
27
+ version: '1',
28
+ chainId: '0x190f1b44',
29
+ verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
30
+ };
31
+ exports.ADD_PROVIDER_DEFINITION = {
32
+ AddProvider: [
33
+ {
34
+ name: 'authorizedMsaId',
35
+ type: 'uint64',
36
+ },
37
+ {
38
+ name: 'schemaIds',
39
+ type: 'uint16[]',
40
+ },
41
+ {
42
+ name: 'expiration',
43
+ type: 'uint32',
44
+ },
45
+ ],
46
+ };
47
+ exports.ADD_KEY_DATA_DEFINITION = {
48
+ AddKeyData: [
49
+ {
50
+ name: 'msaId',
51
+ type: 'uint64',
52
+ },
53
+ {
54
+ name: 'expiration',
55
+ type: 'uint32',
56
+ },
57
+ {
58
+ name: 'newPublicKey',
59
+ type: 'address',
60
+ },
61
+ ],
62
+ };
63
+ exports.CLAIM_HANDLE_PAYLOAD_DEFINITION = {
64
+ ClaimHandlePayload: [
65
+ {
66
+ name: 'handle',
67
+ type: 'string',
68
+ },
69
+ {
70
+ name: 'expiration',
71
+ type: 'uint32',
72
+ },
73
+ ],
74
+ };
75
+ exports.PASSKEY_PUBLIC_KEY_DEFINITION = {
76
+ PasskeyPublicKey: [
77
+ {
78
+ name: 'publicKey',
79
+ type: 'bytes',
80
+ },
81
+ ],
82
+ };
83
+ exports.PAGINATED_DELETE_SIGNATURE_PAYLOAD_DEFINITION_V2 = {
84
+ PaginatedDeleteSignaturePayloadV2: [
85
+ {
86
+ name: 'schemaId',
87
+ type: 'uint16',
88
+ },
89
+ {
90
+ name: 'pageId',
91
+ type: 'uint16',
92
+ },
93
+ {
94
+ name: 'targetHash',
95
+ type: 'uint32',
96
+ },
97
+ {
98
+ name: 'expiration',
99
+ type: 'uint32',
100
+ },
101
+ ],
102
+ };
103
+ exports.PAGINATED_UPSERT_SIGNATURE_PAYLOAD_DEFINITION_V2 = {
104
+ PaginatedUpsertSignaturePayloadV2: [
105
+ {
106
+ name: 'schemaId',
107
+ type: 'uint16',
108
+ },
109
+ {
110
+ name: 'pageId',
111
+ type: 'uint16',
112
+ },
113
+ {
114
+ name: 'targetHash',
115
+ type: 'uint32',
116
+ },
117
+ {
118
+ name: 'expiration',
119
+ type: 'uint32',
120
+ },
121
+ {
122
+ name: 'payload',
123
+ type: 'bytes',
124
+ },
125
+ ],
126
+ };
127
+ exports.ITEMIZED_SIGNATURE_PAYLOAD_DEFINITION_V2 = {
128
+ ItemizedSignaturePayloadV2: [
129
+ {
130
+ name: 'schemaId',
131
+ type: 'uint16',
132
+ },
133
+ {
134
+ name: 'targetHash',
135
+ type: 'uint32',
136
+ },
137
+ {
138
+ name: 'expiration',
139
+ type: 'uint32',
140
+ },
141
+ {
142
+ name: 'actions',
143
+ type: 'ItemAction[]',
144
+ },
145
+ ],
146
+ ItemAction: [
147
+ { name: 'actionType', type: 'string' },
148
+ { name: 'data', type: 'bytes' },
149
+ { name: 'index', type: 'uint16' },
150
+ ],
151
+ };
@@ -0,0 +1,342 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.signEip712 = signEip712;
4
+ exports.verifyEip712Signature = verifyEip712Signature;
5
+ exports.createAddKeyData = createAddKeyData;
6
+ exports.createAddProvider = createAddProvider;
7
+ exports.createClaimHandlePayload = createClaimHandlePayload;
8
+ exports.createPasskeyPublicKey = createPasskeyPublicKey;
9
+ exports.createItemizedAddAction = createItemizedAddAction;
10
+ exports.createItemizedDeleteAction = createItemizedDeleteAction;
11
+ exports.createItemizedSignaturePayloadV2 = createItemizedSignaturePayloadV2;
12
+ exports.createPaginatedDeleteSignaturePayloadV2 = createPaginatedDeleteSignaturePayloadV2;
13
+ exports.createPaginatedUpsertSignaturePayloadV2 = createPaginatedUpsertSignaturePayloadV2;
14
+ exports.getEip712BrowserRequestAddKeyData = getEip712BrowserRequestAddKeyData;
15
+ exports.getEip712BrowserRequestAddProvider = getEip712BrowserRequestAddProvider;
16
+ exports.getEip712BrowserRequestPaginatedUpsertSignaturePayloadV2 = getEip712BrowserRequestPaginatedUpsertSignaturePayloadV2;
17
+ exports.getEip712BrowserRequestPaginatedDeleteSignaturePayloadV2 = getEip712BrowserRequestPaginatedDeleteSignaturePayloadV2;
18
+ exports.getEip712BrowserRequestItemizedSignaturePayloadV2 = getEip712BrowserRequestItemizedSignaturePayloadV2;
19
+ exports.getEip712BrowserRequestClaimHandlePayload = getEip712BrowserRequestClaimHandlePayload;
20
+ exports.getEip712BrowserRequestPasskeyPublicKey = getEip712BrowserRequestPasskeyPublicKey;
21
+ const utils_1 = require("./utils");
22
+ const address_1 = require("./address");
23
+ const ethers_1 = require("ethers");
24
+ const util_1 = require("@polkadot/util");
25
+ const signature_definitions_1 = require("./signature.definitions");
26
+ /**
27
+ * Signing EIP-712 compatible signature for payload
28
+ * @param secretKey
29
+ * @param payload
30
+ * @param chain
31
+ */
32
+ async function signEip712(secretKey, payload, chain = 'Mainnet-Frequency') {
33
+ const types = getTypesFor(payload.type);
34
+ const normalizedPayload = normalizePayload(payload);
35
+ const wallet = new ethers_1.ethers.Wallet(secretKey);
36
+ // TODO: use correct chainID for different networks
37
+ // TODO: use correct contract address for different payloads
38
+ const signature = await wallet.signTypedData(signature_definitions_1.EIP712_DOMAIN_DEFAULT, types, normalizedPayload);
39
+ return { Ecdsa: signature };
40
+ }
41
+ /**
42
+ * Verify EIP-712 signatures
43
+ * @param ethereumAddress
44
+ * @param signature
45
+ * @param payload
46
+ * @param chain
47
+ */
48
+ function verifyEip712Signature(ethereumAddress, signature, payload, chain = 'Mainnet-Frequency') {
49
+ const types = getTypesFor(payload.type);
50
+ const normalizedPayload = normalizePayload(payload);
51
+ // TODO: use correct chainID for different networks
52
+ // TODO: use correct contract address for different payloads
53
+ const recoveredAddress = ethers_1.ethers.verifyTypedData(signature_definitions_1.EIP712_DOMAIN_DEFAULT, types, normalizedPayload, signature);
54
+ return recoveredAddress.toLowerCase() === ethereumAddress.toLowerCase();
55
+ }
56
+ function normalizePayload(payload) {
57
+ const clonedPayload = Object.assign({}, payload);
58
+ switch (clonedPayload.type) {
59
+ case 'PaginatedUpsertSignaturePayloadV2':
60
+ case 'PaginatedDeleteSignaturePayloadV2':
61
+ case 'ItemizedSignaturePayloadV2':
62
+ case 'PasskeyPublicKey':
63
+ case 'ClaimHandlePayload':
64
+ case 'AddProvider':
65
+ break;
66
+ case 'AddKeyData':
67
+ // convert to 20 bytes ethereum address for signature
68
+ if (clonedPayload.newPublicKey.length !== 42) {
69
+ clonedPayload.newPublicKey = (0, address_1.reverseUnifiedAddressToEthereumAddress)(payload.newPublicKey);
70
+ }
71
+ clonedPayload.newPublicKey = clonedPayload.newPublicKey.toLowerCase();
72
+ break;
73
+ default:
74
+ throw new Error(`Unsupported payload type: ${JSON.stringify(payload)}`);
75
+ }
76
+ // Remove the type field
77
+ const { type, ...payloadWithoutType } = clonedPayload;
78
+ return payloadWithoutType;
79
+ }
80
+ function getTypesFor(payloadType) {
81
+ const PAYLOAD_TYPE_DEFINITIONS = {
82
+ PaginatedUpsertSignaturePayloadV2: signature_definitions_1.PAGINATED_UPSERT_SIGNATURE_PAYLOAD_DEFINITION_V2,
83
+ PaginatedDeleteSignaturePayloadV2: signature_definitions_1.PAGINATED_DELETE_SIGNATURE_PAYLOAD_DEFINITION_V2,
84
+ ItemizedSignaturePayloadV2: signature_definitions_1.ITEMIZED_SIGNATURE_PAYLOAD_DEFINITION_V2,
85
+ PasskeyPublicKey: signature_definitions_1.PASSKEY_PUBLIC_KEY_DEFINITION,
86
+ ClaimHandlePayload: signature_definitions_1.CLAIM_HANDLE_PAYLOAD_DEFINITION,
87
+ AddKeyData: signature_definitions_1.ADD_KEY_DATA_DEFINITION,
88
+ AddProvider: signature_definitions_1.ADD_PROVIDER_DEFINITION,
89
+ };
90
+ const definition = PAYLOAD_TYPE_DEFINITIONS[payloadType];
91
+ if (!definition) {
92
+ throw new Error(`Unsupported payload type: ${payloadType}`);
93
+ }
94
+ return definition;
95
+ }
96
+ /**
97
+ * Build an AddKeyData payload for signature.
98
+ *
99
+ * @param msaId MSA ID (uint64) to add the key
100
+ * @param newPublicKey 32 bytes public key to add in hex or Uint8Array
101
+ * @param expirationBlock Block number after which this payload is invalid
102
+ */
103
+ function createAddKeyData(msaId, newPublicKey, expirationBlock) {
104
+ const parsedMsaId = typeof msaId === 'string' ? msaId : `${msaId}`;
105
+ const parsedNewPublicKey = typeof newPublicKey === 'object' ? (0, util_1.u8aToHex)(newPublicKey) : newPublicKey;
106
+ (0, utils_1.assert)((0, utils_1.isValidUint64String)(parsedMsaId), 'msaId should be a valid uint64');
107
+ (0, utils_1.assert)((0, utils_1.isValidUint32)(expirationBlock), 'expiration should be a valid uint32');
108
+ (0, utils_1.assert)((0, utils_1.isHexString)(parsedNewPublicKey), 'newPublicKey should be valid hex');
109
+ return {
110
+ type: 'AddKeyData',
111
+ msaId: parsedMsaId,
112
+ expiration: expirationBlock,
113
+ newPublicKey: parsedNewPublicKey,
114
+ };
115
+ }
116
+ /**
117
+ * Build an AddProvider payload for signature.
118
+ *
119
+ * @param authorizedMsaId MSA ID (uint64) that will be granted provider rights
120
+ * @param schemaIds One or more schema IDs (uint16) the provider may use
121
+ * @param expirationBlock Block number after which this payload is invalid
122
+ */
123
+ function createAddProvider(authorizedMsaId, schemaIds, expirationBlock) {
124
+ (0, utils_1.assert)((0, utils_1.isValidUint64String)(authorizedMsaId), 'authorizedMsaId should be a valid uint64');
125
+ (0, utils_1.assert)((0, utils_1.isValidUint32)(expirationBlock), 'expiration should be a valid uint32');
126
+ schemaIds.forEach((schemaId) => {
127
+ (0, utils_1.assert)((0, utils_1.isValidUint16)(schemaId), 'schemaId should be a valid uint16');
128
+ });
129
+ return {
130
+ type: 'AddProvider',
131
+ authorizedMsaId: authorizedMsaId.toString(),
132
+ schemaIds,
133
+ expiration: expirationBlock,
134
+ };
135
+ }
136
+ /**
137
+ * Build a ClaimHandlePayload for signature.
138
+ *
139
+ * @param handle The handle the user wishes to claim
140
+ * @param expirationBlock Block number after which this payload is invalid
141
+ */
142
+ function createClaimHandlePayload(handle, expirationBlock) {
143
+ (0, utils_1.assert)(handle.length > 0, 'handle should be a valid string');
144
+ (0, utils_1.assert)((0, utils_1.isValidUint32)(expirationBlock), 'expiration should be a valid uint32');
145
+ return {
146
+ type: 'ClaimHandlePayload',
147
+ handle,
148
+ expiration: expirationBlock,
149
+ };
150
+ }
151
+ /**
152
+ * Build a PasskeyPublicKey payload for signature.
153
+ *
154
+ * @param publicKey The passkey’s public key (hex string or raw bytes)
155
+ */
156
+ function createPasskeyPublicKey(publicKey) {
157
+ const parsedNewPublicKey = typeof publicKey === 'object' ? (0, util_1.u8aToHex)(publicKey) : publicKey;
158
+ (0, utils_1.assert)((0, utils_1.isHexString)(parsedNewPublicKey), 'publicKey should be valid hex');
159
+ return {
160
+ type: 'PasskeyPublicKey',
161
+ publicKey: parsedNewPublicKey,
162
+ };
163
+ }
164
+ function createItemizedAddAction(data) {
165
+ const parsedData = typeof data === 'object' ? (0, util_1.u8aToHex)(data) : data;
166
+ (0, utils_1.assert)((0, utils_1.isHexString)(parsedData), 'itemized data should be valid hex');
167
+ return { actionType: 'Add', data, index: 0 };
168
+ }
169
+ function createItemizedDeleteAction(index) {
170
+ (0, utils_1.assert)((0, utils_1.isValidUint16)(index), 'itemized index should be a valid uint16');
171
+ return { actionType: 'Delete', data: '0x', index };
172
+ }
173
+ /**
174
+ * Build an ItemizedSignaturePayloadV2 for signing.
175
+ *
176
+ * @param schemaId uint16 schema identifier
177
+ * @param targetHash uint32 page hash
178
+ * @param expiration uint32 expiration block
179
+ * @param actions Array of Add/Delete itemized actions
180
+ */
181
+ function createItemizedSignaturePayloadV2(schemaId, targetHash, expiration, actions) {
182
+ (0, utils_1.assert)((0, utils_1.isValidUint16)(schemaId), 'schemaId should be a valid uint16');
183
+ (0, utils_1.assert)((0, utils_1.isValidUint32)(targetHash), 'targetHash should be a valid uint32');
184
+ (0, utils_1.assert)((0, utils_1.isValidUint32)(expiration), 'expiration should be a valid uint32');
185
+ (0, utils_1.assert)(actions.length > 0, 'At least one action is required for ItemizedSignaturePayloadV2');
186
+ return {
187
+ type: 'ItemizedSignaturePayloadV2',
188
+ schemaId,
189
+ targetHash,
190
+ expiration,
191
+ actions,
192
+ };
193
+ }
194
+ /**
195
+ * Build a PaginatedDeleteSignaturePayloadV2 for signing.
196
+ *
197
+ * @param schemaId uint16 schema identifier
198
+ * @param pageId uint16 page identifier
199
+ * @param targetHash uint32 page hash
200
+ * @param expiration uint32 expiration block
201
+ */
202
+ function createPaginatedDeleteSignaturePayloadV2(schemaId, pageId, targetHash, expiration) {
203
+ (0, utils_1.assert)((0, utils_1.isValidUint16)(schemaId), 'schemaId should be a valid uint16');
204
+ (0, utils_1.assert)((0, utils_1.isValidUint16)(pageId), 'pageId should be a valid uint16');
205
+ (0, utils_1.assert)((0, utils_1.isValidUint32)(targetHash), 'targetHash should be a valid uint32');
206
+ (0, utils_1.assert)((0, utils_1.isValidUint32)(expiration), 'expiration should be a valid uint32');
207
+ return {
208
+ type: 'PaginatedDeleteSignaturePayloadV2',
209
+ schemaId,
210
+ pageId,
211
+ targetHash,
212
+ expiration,
213
+ };
214
+ }
215
+ /**
216
+ * Build a PaginatedUpsertSignaturePayloadV2 for signing.
217
+ *
218
+ * @param schemaId uint16 schema identifier
219
+ * @param pageId uint16 page identifier
220
+ * @param targetHash uint32 page hash
221
+ * @param expiration uint32 expiration block
222
+ * @param payload HexString or Uint8Array data to upsert
223
+ */
224
+ function createPaginatedUpsertSignaturePayloadV2(schemaId, pageId, targetHash, expiration, payload) {
225
+ const parsedPayload = typeof payload === 'object' ? (0, util_1.u8aToHex)(payload) : payload;
226
+ (0, utils_1.assert)((0, utils_1.isValidUint16)(schemaId), 'schemaId should be a valid uint16');
227
+ (0, utils_1.assert)((0, utils_1.isValidUint16)(pageId), 'pageId should be a valid uint16');
228
+ (0, utils_1.assert)((0, utils_1.isValidUint32)(targetHash), 'targetHash should be a valid uint32');
229
+ (0, utils_1.assert)((0, utils_1.isValidUint32)(expiration), 'expiration should be a valid uint32');
230
+ (0, utils_1.assert)((0, utils_1.isHexString)(parsedPayload), 'payload should be valid hex');
231
+ return {
232
+ type: 'PaginatedUpsertSignaturePayloadV2',
233
+ schemaId,
234
+ pageId,
235
+ targetHash,
236
+ expiration,
237
+ payload: parsedPayload,
238
+ };
239
+ }
240
+ /**
241
+ * Returns the EIP-712 browser request for a AddKeyData for signing.
242
+ *
243
+ * @param msaId MSA ID (uint64) to add the key
244
+ * @param newPublicKey 32 bytes public key to add in hex or Uint8Array
245
+ * @param expirationBlock Block number after which this payload is invalid
246
+ * @param domain
247
+ */
248
+ function getEip712BrowserRequestAddKeyData(msaId, newPublicKey, expirationBlock, domain = signature_definitions_1.EIP712_DOMAIN_DEFAULT) {
249
+ const message = createAddKeyData(msaId, newPublicKey, expirationBlock);
250
+ const normalized = normalizePayload(message);
251
+ return createEip712Payload(signature_definitions_1.ADD_KEY_DATA_DEFINITION, message.type, domain, normalized);
252
+ }
253
+ /**
254
+ * Returns the EIP-712 browser request for a AddProvider for signing.
255
+ *
256
+ * @param authorizedMsaId MSA ID (uint64) that will be granted provider rights
257
+ * @param schemaIds One or more schema IDs (uint16) the provider may use
258
+ * @param expirationBlock Block number after which this payload is invalid
259
+ * @param domain
260
+ */
261
+ function getEip712BrowserRequestAddProvider(authorizedMsaId, schemaIds, expirationBlock, domain = signature_definitions_1.EIP712_DOMAIN_DEFAULT) {
262
+ const message = createAddProvider(authorizedMsaId, schemaIds, expirationBlock);
263
+ const normalized = normalizePayload(message);
264
+ return createEip712Payload(signature_definitions_1.ADD_PROVIDER_DEFINITION, message.type, domain, normalized);
265
+ }
266
+ /**
267
+ * Returns the EIP-712 browser request for a PaginatedUpsertSignaturePayloadV2 for signing.
268
+ *
269
+ * @param schemaId uint16 schema identifier
270
+ * @param pageId uint16 page identifier
271
+ * @param targetHash uint32 page hash
272
+ * @param expiration uint32 expiration block
273
+ * @param payload HexString or Uint8Array data to upsert
274
+ * @param domain
275
+ */
276
+ function getEip712BrowserRequestPaginatedUpsertSignaturePayloadV2(schemaId, pageId, targetHash, expiration, payload, domain = signature_definitions_1.EIP712_DOMAIN_DEFAULT) {
277
+ const message = createPaginatedUpsertSignaturePayloadV2(schemaId, pageId, targetHash, expiration, payload);
278
+ const normalized = normalizePayload(message);
279
+ return createEip712Payload(signature_definitions_1.PAGINATED_UPSERT_SIGNATURE_PAYLOAD_DEFINITION_V2, message.type, domain, normalized);
280
+ }
281
+ /**
282
+ * Returns the EIP-712 browser request for a PaginatedDeleteSignaturePayloadV2 for signing.
283
+ *
284
+ * @param schemaId uint16 schema identifier
285
+ * @param pageId uint16 page identifier
286
+ * @param targetHash uint32 page hash
287
+ * @param expiration uint32 expiration block
288
+ * @param domain
289
+ */
290
+ function getEip712BrowserRequestPaginatedDeleteSignaturePayloadV2(schemaId, pageId, targetHash, expiration, domain = signature_definitions_1.EIP712_DOMAIN_DEFAULT) {
291
+ const message = createPaginatedDeleteSignaturePayloadV2(schemaId, pageId, targetHash, expiration);
292
+ const normalized = normalizePayload(message);
293
+ return createEip712Payload(signature_definitions_1.PAGINATED_DELETE_SIGNATURE_PAYLOAD_DEFINITION_V2, message.type, domain, normalized);
294
+ }
295
+ /**
296
+ * Returns the EIP-712 browser request for a ItemizedSignaturePayloadV2 for signing.
297
+ *
298
+ * @param schemaId uint16 schema identifier
299
+ * @param targetHash uint32 page hash
300
+ * @param expiration uint32 expiration block
301
+ * @param actions Array of Add/Delete itemized actions
302
+ * @param domain
303
+ */
304
+ function getEip712BrowserRequestItemizedSignaturePayloadV2(schemaId, targetHash, expiration, actions, domain = signature_definitions_1.EIP712_DOMAIN_DEFAULT) {
305
+ const message = createItemizedSignaturePayloadV2(schemaId, targetHash, expiration, actions);
306
+ const normalized = normalizePayload(message);
307
+ return createEip712Payload(signature_definitions_1.ITEMIZED_SIGNATURE_PAYLOAD_DEFINITION_V2, message.type, domain, normalized);
308
+ }
309
+ /**
310
+ * Returns the EIP-712 browser request for a ClaimHandlePayload for signing.
311
+ *
312
+ * @param handle The handle the user wishes to claim
313
+ * @param expirationBlock Block number after which this payload is invalid
314
+ * @param domain
315
+ */
316
+ function getEip712BrowserRequestClaimHandlePayload(handle, expirationBlock, domain = signature_definitions_1.EIP712_DOMAIN_DEFAULT) {
317
+ const message = createClaimHandlePayload(handle, expirationBlock);
318
+ const normalized = normalizePayload(message);
319
+ return createEip712Payload(signature_definitions_1.CLAIM_HANDLE_PAYLOAD_DEFINITION, message.type, domain, normalized);
320
+ }
321
+ /**
322
+ * Returns the EIP-712 browser request for a PasskeyPublicKey for signing.
323
+ *
324
+ * @param publicKey The passkey’s public key (hex string or raw bytes)
325
+ * @param domain
326
+ */
327
+ function getEip712BrowserRequestPasskeyPublicKey(publicKey, domain = signature_definitions_1.EIP712_DOMAIN_DEFAULT) {
328
+ const message = createPasskeyPublicKey(publicKey);
329
+ const normalized = normalizePayload(message);
330
+ return createEip712Payload(signature_definitions_1.PASSKEY_PUBLIC_KEY_DEFINITION, message.type, domain, normalized);
331
+ }
332
+ function createEip712Payload(typeDefinition, primaryType, domain, message) {
333
+ return {
334
+ types: {
335
+ ...signature_definitions_1.EIP712_DOMAIN_DEFINITION,
336
+ ...typeDefinition,
337
+ },
338
+ primaryType,
339
+ domain,
340
+ message,
341
+ };
342
+ }
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
+ }