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