@frequency-chain/ethereum-utils 0.0.0-0eb70a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +8 -0
- package/LICENSE +201 -0
- package/README.md +74 -0
- package/address.d.ts +37 -0
- package/browser/frequency-ethereum-utils.esm.min.js +20302 -0
- package/browser/frequency-ethereum-utils.umd.min.js +11548 -0
- package/cjs/address.js +118 -0
- package/cjs/index.js +47 -0
- package/cjs/package.json +3 -0
- package/cjs/payloads.js +2 -0
- package/cjs/signature.definitions.js +214 -0
- package/cjs/signature.js +555 -0
- package/cjs/utils.js +45 -0
- package/esm/address.js +108 -0
- package/esm/index.js +9 -0
- package/esm/package.json +3 -0
- package/esm/payloads.js +1 -0
- package/esm/signature.definitions.js +211 -0
- package/esm/signature.js +526 -0
- package/esm/utils.js +38 -0
- package/index.d.ts +115 -0
- package/package.json +35 -0
- package/payloads.d.ts +104 -0
- package/signature.d.ts +217 -0
- package/signature.definitions.d.ts +130 -0
- package/utils.d.ts +23 -0
package/esm/address.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { encodeAddress, ethereumEncode } from '@polkadot/util-crypto';
|
|
2
|
+
import { hexToU8a, u8aToHex } from '@polkadot/util';
|
|
3
|
+
import { ethers } from 'ethers';
|
|
4
|
+
import { Keyring } from '@polkadot/api';
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Random Ethereum key
|
|
7
|
+
*/
|
|
8
|
+
export function createRandomKey() {
|
|
9
|
+
const k = ethers.Wallet.createRandom();
|
|
10
|
+
return {
|
|
11
|
+
publicKey: k.publicKey,
|
|
12
|
+
privateKey: k.privateKey,
|
|
13
|
+
address: {
|
|
14
|
+
ethereumAddress: k.address,
|
|
15
|
+
unifiedAddress: getUnified32BytesAddress(k.address),
|
|
16
|
+
unifiedAddressSS58: getSS58AccountFromEthereumAccount(k.address),
|
|
17
|
+
},
|
|
18
|
+
mnemonic: k.mnemonic?.phrase ?? '',
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Create a partial KeyringPair from an Ethereum address
|
|
23
|
+
*/
|
|
24
|
+
export function ethereumAddressToKeyringPair(ethereumAddress) {
|
|
25
|
+
return {
|
|
26
|
+
type: 'ethereum',
|
|
27
|
+
address: ethereumAddress.toHex(),
|
|
28
|
+
addressRaw: ethereumAddress,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns unified 32 bytes SS58 accountId
|
|
33
|
+
* @param pair
|
|
34
|
+
*/
|
|
35
|
+
export function getUnifiedAddress(pair) {
|
|
36
|
+
if ('ethereum' === pair.type) {
|
|
37
|
+
const etheAddressHex = ethereumEncode(pair.publicKey || pair.address);
|
|
38
|
+
return getSS58AccountFromEthereumAccount(etheAddressHex);
|
|
39
|
+
}
|
|
40
|
+
if (pair.type === 'ecdsa') {
|
|
41
|
+
throw new Error('Ecdsa key type is not supported and it should be replaced with ethereum ones!');
|
|
42
|
+
}
|
|
43
|
+
return pair.address;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns ethereum style public key with suffixed 0xee example: 0x19a701d23f0ee1748b5d5f883cb833943096c6c4eeeeeeeeeeeeeeeeeeeeeeee
|
|
47
|
+
* @param pair
|
|
48
|
+
*/
|
|
49
|
+
export function getUnifiedPublicKey(pair) {
|
|
50
|
+
if ('ethereum' === pair.type) {
|
|
51
|
+
const unifiedHex = getUnified32BytesAddress(u8aToHex(pair.publicKey));
|
|
52
|
+
return hexToU8a(unifiedHex);
|
|
53
|
+
}
|
|
54
|
+
if (pair.type === 'ecdsa') {
|
|
55
|
+
throw new Error('Ecdsa key type is not supported and it should be replaced with ethereum ones!');
|
|
56
|
+
}
|
|
57
|
+
return pair.publicKey;
|
|
58
|
+
}
|
|
59
|
+
export function reverseUnifiedAddressToEthereumAddress(unifiedAddress) {
|
|
60
|
+
if (!unifiedAddress.toLowerCase().endsWith('ee'.repeat(12))) {
|
|
61
|
+
throw new Error(`Address ${unifiedAddress} is not reversible!`);
|
|
62
|
+
}
|
|
63
|
+
return `${unifiedAddress.substring(0, 42)}`;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* converts an ethereum account to SS58 format
|
|
67
|
+
* @param accountId20Hex
|
|
68
|
+
*/
|
|
69
|
+
export function getSS58AccountFromEthereumAccount(accountId20Hex) {
|
|
70
|
+
const addressBytes = hexToU8a(accountId20Hex);
|
|
71
|
+
const suffix = new Uint8Array(12).fill(0xee);
|
|
72
|
+
const result = new Uint8Array(32);
|
|
73
|
+
result.set(addressBytes, 0);
|
|
74
|
+
result.set(suffix, 20);
|
|
75
|
+
return encodeAddress(result);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
*
|
|
79
|
+
* @param secretKey of secp256k1 keypair exported from any wallet (should be 32 bytes)
|
|
80
|
+
*/
|
|
81
|
+
export function getKeyringPairFromSecp256k1PrivateKey(secretKey) {
|
|
82
|
+
const publicKey = ethers.SigningKey.computePublicKey(secretKey, true);
|
|
83
|
+
const keypair = {
|
|
84
|
+
secretKey,
|
|
85
|
+
publicKey: hexToU8a(publicKey),
|
|
86
|
+
};
|
|
87
|
+
return new Keyring({ type: 'ethereum' }).createFromPair(keypair, undefined, 'ethereum');
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Convert a keyPair into a 20 byte ethereum address
|
|
91
|
+
* @param pair
|
|
92
|
+
*/
|
|
93
|
+
export function getAccountId20MultiAddress(pair) {
|
|
94
|
+
if (pair.type !== 'ethereum') {
|
|
95
|
+
throw new Error(`Only ethereum keys are supported!`);
|
|
96
|
+
}
|
|
97
|
+
const etheAddress = ethereumEncode(pair.publicKey || pair.address);
|
|
98
|
+
const ethAddress20 = Array.from(hexToU8a(etheAddress));
|
|
99
|
+
return { Address20: ethAddress20 };
|
|
100
|
+
}
|
|
101
|
+
function getUnified32BytesAddress(ethAddressOrPublicKey) {
|
|
102
|
+
const ethAddressBytes = hexToU8a(ethereumEncode(ethAddressOrPublicKey));
|
|
103
|
+
const suffix = new Uint8Array(12).fill(0xee);
|
|
104
|
+
const result = new Uint8Array(32);
|
|
105
|
+
result.set(ethAddressBytes, 0);
|
|
106
|
+
result.set(suffix, 20);
|
|
107
|
+
return u8aToHex(result);
|
|
108
|
+
}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as address from './address.js';
|
|
2
|
+
import * as payloads from './payloads.js';
|
|
3
|
+
import * as signature from './signature.js';
|
|
4
|
+
import * as signatureDefinitions from './signature.definitions.js';
|
|
5
|
+
export * from './payloads.js';
|
|
6
|
+
export * from './signature.js';
|
|
7
|
+
export * from './signature.definitions.js';
|
|
8
|
+
export * from './address.js';
|
|
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,211 @@
|
|
|
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 2091 for mainnet
|
|
22
|
+
export const EIP712_DOMAIN_MAINNET = {
|
|
23
|
+
name: 'Frequency',
|
|
24
|
+
version: '1',
|
|
25
|
+
chainId: '0x082B',
|
|
26
|
+
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
|
|
27
|
+
};
|
|
28
|
+
// using pallet_revive test chain ID for testnet/dev
|
|
29
|
+
export const EIP712_DOMAIN_TESTNET = {
|
|
30
|
+
name: 'Frequency',
|
|
31
|
+
version: '1',
|
|
32
|
+
chainId: '0x190f1b44',
|
|
33
|
+
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
|
|
34
|
+
};
|
|
35
|
+
export const ADD_PROVIDER_DEFINITION = {
|
|
36
|
+
AddProvider: [
|
|
37
|
+
{
|
|
38
|
+
name: 'authorizedMsaId',
|
|
39
|
+
type: 'uint64',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: 'schemaIds',
|
|
43
|
+
type: 'uint16[]',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: 'expiration',
|
|
47
|
+
type: 'uint32',
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
};
|
|
51
|
+
export const ADD_KEY_DATA_DEFINITION = {
|
|
52
|
+
AddKeyData: [
|
|
53
|
+
{
|
|
54
|
+
name: 'msaId',
|
|
55
|
+
type: 'uint64',
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: 'expiration',
|
|
59
|
+
type: 'uint32',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: 'newPublicKey',
|
|
63
|
+
type: 'address',
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
};
|
|
67
|
+
export const AUTHORIZED_KEY_DATA_DEFINITION = {
|
|
68
|
+
AuthorizedKeyData: [
|
|
69
|
+
{
|
|
70
|
+
name: 'msaId',
|
|
71
|
+
type: 'uint64',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: 'expiration',
|
|
75
|
+
type: 'uint32',
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
name: 'authorizedPublicKey',
|
|
79
|
+
type: 'address',
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
};
|
|
83
|
+
export const RECOVERY_COMMITMENT_PAYLOAD_DEFINITION = {
|
|
84
|
+
RecoveryCommitmentPayload: [
|
|
85
|
+
{
|
|
86
|
+
name: 'recoveryCommitment',
|
|
87
|
+
type: 'bytes',
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: 'expiration',
|
|
91
|
+
type: 'uint32',
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
};
|
|
95
|
+
export const CLAIM_HANDLE_PAYLOAD_DEFINITION = {
|
|
96
|
+
ClaimHandlePayload: [
|
|
97
|
+
{
|
|
98
|
+
name: 'handle',
|
|
99
|
+
type: 'string',
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
name: 'expiration',
|
|
103
|
+
type: 'uint32',
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
};
|
|
107
|
+
export const PASSKEY_PUBLIC_KEY_DEFINITION = {
|
|
108
|
+
PasskeyPublicKey: [
|
|
109
|
+
{
|
|
110
|
+
name: 'publicKey',
|
|
111
|
+
type: 'bytes',
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
};
|
|
115
|
+
export const PAGINATED_DELETE_SIGNATURE_PAYLOAD_DEFINITION_V2 = {
|
|
116
|
+
PaginatedDeleteSignaturePayloadV2: [
|
|
117
|
+
{
|
|
118
|
+
name: 'schemaId',
|
|
119
|
+
type: 'uint16',
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
name: 'pageId',
|
|
123
|
+
type: 'uint16',
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
name: 'targetHash',
|
|
127
|
+
type: 'uint32',
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
name: 'expiration',
|
|
131
|
+
type: 'uint32',
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
};
|
|
135
|
+
export const PAGINATED_UPSERT_SIGNATURE_PAYLOAD_DEFINITION_V2 = {
|
|
136
|
+
PaginatedUpsertSignaturePayloadV2: [
|
|
137
|
+
{
|
|
138
|
+
name: 'schemaId',
|
|
139
|
+
type: 'uint16',
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
name: 'pageId',
|
|
143
|
+
type: 'uint16',
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: 'targetHash',
|
|
147
|
+
type: 'uint32',
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
name: 'expiration',
|
|
151
|
+
type: 'uint32',
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
name: 'payload',
|
|
155
|
+
type: 'bytes',
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
};
|
|
159
|
+
export const ITEMIZED_SIGNATURE_PAYLOAD_DEFINITION_V2 = {
|
|
160
|
+
ItemizedSignaturePayloadV2: [
|
|
161
|
+
{
|
|
162
|
+
name: 'schemaId',
|
|
163
|
+
type: 'uint16',
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
name: 'targetHash',
|
|
167
|
+
type: 'uint32',
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
name: 'expiration',
|
|
171
|
+
type: 'uint32',
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: 'actions',
|
|
175
|
+
type: 'ItemAction[]',
|
|
176
|
+
},
|
|
177
|
+
],
|
|
178
|
+
ItemAction: [
|
|
179
|
+
{ name: 'actionType', type: 'string' },
|
|
180
|
+
{ name: 'data', type: 'bytes' },
|
|
181
|
+
{ name: 'index', type: 'uint16' },
|
|
182
|
+
],
|
|
183
|
+
};
|
|
184
|
+
export const SIWF_SIGNED_REQUEST_PAYLOAD_DEFINITION = {
|
|
185
|
+
SiwfSignedRequestPayload: [
|
|
186
|
+
{
|
|
187
|
+
name: 'callback',
|
|
188
|
+
type: 'string',
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
name: 'permissions',
|
|
192
|
+
type: 'uint16[]',
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
name: 'userIdentifierAdminUrl',
|
|
196
|
+
type: 'string',
|
|
197
|
+
},
|
|
198
|
+
],
|
|
199
|
+
};
|
|
200
|
+
const PAYLOAD_DEFINITIONS = [
|
|
201
|
+
ADD_PROVIDER_DEFINITION,
|
|
202
|
+
ADD_KEY_DATA_DEFINITION,
|
|
203
|
+
AUTHORIZED_KEY_DATA_DEFINITION,
|
|
204
|
+
CLAIM_HANDLE_PAYLOAD_DEFINITION,
|
|
205
|
+
PASSKEY_PUBLIC_KEY_DEFINITION,
|
|
206
|
+
PAGINATED_DELETE_SIGNATURE_PAYLOAD_DEFINITION_V2,
|
|
207
|
+
PAGINATED_UPSERT_SIGNATURE_PAYLOAD_DEFINITION_V2,
|
|
208
|
+
RECOVERY_COMMITMENT_PAYLOAD_DEFINITION,
|
|
209
|
+
ITEMIZED_SIGNATURE_PAYLOAD_DEFINITION_V2,
|
|
210
|
+
SIWF_SIGNED_REQUEST_PAYLOAD_DEFINITION,
|
|
211
|
+
];
|