@frequency-chain/ethereum-utils 0.0.0-0b0810

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,118 @@
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
+ exports.getKeyringPairFromSecp256k1PrivateKey = getKeyringPairFromSecp256k1PrivateKey;
10
+ exports.getAccountId20MultiAddress = getAccountId20MultiAddress;
11
+ const util_crypto_1 = require("@polkadot/util-crypto");
12
+ const util_1 = require("@polkadot/util");
13
+ const ethers_1 = require("ethers");
14
+ const api_1 = require("@polkadot/api");
15
+ /**
16
+ * Creates a Random Ethereum key
17
+ */
18
+ function createRandomKey() {
19
+ const k = ethers_1.ethers.Wallet.createRandom();
20
+ return {
21
+ publicKey: k.publicKey,
22
+ privateKey: k.privateKey,
23
+ address: {
24
+ ethereumAddress: k.address,
25
+ unifiedAddress: getUnified32BytesAddress(k.address),
26
+ unifiedAddressSS58: getSS58AccountFromEthereumAccount(k.address),
27
+ },
28
+ mnemonic: k.mnemonic?.phrase ?? '',
29
+ };
30
+ }
31
+ /**
32
+ * Create a partial KeyringPair from an Ethereum address
33
+ */
34
+ function ethereumAddressToKeyringPair(ethereumAddress) {
35
+ return {
36
+ type: 'ethereum',
37
+ address: ethereumAddress.toHex(),
38
+ addressRaw: ethereumAddress,
39
+ };
40
+ }
41
+ /**
42
+ * Returns unified 32 bytes SS58 accountId
43
+ * @param pair
44
+ */
45
+ function getUnifiedAddress(pair) {
46
+ if ('ethereum' === pair.type) {
47
+ const etheAddressHex = (0, util_crypto_1.ethereumEncode)(pair.publicKey || pair.address);
48
+ return getSS58AccountFromEthereumAccount(etheAddressHex);
49
+ }
50
+ if (pair.type === 'ecdsa') {
51
+ throw new Error('Ecdsa key type is not supported and it should be replaced with ethereum ones!');
52
+ }
53
+ return pair.address;
54
+ }
55
+ /**
56
+ * Returns ethereum style public key with suffixed 0xee example: 0x19a701d23f0ee1748b5d5f883cb833943096c6c4eeeeeeeeeeeeeeeeeeeeeeee
57
+ * @param pair
58
+ */
59
+ function getUnifiedPublicKey(pair) {
60
+ if ('ethereum' === pair.type) {
61
+ const unifiedHex = getUnified32BytesAddress((0, util_1.u8aToHex)(pair.publicKey));
62
+ return (0, util_1.hexToU8a)(unifiedHex);
63
+ }
64
+ if (pair.type === 'ecdsa') {
65
+ throw new Error('Ecdsa key type is not supported and it should be replaced with ethereum ones!');
66
+ }
67
+ return pair.publicKey;
68
+ }
69
+ function reverseUnifiedAddressToEthereumAddress(unifiedAddress) {
70
+ if (!unifiedAddress.toLowerCase().endsWith('ee'.repeat(12))) {
71
+ throw new Error(`Address ${unifiedAddress} is not reversible!`);
72
+ }
73
+ return `${unifiedAddress.substring(0, 42)}`;
74
+ }
75
+ /**
76
+ * converts an ethereum account to SS58 format
77
+ * @param accountId20Hex
78
+ */
79
+ function getSS58AccountFromEthereumAccount(accountId20Hex) {
80
+ const addressBytes = (0, util_1.hexToU8a)(accountId20Hex);
81
+ const suffix = new Uint8Array(12).fill(0xee);
82
+ const result = new Uint8Array(32);
83
+ result.set(addressBytes, 0);
84
+ result.set(suffix, 20);
85
+ return (0, util_crypto_1.encodeAddress)(result);
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
+ }
111
+ function getUnified32BytesAddress(ethAddressOrPublicKey) {
112
+ const ethAddressBytes = (0, util_1.hexToU8a)((0, util_crypto_1.ethereumEncode)(ethAddressOrPublicKey));
113
+ const suffix = new Uint8Array(12).fill(0xee);
114
+ const result = new Uint8Array(32);
115
+ result.set(ethAddressBytes, 0);
116
+ result.set(suffix, 20);
117
+ return (0, util_1.u8aToHex)(result);
118
+ }
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.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
+ 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,214 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
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.RECOVERY_COMMITMENT_PAYLOAD_DEFINITION = exports.AUTHORIZED_KEY_DATA_DEFINITION = exports.ADD_KEY_DATA_DEFINITION = exports.ADD_PROVIDER_DEFINITION = exports.EIP712_DOMAIN_TESTNET = exports.EIP712_DOMAIN_MAINNET = 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 2091 for mainnet
25
+ exports.EIP712_DOMAIN_MAINNET = {
26
+ name: 'Frequency',
27
+ version: '1',
28
+ chainId: '0x082B',
29
+ verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
30
+ };
31
+ // using pallet_revive test chain ID for testnet/dev
32
+ exports.EIP712_DOMAIN_TESTNET = {
33
+ name: 'Frequency',
34
+ version: '1',
35
+ chainId: '0x190f1b44',
36
+ verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
37
+ };
38
+ exports.ADD_PROVIDER_DEFINITION = {
39
+ AddProvider: [
40
+ {
41
+ name: 'authorizedMsaId',
42
+ type: 'uint64',
43
+ },
44
+ {
45
+ name: 'schemaIds',
46
+ type: 'uint16[]',
47
+ },
48
+ {
49
+ name: 'expiration',
50
+ type: 'uint32',
51
+ },
52
+ ],
53
+ };
54
+ exports.ADD_KEY_DATA_DEFINITION = {
55
+ AddKeyData: [
56
+ {
57
+ name: 'msaId',
58
+ type: 'uint64',
59
+ },
60
+ {
61
+ name: 'expiration',
62
+ type: 'uint32',
63
+ },
64
+ {
65
+ name: 'newPublicKey',
66
+ type: 'address',
67
+ },
68
+ ],
69
+ };
70
+ exports.AUTHORIZED_KEY_DATA_DEFINITION = {
71
+ AuthorizedKeyData: [
72
+ {
73
+ name: 'msaId',
74
+ type: 'uint64',
75
+ },
76
+ {
77
+ name: 'expiration',
78
+ type: 'uint32',
79
+ },
80
+ {
81
+ name: 'authorizedPublicKey',
82
+ type: 'address',
83
+ },
84
+ ],
85
+ };
86
+ exports.RECOVERY_COMMITMENT_PAYLOAD_DEFINITION = {
87
+ RecoveryCommitmentPayload: [
88
+ {
89
+ name: 'recoveryCommitment',
90
+ type: 'bytes',
91
+ },
92
+ {
93
+ name: 'expiration',
94
+ type: 'uint32',
95
+ },
96
+ ],
97
+ };
98
+ exports.CLAIM_HANDLE_PAYLOAD_DEFINITION = {
99
+ ClaimHandlePayload: [
100
+ {
101
+ name: 'handle',
102
+ type: 'string',
103
+ },
104
+ {
105
+ name: 'expiration',
106
+ type: 'uint32',
107
+ },
108
+ ],
109
+ };
110
+ exports.PASSKEY_PUBLIC_KEY_DEFINITION = {
111
+ PasskeyPublicKey: [
112
+ {
113
+ name: 'publicKey',
114
+ type: 'bytes',
115
+ },
116
+ ],
117
+ };
118
+ exports.PAGINATED_DELETE_SIGNATURE_PAYLOAD_DEFINITION_V2 = {
119
+ PaginatedDeleteSignaturePayloadV2: [
120
+ {
121
+ name: 'schemaId',
122
+ type: 'uint16',
123
+ },
124
+ {
125
+ name: 'pageId',
126
+ type: 'uint16',
127
+ },
128
+ {
129
+ name: 'targetHash',
130
+ type: 'uint32',
131
+ },
132
+ {
133
+ name: 'expiration',
134
+ type: 'uint32',
135
+ },
136
+ ],
137
+ };
138
+ exports.PAGINATED_UPSERT_SIGNATURE_PAYLOAD_DEFINITION_V2 = {
139
+ PaginatedUpsertSignaturePayloadV2: [
140
+ {
141
+ name: 'schemaId',
142
+ type: 'uint16',
143
+ },
144
+ {
145
+ name: 'pageId',
146
+ type: 'uint16',
147
+ },
148
+ {
149
+ name: 'targetHash',
150
+ type: 'uint32',
151
+ },
152
+ {
153
+ name: 'expiration',
154
+ type: 'uint32',
155
+ },
156
+ {
157
+ name: 'payload',
158
+ type: 'bytes',
159
+ },
160
+ ],
161
+ };
162
+ exports.ITEMIZED_SIGNATURE_PAYLOAD_DEFINITION_V2 = {
163
+ ItemizedSignaturePayloadV2: [
164
+ {
165
+ name: 'schemaId',
166
+ type: 'uint16',
167
+ },
168
+ {
169
+ name: 'targetHash',
170
+ type: 'uint32',
171
+ },
172
+ {
173
+ name: 'expiration',
174
+ type: 'uint32',
175
+ },
176
+ {
177
+ name: 'actions',
178
+ type: 'ItemAction[]',
179
+ },
180
+ ],
181
+ ItemAction: [
182
+ { name: 'actionType', type: 'string' },
183
+ { name: 'data', type: 'bytes' },
184
+ { name: 'index', type: 'uint16' },
185
+ ],
186
+ };
187
+ exports.SIWF_SIGNED_REQUEST_PAYLOAD_DEFINITION = {
188
+ SiwfSignedRequestPayload: [
189
+ {
190
+ name: 'callback',
191
+ type: 'string',
192
+ },
193
+ {
194
+ name: 'permissions',
195
+ type: 'uint16[]',
196
+ },
197
+ {
198
+ name: 'userIdentifierAdminUrl',
199
+ type: 'string',
200
+ },
201
+ ],
202
+ };
203
+ const PAYLOAD_DEFINITIONS = [
204
+ exports.ADD_PROVIDER_DEFINITION,
205
+ exports.ADD_KEY_DATA_DEFINITION,
206
+ exports.AUTHORIZED_KEY_DATA_DEFINITION,
207
+ exports.CLAIM_HANDLE_PAYLOAD_DEFINITION,
208
+ exports.PASSKEY_PUBLIC_KEY_DEFINITION,
209
+ exports.PAGINATED_DELETE_SIGNATURE_PAYLOAD_DEFINITION_V2,
210
+ exports.PAGINATED_UPSERT_SIGNATURE_PAYLOAD_DEFINITION_V2,
211
+ exports.RECOVERY_COMMITMENT_PAYLOAD_DEFINITION,
212
+ exports.ITEMIZED_SIGNATURE_PAYLOAD_DEFINITION_V2,
213
+ exports.SIWF_SIGNED_REQUEST_PAYLOAD_DEFINITION,
214
+ ];