@frequency-chain/ethereum-utils 1.17.0-rc4 → 1.17.0-rc5

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 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/signature.js CHANGED
@@ -18,6 +18,8 @@ exports.getEip712BrowserRequestPaginatedDeleteSignaturePayloadV2 = getEip712Brow
18
18
  exports.getEip712BrowserRequestItemizedSignaturePayloadV2 = getEip712BrowserRequestItemizedSignaturePayloadV2;
19
19
  exports.getEip712BrowserRequestClaimHandlePayload = getEip712BrowserRequestClaimHandlePayload;
20
20
  exports.getEip712BrowserRequestPasskeyPublicKey = getEip712BrowserRequestPasskeyPublicKey;
21
+ exports.getEthereumRegularSigner = getEthereumRegularSigner;
22
+ exports.getEthereumMessageSigner = getEthereumMessageSigner;
21
23
  const utils_1 = require("./utils");
22
24
  const address_1 = require("./address");
23
25
  const ethers_1 = require("ethers");
@@ -340,3 +342,49 @@ function createEip712Payload(typeDefinition, primaryType, domain, message) {
340
342
  message,
341
343
  };
342
344
  }
345
+ /**
346
+ * Returns An ethereum compatible signature for the extrinsic
347
+ * @param ethereumPair
348
+ */
349
+ function getEthereumRegularSigner(ethereumPair) {
350
+ return {
351
+ signRaw: async (payload) => {
352
+ const sig = ethereumPair.sign(payload.data);
353
+ const prefixedSignature = new Uint8Array(sig.length + 1);
354
+ prefixedSignature[0] = 2;
355
+ prefixedSignature.set(sig, 1);
356
+ const hex = (0, util_1.u8aToHex)(prefixedSignature);
357
+ return {
358
+ signature: hex,
359
+ };
360
+ },
361
+ };
362
+ }
363
+ /**
364
+ * This custom signer can get used to mimic EIP-191 message signing. By replacing the `ethereumPair.sign` with
365
+ * any wallet call we can sign any extrinsic with any wallet
366
+ * @param ethereumPair
367
+ */
368
+ function getEthereumMessageSigner(ethereumPair) {
369
+ return {
370
+ signRaw: async (payload) => {
371
+ const sig = ethereumPair.sign(prefixEthereumTags(payload.data));
372
+ const prefixedSignature = new Uint8Array(sig.length + 1);
373
+ prefixedSignature[0] = 2;
374
+ prefixedSignature.set(sig, 1);
375
+ const hex = (0, util_1.u8aToHex)(prefixedSignature);
376
+ return {
377
+ signature: hex,
378
+ };
379
+ },
380
+ };
381
+ }
382
+ /**
383
+ * prefixing with the EIP-191 for personal_sign messages (this gets wrapped automatically in Metamask)
384
+ * @param hexPayload
385
+ */
386
+ function prefixEthereumTags(hexPayload) {
387
+ const wrapped = `\x19Ethereum Signed Message:\n${hexPayload.length}${hexPayload}`;
388
+ const buffer = Buffer.from(wrapped, 'utf-8');
389
+ return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.length);
390
+ }
package/esm/address.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { encodeAddress, ethereumEncode } from '@polkadot/util-crypto';
2
2
  import { hexToU8a, u8aToHex } from '@polkadot/util';
3
3
  import { ethers } from 'ethers';
4
+ import { Keyring } from '@polkadot/api';
4
5
  /**
5
6
  * Creates a Random Ethereum key
6
7
  */
@@ -73,6 +74,30 @@ export function getSS58AccountFromEthereumAccount(accountId20Hex) {
73
74
  result.set(suffix, 20);
74
75
  return encodeAddress(result);
75
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
+ }
76
101
  function getUnified32BytesAddress(ethAddressOrPublicKey) {
77
102
  const ethAddressBytes = hexToU8a(ethereumEncode(ethAddressOrPublicKey));
78
103
  const suffix = new Uint8Array(12).fill(0xee);
package/esm/signature.js CHANGED
@@ -320,3 +320,49 @@ function createEip712Payload(typeDefinition, primaryType, domain, message) {
320
320
  message,
321
321
  };
322
322
  }
