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