323
+ /**
324
+ * Returns An ethereum compatible signature for the extrinsic
325
+ * @param ethereumPair
326
+ */
327
+ export function getEthereumRegularSigner(ethereumPair) {
328
+ return {
329
+ signRaw: async (payload) => {
330
+ const sig = ethereumPair.sign(payload.data);
331
+ const prefixedSignature = new Uint8Array(sig.length + 1);
332
+ prefixedSignature[0] = 2;
333
+ prefixedSignature.set(sig, 1);
334
+ const hex = u8aToHex(prefixedSignature);
335
+ return {
336
+ signature: hex,
337
+ };
338
+ },
339
+ };
340
+ }
341
+ /**
342
+ * This custom signer can get used to mimic EIP-191 message signing. By replacing the `ethereumPair.sign` with
343
+ * any wallet call we can sign any extrinsic with any wallet
344
+ * @param ethereumPair
345
+ */
346
+ export function getEthereumMessageSigner(ethereumPair) {
347
+ return {
348
+ signRaw: async (payload) => {
349
+ const sig = ethereumPair.sign(prefixEthereumTags(payload.data));
350
+ const prefixedSignature = new Uint8Array(sig.length + 1);
351
+ prefixedSignature[0] = 2;
352
+ prefixedSignature.set(sig, 1);
353
+ const hex = u8aToHex(prefixedSignature);
354
+ return {
355
+ signature: hex,
356
+ };
357
+ },
358
+ };
359
+ }
360
+ /**
361
+ * prefixing with the EIP-191 for personal_sign messages (this gets wrapped automatically in Metamask)
362
+ * @param hexPayload
363
+ */
364
+ function prefixEthereumTags(hexPayload) {
365
+ const wrapped = `\x19Ethereum Signed Message:\n${hexPayload.length}${hexPayload}`;
366
+ const buffer = Buffer.from(wrapped, 'utf-8');
367
+ return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.length);
368
+ }
package/index.d.ts CHANGED
@@ -22,6 +22,8 @@ declare const _default: {
22
22
  getEip712BrowserRequestItemizedSignaturePayloadV2(schemaId: number, targetHash: number, expiration: number, actions: payloads.ItemizedAction[], domain?: payloads.EipDomainPayload): unknown;
23
23
  getEip712BrowserRequestClaimHandlePayload(handle: string, expirationBlock: number, domain?: payloads.EipDomainPayload): unknown;
24
24
  getEip712BrowserRequestPasskeyPublicKey(publicKey: payloads.HexString | Uint8Array, domain?: payloads.EipDomainPayload): unknown;
25
+ getEthereumRegularSigner(ethereumPair: import("@polkadot/keyring/types").KeyringPair): import("@polkadot/types/types").Signer;
26
+ getEthereumMessageSigner(ethereumPair: import("@polkadot/keyring/types").KeyringPair): import("@polkadot/types/types").Signer;
25
27
  EIP712_DOMAIN_DEFINITION: {
26
28
  EIP712Domain: {
27
29
  name: string;
@@ -81,5 +83,7 @@ declare const _default: {
81
83
  getUnifiedPublicKey(pair: import("@polkadot/keyring/types").KeyringPair): Uint8Array;
82
84
  reverseUnifiedAddressToEthereumAddress(unifiedAddress: payloads.HexString): payloads.HexString;
83
85
  getSS58AccountFromEthereumAccount(accountId20Hex: string): string;
86
+ getKeyringPairFromSecp256k1PrivateKey(secretKey: Uint8Array): import("@polkadot/keyring/types").KeyringPair;
87
+ getAccountId20MultiAddress(pair: import("@polkadot/keyring/types").KeyringPair): payloads.Address20MultiAddress;
84
88
  };
85
89
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frequency-chain/ethereum-utils",
3
- "version": "1.17.0-rc4",
3
+ "version": "1.17.0-rc5",
4
4
  "bugs": {
5
5
  "url": "https://github.com/frequency-chain/frequency/issues"
6
6
  },
@@ -15,9 +15,9 @@
15
15
  "author": "frequency-chain",
16
16
  "license": "Apache-2.0",
17
17
  "dependencies": {
18
- "@polkadot/api": "^15.10.2",
19
- "@polkadot/util": "13.4.4",
20
- "ethers": "^6.14.0"
18
+ "@polkadot/api": "^16.1.1",
19
+ "@polkadot/util": "13.5.1",
20
+ "ethers": "^6.14.3"
21
21
  },
22
22
  "module": "./esm/index.js",
23
23
  "types": "index.d.ts",
package/payloads.d.ts CHANGED
@@ -2,6 +2,9 @@ export type HexString = `0x${string}`;
2
2
  export interface EcdsaSignature {
3
3
  Ecdsa: HexString;
4
4
  }
5
+ export interface Address20MultiAddress {
6
+ Address20: number[];
7
+ }
5
8
  export type ChainType = 'Mainnet-Frequency' | 'Paseo-Testnet-Frequency' | 'Dev';
6
9
  export interface AddressWrapper {
7
10
  ethereumAddress: HexString;
package/signature.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { AddKeyData, AddProvider, ChainType, ClaimHandlePayload, EcdsaSignature, ItemizedSignaturePayloadV2, PaginatedDeleteSignaturePayloadV2, PaginatedUpsertSignaturePayloadV2, PasskeyPublicKey, SupportedPayload, HexString, AddItemizedAction, DeleteItemizedAction, ItemizedAction, EipDomainPayload } from './payloads';
2
+ import { KeyringPair } from '@polkadot/keyring/types';
3
+ import { Signer } from '@polkadot/types/types';
2
4
  /**
3
5
  * Signing EIP-712 compatible signature for payload
4
6
  * @param secretKey
@@ -137,3 +139,14 @@ export declare function getEip712BrowserRequestClaimHandlePayload(handle: string
137
139
  * @param domain
138
140
  */
139
141
  export declare function getEip712BrowserRequestPasskeyPublicKey(publicKey: HexString | Uint8Array, domain?: EipDomainPayload): unknown;
142
+ /**
143
+ * Returns An ethereum compatible signature for the extrinsic
144
+ * @param ethereumPair
145
+ */
146
+ export declare function getEthereumRegularSigner(ethereumPair: KeyringPair): Signer;
147
+ /**
148
+ * This custom signer can get used to mimic EIP-191 message signing. By replacing the `ethereumPair.sign` with
149
+ * any wallet call we can sign any extrinsic with any wallet
150
+ * @param ethereumPair
151
+ */
152
+ export declare function getEthereumMessageSigner(ethereumPair: KeyringPair): Signer;