@metamask-previews/eth-simple-keyring 6.0.2-38c4bd5
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/CHANGELOG.md +72 -0
- package/LICENSE +15 -0
- package/README.md +133 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/simple-keyring.d.ts +34 -0
- package/dist/simple-keyring.js +178 -0
- package/dist/simple-keyring.js.map +1 -0
- package/package.json +76 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [6.0.2]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Bump `@metamask/utils` from `^8.1.0` to `^9.0.0` ([#177](https://github.com/MetaMask/eth-simple-keyring/pull/177))
|
|
15
|
+
|
|
16
|
+
## [6.0.1]
|
|
17
|
+
|
|
18
|
+
### Fixed
|
|
19
|
+
|
|
20
|
+
- Treat `undefined` and `null` as empty array in deserialize function ([#163](https://github.com/MetaMask/eth-simple-keyring/pull/163))
|
|
21
|
+
|
|
22
|
+
## [6.0.0]
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
|
|
26
|
+
- **BREAKING**: Increase minimum Node.js version to 16 ([#152](https://github.com/MetaMask/eth-simple-keyring/pull/152))
|
|
27
|
+
- **BREAKING**: Bump @metamask/eth-sig-util from ^6.0.1 to ^7.0.0 ([#156](https://github.com/MetaMask/eth-simple-keyring/pull/156))
|
|
28
|
+
- Bump @metamask/utils from ^5.0.0 to ^8.1.0 ([#153](https://github.com/MetaMask/eth-simple-keyring/pull/153))
|
|
29
|
+
- Bump ethereum-cryptography from ^1.2.0 to ^2.1.2 ([#153](https://github.com/MetaMask/eth-simple-keyring/pull/153))
|
|
30
|
+
|
|
31
|
+
## [5.1.1]
|
|
32
|
+
|
|
33
|
+
### Fixed
|
|
34
|
+
|
|
35
|
+
- Treat `undefined` and `null` as empty array in deserialize function ([#166](https://github.com/MetaMask/eth-simple-keyring/pull/166))
|
|
36
|
+
|
|
37
|
+
## [5.1.0]
|
|
38
|
+
|
|
39
|
+
### Changed
|
|
40
|
+
|
|
41
|
+
- Export TypeScript interfaces ([#140](https://github.com/MetaMask/eth-simple-keyring/pull/140))
|
|
42
|
+
- Update all dependencies ([#140](https://github.com/MetaMask/eth-simple-keyring/pull/140)) ([#149](https://github.com/MetaMask/eth-simple-keyring/pull/149))
|
|
43
|
+
|
|
44
|
+
### Fixed
|
|
45
|
+
|
|
46
|
+
- Add `validateMessage` option to `signMessage` to configure if runtime-validation should be done that input string is hex (default: `true`) ([#148](https://github.com/MetaMask/eth-simple-keyring/pull/148))
|
|
47
|
+
|
|
48
|
+
## [5.0.0]
|
|
49
|
+
|
|
50
|
+
### Changed
|
|
51
|
+
|
|
52
|
+
- **BREAKING:** Makes version-specific `signTypedData` methods private ([#84](https://github.com/MetaMask/eth-simple-keyring/pull/84))
|
|
53
|
+
- Consumers should use the generic `signTypedData` method and pass the version they'd like as a property in the options argument.
|
|
54
|
+
- **BREAKING:** Makes the `wallets` property private ([#87](https://github.com/MetaMask/eth-simple-keyring/pull/87))
|
|
55
|
+
- Consumers should not use this property as it is intended for internal use only.
|
|
56
|
+
- **BREAKING:** Makes `getPrivateKeyFor` a private method ([#83](https://github.com/MetaMask/eth-simple-keyring/pull/83))
|
|
57
|
+
- Consumers who wish to get the private key for a given account should use the `exportAccount` method.
|
|
58
|
+
- **BREAKING:** Set the minimum Node.js version to 14 ([#68](https://github.com/MetaMask/eth-simple-keyring/pull/68)) ([#109](https://github.com/MetaMask/eth-simple-keyring/pull/109))
|
|
59
|
+
- Always return rejected Promise upon failure ([#85](https://github.com/MetaMask/eth-simple-keyring/pull/85))
|
|
60
|
+
|
|
61
|
+
### Removed
|
|
62
|
+
|
|
63
|
+
- **BREAKING:** Remove redundant `newGethSignMessage` method ([#72](https://github.com/MetaMask/eth-simple-keyring/pull/72))
|
|
64
|
+
- Consumers can use `signPersonalMessage` method as a replacement for `newGethSignMessage`.
|
|
65
|
+
|
|
66
|
+
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@6.0.2...HEAD
|
|
67
|
+
[6.0.2]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@6.0.1...@metamask/eth-simple-keyring@6.0.2
|
|
68
|
+
[6.0.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@6.0.0...@metamask/eth-simple-keyring@6.0.1
|
|
69
|
+
[6.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@5.1.1...@metamask/eth-simple-keyring@6.0.0
|
|
70
|
+
[5.1.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@5.1.0...@metamask/eth-simple-keyring@5.1.1
|
|
71
|
+
[5.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-simple-keyring@5.0.0...@metamask/eth-simple-keyring@5.1.0
|
|
72
|
+
[5.0.0]: https://github.com/MetaMask/accounts/releases/tag/@metamask/eth-simple-keyring@5.0.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 MetaMask
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# Simple Keyring
|
|
2
|
+
|
|
3
|
+
A simple JS class wrapped around [ethereumjs-wallet](https://github.com/ethereumjs/ethereumjs-wallet) designed to expose an interface common to many different signing strategies to be used in a `KeyringController`; such as the one used in [MetaMask](https://metamask.io/)
|
|
4
|
+
|
|
5
|
+
## The Keyring Class Protocol
|
|
6
|
+
|
|
7
|
+
One of the goals of this class is to allow developers to easily add new signing strategies to MetaMask. We call these signing strategies Keyrings, because they can manage multiple keys.
|
|
8
|
+
|
|
9
|
+
### Keyring.type
|
|
10
|
+
|
|
11
|
+
A class property that returns a unique string describing the Keyring.
|
|
12
|
+
This is the only class property or method, the remaining methods are instance methods.
|
|
13
|
+
|
|
14
|
+
### constructor( options )
|
|
15
|
+
|
|
16
|
+
As a Javascript class, your Keyring object will be used to instantiate new Keyring instances using the new keyword. For example:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
const keyring = new YourKeyringClass(options);
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The constructor currently receives an options object that will be defined by your keyring-building UI, once the user has gone through the steps required for you to fully instantiate a new keyring. For example, choosing a pattern for a vanity account, or entering a seed phrase.
|
|
23
|
+
|
|
24
|
+
We haven't defined the protocol for this account-generating UI yet, so for now please ensure your Keyring behaves nicely when not passed any options object.
|
|
25
|
+
|
|
26
|
+
## Keyring Instance Methods
|
|
27
|
+
|
|
28
|
+
All below instance methods must return Promises to allow asynchronous resolution.
|
|
29
|
+
|
|
30
|
+
### serialize()
|
|
31
|
+
|
|
32
|
+
In this method, you must return any JSON-serializable JavaScript object that you like. It will be encoded to a string, encrypted with the user's password, and stored to disk. This is the same object you will receive in the deserialize() method, so it should capture all the information you need to restore the Keyring's state.
|
|
33
|
+
|
|
34
|
+
### deserialize( object )
|
|
35
|
+
|
|
36
|
+
As discussed above, the deserialize() method will be passed the JavaScript object that you returned when the serialize() method was called.
|
|
37
|
+
|
|
38
|
+
### addAccounts( n = 1 )
|
|
39
|
+
|
|
40
|
+
The addAccounts(n) method is used to inform your keyring that the user wishes to create a new account. You should perform whatever internal steps are needed so that a call to serialize() will persist the new account, and then return an array of the new account addresses.
|
|
41
|
+
|
|
42
|
+
The method may be called with or without an argument, specifying the number of accounts to create. You should generally default to 1 per call.
|
|
43
|
+
|
|
44
|
+
### getAccounts()
|
|
45
|
+
|
|
46
|
+
When this method is called, you must return an array of hex-string addresses for the accounts that your Keyring is able to sign for.
|
|
47
|
+
|
|
48
|
+
### signTransaction(address, transaction)
|
|
49
|
+
|
|
50
|
+
This method will receive a hex-prefixed, all-lowercase address string for the account you should sign the incoming transaction with.
|
|
51
|
+
|
|
52
|
+
For your convenience, the transaction is an instance of ethereumjs-tx, (https://github.com/ethereumjs/ethereumjs-tx) so signing can be as simple as:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
transaction.sign(privateKey)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
You must return a valid signed ethereumjs-tx (https://github.com/ethereumjs/ethereumjs-tx) object when complete, it can be the same transaction you received.
|
|
59
|
+
|
|
60
|
+
### signMessage(address, data)
|
|
61
|
+
|
|
62
|
+
The `eth_sign` method will receive the incoming data, already hashed, and must sign that hash, and then return the raw signed hash.
|
|
63
|
+
|
|
64
|
+
### getEncryptionPublicKey(address)
|
|
65
|
+
|
|
66
|
+
This provides the public key for encryption function.
|
|
67
|
+
|
|
68
|
+
### decryptMessage(address, data)
|
|
69
|
+
|
|
70
|
+
The `eth_decryptMessage` method will receive the incoming data in array format that returns `encrypt` function in `eth-sig-util` and must decrypt message, and then return the raw message.
|
|
71
|
+
|
|
72
|
+
### exportAccount(address)
|
|
73
|
+
|
|
74
|
+
Exports the specified account as a private key hex string.
|
|
75
|
+
|
|
76
|
+
### removeAccount(address)
|
|
77
|
+
|
|
78
|
+
removes the specified account from the list of accounts.
|
|
79
|
+
|
|
80
|
+
## Contributing
|
|
81
|
+
|
|
82
|
+
### Setup
|
|
83
|
+
|
|
84
|
+
- Install [Node.js](https://nodejs.org) version 16 or greater
|
|
85
|
+
- If you are using [nvm](https://github.com/creationix/nvm#installation) (recommended) running `nvm use` will automatically choose the right node version for you.
|
|
86
|
+
- Install [Yarn v3](https://yarnpkg.com/getting-started/install)
|
|
87
|
+
- Run `yarn install` to install dependencies and run any required post-install scripts
|
|
88
|
+
|
|
89
|
+
-- This package implicitly relies on Buffer to be present (meaning that if someone wants to use this in a browser context, they need to supply a polyfill for Buffer or use the `buffer` package)
|
|
90
|
+
|
|
91
|
+
### Testing and Linting
|
|
92
|
+
|
|
93
|
+
Run `yarn test` to run the tests once. To run tests on file changes, run `yarn test:watch`.
|
|
94
|
+
|
|
95
|
+
Run `yarn lint` to run the linter, or run `yarn lint:fix` to run the linter and fix any automatically fixable issues.
|
|
96
|
+
|
|
97
|
+
### Release & Publishing
|
|
98
|
+
|
|
99
|
+
The project follows the same release process as the other libraries in the MetaMask organization. The GitHub Actions [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) and [`action-publish-release`](https://github.com/MetaMask/action-publish-release) are used to automate the release process; see those repositories for more information about how they work.
|
|
100
|
+
|
|
101
|
+
1. Choose a release version.
|
|
102
|
+
|
|
103
|
+
- The release version should be chosen according to SemVer. Analyze the changes to see whether they include any breaking changes, new features, or deprecations, then choose the appropriate SemVer version. See [the SemVer specification](https://semver.org/) for more information.
|
|
104
|
+
|
|
105
|
+
2. If this release is backporting changes onto a previous release, then ensure there is a major version branch for that version (e.g. `1.x` for a `v1` backport release).
|
|
106
|
+
|
|
107
|
+
- The major version branch should be set to the most recent release with that major version. For example, when backporting a `v1.0.2` release, you'd want to ensure there was a `1.x` branch that was set to the `v1.0.1` tag.
|
|
108
|
+
|
|
109
|
+
3. Trigger the [`workflow_dispatch`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch) event [manually](https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow) for the `Create Release Pull Request` action to create the release PR.
|
|
110
|
+
|
|
111
|
+
- For a backport release, the base branch should be the major version branch that you ensured existed in step 2. For a normal release, the base branch should be the main branch for that repository (which should be the default value).
|
|
112
|
+
- This should trigger the [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) workflow to create the release PR.
|
|
113
|
+
|
|
114
|
+
4. Update the changelog to move each change entry into the appropriate change category ([See here](https://keepachangelog.com/en/1.0.0/#types) for the full list of change categories, and the correct ordering), and edit them to be more easily understood by users of the package.
|
|
115
|
+
|
|
116
|
+
- Generally any changes that don't affect consumers of the package (e.g. lockfile changes or development environment changes) are omitted. Exceptions may be made for changes that might be of interest despite not having an effect upon the published package (e.g. major test improvements, security improvements, improved documentation, etc.).
|
|
117
|
+
- Try to explain each change in terms that users of the package would understand (e.g. avoid referencing internal variables/concepts).
|
|
118
|
+
- Consolidate related changes into one change entry if it makes it easier to explain.
|
|
119
|
+
- Run `yarn auto-changelog validate --rc` to check that the changelog is correctly formatted.
|
|
120
|
+
|
|
121
|
+
5. Review and QA the release.
|
|
122
|
+
|
|
123
|
+
- If changes are made to the base branch, the release branch will need to be updated with these changes and review/QA will need to restart again. As such, it's probably best to avoid merging other PRs into the base branch while review is underway.
|
|
124
|
+
|
|
125
|
+
6. Squash & Merge the release.
|
|
126
|
+
|
|
127
|
+
- This should trigger the [`action-publish-release`](https://github.com/MetaMask/action-publish-release) workflow to tag the final release commit and publish the release on GitHub.
|
|
128
|
+
|
|
129
|
+
7. Publish the release on npm.
|
|
130
|
+
|
|
131
|
+
- Be very careful to use a clean local environment to publish the release, and follow exactly the same steps used during CI.
|
|
132
|
+
- Use `npm publish --dry-run` to examine the release contents to ensure the correct files are included. Compare to previous releases if necessary (e.g. using `https://unpkg.com/browse/[package name]@[package version]/`).
|
|
133
|
+
- Once you are confident the release contents are correct, publish the release using `npm publish`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './simple-keyring';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var simple_keyring_1 = require("./simple-keyring");
|
|
8
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(simple_keyring_1).default; } });
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,mDAA2C;AAAlC,0HAAA,OAAO,OAAA","sourcesContent":["export { default } from './simple-keyring';\n"]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { TypedTransaction } from '@ethereumjs/tx';
|
|
2
|
+
import { SignTypedDataVersion } from '@metamask/eth-sig-util';
|
|
3
|
+
import { Eip1024EncryptedData, Hex, Keyring } from '@metamask/utils';
|
|
4
|
+
declare type KeyringOpt = {
|
|
5
|
+
withAppKeyOrigin?: string;
|
|
6
|
+
version?: SignTypedDataVersion | string;
|
|
7
|
+
};
|
|
8
|
+
export default class SimpleKeyring implements Keyring<string[]> {
|
|
9
|
+
#private;
|
|
10
|
+
readonly type: string;
|
|
11
|
+
static type: string;
|
|
12
|
+
constructor(privateKeys?: string[]);
|
|
13
|
+
serialize(): Promise<string[]>;
|
|
14
|
+
deserialize(privateKeys?: string[]): Promise<void>;
|
|
15
|
+
addAccounts(numAccounts?: number): Promise<`0x${string}`[]>;
|
|
16
|
+
getAccounts(): Promise<`0x${string}`[]>;
|
|
17
|
+
signTransaction(address: Hex, transaction: TypedTransaction, opts?: KeyringOpt): Promise<TypedTransaction>;
|
|
18
|
+
signMessage(address: Hex, data: string, opts?: {
|
|
19
|
+
withAppKeyOrigin: string;
|
|
20
|
+
validateMessage: boolean;
|
|
21
|
+
}): Promise<string>;
|
|
22
|
+
signPersonalMessage(address: Hex, msgHex: Hex, opts?: {
|
|
23
|
+
withAppKeyOrigin: string;
|
|
24
|
+
}): Promise<string>;
|
|
25
|
+
decryptMessage(withAccount: Hex, encryptedData: Eip1024EncryptedData): Promise<string>;
|
|
26
|
+
signTypedData(address: Hex, typedData: any, opts?: KeyringOpt): Promise<string>;
|
|
27
|
+
getEncryptionPublicKey(withAccount: Hex, opts?: KeyringOpt): Promise<string>;
|
|
28
|
+
getAppKeyAddress(address: Hex, origin: string): Promise<`0x${string}`>;
|
|
29
|
+
exportAccount(address: Hex, opts?: {
|
|
30
|
+
withAppKeyOrigin: string;
|
|
31
|
+
}): Promise<string>;
|
|
32
|
+
removeAccount(address: string): void;
|
|
33
|
+
}
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
var _SimpleKeyring_instances, _SimpleKeyring_wallets, _SimpleKeyring_getPrivateKeyFor, _SimpleKeyring_getWalletForAccount;
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
const util_1 = require("@ethereumjs/util");
|
|
19
|
+
const eth_sig_util_1 = require("@metamask/eth-sig-util");
|
|
20
|
+
const utils_1 = require("@metamask/utils");
|
|
21
|
+
const keccak_1 = require("ethereum-cryptography/keccak");
|
|
22
|
+
const randombytes_1 = __importDefault(require("randombytes"));
|
|
23
|
+
const TYPE = 'Simple Key Pair';
|
|
24
|
+
class SimpleKeyring {
|
|
25
|
+
constructor(privateKeys = []) {
|
|
26
|
+
_SimpleKeyring_instances.add(this);
|
|
27
|
+
_SimpleKeyring_wallets.set(this, void 0);
|
|
28
|
+
this.type = TYPE;
|
|
29
|
+
__classPrivateFieldSet(this, _SimpleKeyring_wallets, [], "f");
|
|
30
|
+
/* istanbul ignore next: It's not possible to write a unit test for this, because a constructor isn't allowed
|
|
31
|
+
* to be async. Jest can't await the constructor, and when the error gets thrown, Jest can't catch it. */
|
|
32
|
+
this.deserialize(privateKeys).catch((error) => {
|
|
33
|
+
throw new Error(`Problem deserializing SimpleKeyring ${error.message}`);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
async serialize() {
|
|
37
|
+
return __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").map((a) => a.privateKey.toString('hex'));
|
|
38
|
+
}
|
|
39
|
+
async deserialize(privateKeys = []) {
|
|
40
|
+
__classPrivateFieldSet(this, _SimpleKeyring_wallets, privateKeys.map((hexPrivateKey) => {
|
|
41
|
+
const strippedHexPrivateKey = (0, util_1.stripHexPrefix)(hexPrivateKey);
|
|
42
|
+
const privateKey = Buffer.from(strippedHexPrivateKey, 'hex');
|
|
43
|
+
const publicKey = (0, util_1.privateToPublic)(privateKey);
|
|
44
|
+
return { privateKey, publicKey };
|
|
45
|
+
}), "f");
|
|
46
|
+
}
|
|
47
|
+
async addAccounts(numAccounts = 1) {
|
|
48
|
+
const newWallets = [];
|
|
49
|
+
for (let i = 0; i < numAccounts; i++) {
|
|
50
|
+
const privateKey = generateKey();
|
|
51
|
+
const publicKey = (0, util_1.privateToPublic)(privateKey);
|
|
52
|
+
newWallets.push({ privateKey, publicKey });
|
|
53
|
+
}
|
|
54
|
+
__classPrivateFieldSet(this, _SimpleKeyring_wallets, __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").concat(newWallets), "f");
|
|
55
|
+
const hexWallets = newWallets.map(({ publicKey }) => (0, utils_1.add0x)((0, util_1.bufferToHex)((0, util_1.publicToAddress)(publicKey))));
|
|
56
|
+
return hexWallets;
|
|
57
|
+
}
|
|
58
|
+
async getAccounts() {
|
|
59
|
+
return __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").map(({ publicKey }) => (0, utils_1.add0x)((0, util_1.bufferToHex)((0, util_1.publicToAddress)(publicKey))));
|
|
60
|
+
}
|
|
61
|
+
async signTransaction(address, transaction, opts = {}) {
|
|
62
|
+
const privKey = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getPrivateKeyFor).call(this, address, opts);
|
|
63
|
+
const signedTx = transaction.sign(privKey);
|
|
64
|
+
// Newer versions of Ethereumjs-tx are immutable and return a new tx object
|
|
65
|
+
return signedTx === undefined ? transaction : signedTx;
|
|
66
|
+
}
|
|
67
|
+
// For eth_sign, we need to sign arbitrary data:
|
|
68
|
+
async signMessage(address, data, opts = { withAppKeyOrigin: '', validateMessage: true }) {
|
|
69
|
+
const message = (0, util_1.stripHexPrefix)(data);
|
|
70
|
+
if (opts.validateMessage &&
|
|
71
|
+
(message.length === 0 || !message.match(/^[a-fA-F0-9]*$/u))) {
|
|
72
|
+
throw new Error('Cannot sign invalid message');
|
|
73
|
+
}
|
|
74
|
+
const privKey = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getPrivateKeyFor).call(this, address, opts);
|
|
75
|
+
const msgSig = (0, util_1.ecsign)(Buffer.from(message, 'hex'), privKey);
|
|
76
|
+
const rawMsgSig = (0, eth_sig_util_1.concatSig)((0, util_1.toBuffer)(msgSig.v), msgSig.r, msgSig.s);
|
|
77
|
+
return rawMsgSig;
|
|
78
|
+
}
|
|
79
|
+
// For personal_sign, we need to prefix the message:
|
|
80
|
+
async signPersonalMessage(address, msgHex, opts = { withAppKeyOrigin: '' }) {
|
|
81
|
+
const privKey = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getPrivateKeyFor).call(this, address, opts);
|
|
82
|
+
return (0, eth_sig_util_1.personalSign)({ privateKey: privKey, data: msgHex });
|
|
83
|
+
}
|
|
84
|
+
// For eth_decryptMessage:
|
|
85
|
+
async decryptMessage(withAccount, encryptedData) {
|
|
86
|
+
const wallet = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getWalletForAccount).call(this, withAccount);
|
|
87
|
+
const privateKey = wallet.privateKey.toString('hex');
|
|
88
|
+
return (0, eth_sig_util_1.decrypt)({ privateKey, encryptedData });
|
|
89
|
+
}
|
|
90
|
+
// personal_signTypedData, signs data along with the schema
|
|
91
|
+
async signTypedData(address, typedData, opts = { version: eth_sig_util_1.SignTypedDataVersion.V1 }) {
|
|
92
|
+
// Treat invalid versions as "V1"
|
|
93
|
+
let version = eth_sig_util_1.SignTypedDataVersion.V1;
|
|
94
|
+
if (opts.version && isSignTypedDataVersion(opts.version)) {
|
|
95
|
+
version = eth_sig_util_1.SignTypedDataVersion[opts.version];
|
|
96
|
+
}
|
|
97
|
+
const privateKey = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getPrivateKeyFor).call(this, address, opts);
|
|
98
|
+
return (0, eth_sig_util_1.signTypedData)({ privateKey, data: typedData, version });
|
|
99
|
+
}
|
|
100
|
+
// get public key for nacl
|
|
101
|
+
async getEncryptionPublicKey(withAccount, opts) {
|
|
102
|
+
const privKey = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getPrivateKeyFor).call(this, withAccount, opts);
|
|
103
|
+
const publicKey = (0, eth_sig_util_1.getEncryptionPublicKey)(privKey.toString('hex'));
|
|
104
|
+
return publicKey;
|
|
105
|
+
}
|
|
106
|
+
// returns an address specific to an app
|
|
107
|
+
async getAppKeyAddress(address, origin) {
|
|
108
|
+
if (!origin || typeof origin !== 'string') {
|
|
109
|
+
throw new Error(`'origin' must be a non-empty string`);
|
|
110
|
+
}
|
|
111
|
+
const wallet = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getWalletForAccount).call(this, address, {
|
|
112
|
+
withAppKeyOrigin: origin,
|
|
113
|
+
});
|
|
114
|
+
const appKeyAddress = (0, utils_1.add0x)((0, util_1.bufferToHex)((0, util_1.publicToAddress)(wallet.publicKey)));
|
|
115
|
+
return appKeyAddress;
|
|
116
|
+
}
|
|
117
|
+
// exportAccount should return a hex-encoded private key:
|
|
118
|
+
async exportAccount(address, opts = { withAppKeyOrigin: '' }) {
|
|
119
|
+
const wallet = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getWalletForAccount).call(this, address, opts);
|
|
120
|
+
return wallet.privateKey.toString('hex');
|
|
121
|
+
}
|
|
122
|
+
removeAccount(address) {
|
|
123
|
+
if (!__classPrivateFieldGet(this, _SimpleKeyring_wallets, "f")
|
|
124
|
+
.map(({ publicKey }) => (0, util_1.bufferToHex)((0, util_1.publicToAddress)(publicKey)).toLowerCase())
|
|
125
|
+
.includes(address.toLowerCase())) {
|
|
126
|
+
throw new Error(`Address ${address} not found in this keyring`);
|
|
127
|
+
}
|
|
128
|
+
__classPrivateFieldSet(this, _SimpleKeyring_wallets, __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").filter(({ publicKey }) => (0, util_1.bufferToHex)((0, util_1.publicToAddress)(publicKey)).toLowerCase() !==
|
|
129
|
+
address.toLowerCase()), "f");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.default = SimpleKeyring;
|
|
133
|
+
_SimpleKeyring_wallets = new WeakMap(), _SimpleKeyring_instances = new WeakSet(), _SimpleKeyring_getPrivateKeyFor = function _SimpleKeyring_getPrivateKeyFor(address, opts = { withAppKeyOrigin: '' }) {
|
|
134
|
+
if (!address) {
|
|
135
|
+
throw new Error('Must specify address.');
|
|
136
|
+
}
|
|
137
|
+
const wallet = __classPrivateFieldGet(this, _SimpleKeyring_instances, "m", _SimpleKeyring_getWalletForAccount).call(this, address, opts);
|
|
138
|
+
return wallet.privateKey;
|
|
139
|
+
}, _SimpleKeyring_getWalletForAccount = function _SimpleKeyring_getWalletForAccount(account, opts = {}) {
|
|
140
|
+
const address = (0, eth_sig_util_1.normalize)(account);
|
|
141
|
+
let wallet = __classPrivateFieldGet(this, _SimpleKeyring_wallets, "f").find(({ publicKey }) => (0, util_1.bufferToHex)((0, util_1.publicToAddress)(publicKey)) === address);
|
|
142
|
+
if (!wallet) {
|
|
143
|
+
throw new Error('Simple Keyring - Unable to find matching address.');
|
|
144
|
+
}
|
|
145
|
+
if (opts.withAppKeyOrigin) {
|
|
146
|
+
const { privateKey } = wallet;
|
|
147
|
+
const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');
|
|
148
|
+
const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);
|
|
149
|
+
const appKeyPrivateKey = (0, util_1.arrToBufArr)((0, keccak_1.keccak256)(appKeyBuffer));
|
|
150
|
+
const appKeyPublicKey = (0, util_1.privateToPublic)(appKeyPrivateKey);
|
|
151
|
+
wallet = { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };
|
|
152
|
+
}
|
|
153
|
+
return wallet;
|
|
154
|
+
};
|
|
155
|
+
SimpleKeyring.type = TYPE;
|
|
156
|
+
/**
|
|
157
|
+
* Generate and validate a new random key of 32 bytes.
|
|
158
|
+
*
|
|
159
|
+
* @returns Buffer The generated key.
|
|
160
|
+
*/
|
|
161
|
+
function generateKey() {
|
|
162
|
+
const privateKey = (0, randombytes_1.default)(32);
|
|
163
|
+
if (!(0, util_1.isValidPrivate)(privateKey)) {
|
|
164
|
+
throw new Error('Private key does not satisfy the curve requirements (ie. it is invalid)');
|
|
165
|
+
}
|
|
166
|
+
return privateKey;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Type predicate type guard to check if a string is in the enum SignTypedDataVersion.
|
|
170
|
+
*
|
|
171
|
+
* @param version - The string to check.
|
|
172
|
+
* @returns Whether it's in the enum.
|
|
173
|
+
*/
|
|
174
|
+
// TODO: Put this in @metamask/eth-sig-util
|
|
175
|
+
function isSignTypedDataVersion(version) {
|
|
176
|
+
return version in eth_sig_util_1.SignTypedDataVersion;
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=simple-keyring.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simple-keyring.js","sourceRoot":"","sources":["../src/simple-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,2CAS0B;AAC1B,yDAQgC;AAChC,2CAA4E;AAC5E,yDAAyD;AACzD,8DAAsC;AAOtC,MAAM,IAAI,GAAG,iBAAiB,CAAC;AAE/B,MAAqB,aAAa;IAOhC,YAAY,cAAwB,EAAE;;QANtC,yCAAs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Address,\n stripHexPrefix,\n toBuffer,\n} from '@ethereumjs/util';\nimport {\n concatSig,\n decrypt,\n getEncryptionPublicKey,\n normalize,\n personalSign,\n signTypedData,\n SignTypedDataVersion,\n} from '@metamask/eth-sig-util';\nimport { add0x, Eip1024EncryptedData, Hex, Keyring } from '@metamask/utils';\nimport { keccak256 } from 'ethereum-cryptography/keccak';\nimport randombytes from 'randombytes';\n\ntype KeyringOpt = {\n withAppKeyOrigin?: string;\n version?: SignTypedDataVersion | string;\n};\n\nconst TYPE = 'Simple Key Pair';\n\nexport default class SimpleKeyring implements Keyring<string[]> {\n #wallets: { privateKey: Buffer; publicKey: Buffer }[];\n\n readonly type: string = TYPE;\n\n static type: string = TYPE;\n\n constructor(privateKeys: string[] = []) {\n this.#wallets = [];\n\n /* istanbul ignore next: It's not possible to write a unit test for this, because a constructor isn't allowed\n * to be async. Jest can't await the constructor, and when the error gets thrown, Jest can't catch it. */\n this.deserialize(privateKeys).catch((error: Error) => {\n throw new Error(`Problem deserializing SimpleKeyring ${error.message}`);\n });\n }\n\n async serialize() {\n return this.#wallets.map((a) => a.privateKey.toString('hex'));\n }\n\n async deserialize(privateKeys: string[] = []) {\n this.#wallets = privateKeys.map((hexPrivateKey) => {\n const strippedHexPrivateKey = stripHexPrefix(hexPrivateKey);\n const privateKey = Buffer.from(strippedHexPrivateKey, 'hex');\n const publicKey = privateToPublic(privateKey);\n return { privateKey, publicKey };\n });\n }\n\n async addAccounts(numAccounts = 1) {\n const newWallets = [];\n for (let i = 0; i < numAccounts; i++) {\n const privateKey = generateKey();\n const publicKey = privateToPublic(privateKey);\n newWallets.push({ privateKey, publicKey });\n }\n this.#wallets = this.#wallets.concat(newWallets);\n const hexWallets = newWallets.map(({ publicKey }) =>\n add0x(bufferToHex(publicToAddress(publicKey))),\n );\n return hexWallets;\n }\n\n async getAccounts() {\n return this.#wallets.map(({ publicKey }) =>\n add0x(bufferToHex(publicToAddress(publicKey))),\n );\n }\n\n async signTransaction(\n address: Hex,\n transaction: TypedTransaction,\n opts: KeyringOpt = {},\n ): Promise<TypedTransaction> {\n const privKey = this.#getPrivateKeyFor(address, opts);\n const signedTx = transaction.sign(privKey);\n // Newer versions of Ethereumjs-tx are immutable and return a new tx object\n return signedTx === undefined ? transaction : signedTx;\n }\n\n // For eth_sign, we need to sign arbitrary data:\n async signMessage(\n address: Hex,\n data: string,\n opts = { withAppKeyOrigin: '', validateMessage: true },\n ) {\n const message = stripHexPrefix(data);\n if (\n opts.validateMessage &&\n (message.length === 0 || !message.match(/^[a-fA-F0-9]*$/u))\n ) {\n throw new Error('Cannot sign invalid message');\n }\n const privKey = this.#getPrivateKeyFor(address, opts);\n const msgSig = ecsign(Buffer.from(message, 'hex'), privKey);\n const rawMsgSig = concatSig(toBuffer(msgSig.v), msgSig.r, msgSig.s);\n return rawMsgSig;\n }\n\n // For personal_sign, we need to prefix the message:\n async signPersonalMessage(\n address: Hex,\n msgHex: Hex,\n opts = { withAppKeyOrigin: '' },\n ) {\n const privKey = this.#getPrivateKeyFor(address, opts);\n return personalSign({ privateKey: privKey, data: msgHex });\n }\n\n // For eth_decryptMessage:\n async decryptMessage(withAccount: Hex, encryptedData: Eip1024EncryptedData) {\n const wallet = this.#getWalletForAccount(withAccount);\n const privateKey = wallet.privateKey.toString('hex');\n return decrypt({ privateKey, encryptedData });\n }\n\n // personal_signTypedData, signs data along with the schema\n async signTypedData(\n address: Hex,\n typedData: any,\n opts: KeyringOpt = { version: SignTypedDataVersion.V1 },\n ) {\n // Treat invalid versions as \"V1\"\n let version = SignTypedDataVersion.V1;\n\n if (opts.version && isSignTypedDataVersion(opts.version)) {\n version = SignTypedDataVersion[opts.version];\n }\n\n const privateKey = this.#getPrivateKeyFor(address, opts);\n return signTypedData({ privateKey, data: typedData, version });\n }\n\n // get public key for nacl\n async getEncryptionPublicKey(withAccount: Hex, opts?: KeyringOpt) {\n const privKey = this.#getPrivateKeyFor(withAccount, opts);\n const publicKey = getEncryptionPublicKey(privKey.toString('hex'));\n return publicKey;\n }\n\n #getPrivateKeyFor(address: Hex, opts: KeyringOpt = { withAppKeyOrigin: '' }) {\n if (!address) {\n throw new Error('Must specify address.');\n }\n const wallet = this.#getWalletForAccount(address, opts);\n return wallet.privateKey;\n }\n\n // returns an address specific to an app\n async getAppKeyAddress(address: Hex, origin: string) {\n if (!origin || typeof origin !== 'string') {\n throw new Error(`'origin' must be a non-empty string`);\n }\n const wallet = this.#getWalletForAccount(address, {\n withAppKeyOrigin: origin,\n });\n const appKeyAddress = add0x(bufferToHex(publicToAddress(wallet.publicKey)));\n return appKeyAddress;\n }\n\n // exportAccount should return a hex-encoded private key:\n async exportAccount(address: Hex, opts = { withAppKeyOrigin: '' }) {\n const wallet = this.#getWalletForAccount(address, opts);\n return wallet.privateKey.toString('hex');\n }\n\n removeAccount(address: string) {\n if (\n !this.#wallets\n .map(({ publicKey }) =>\n bufferToHex(publicToAddress(publicKey)).toLowerCase(),\n )\n .includes(address.toLowerCase())\n ) {\n throw new Error(`Address ${address} not found in this keyring`);\n }\n\n this.#wallets = this.#wallets.filter(\n ({ publicKey }) =>\n bufferToHex(publicToAddress(publicKey)).toLowerCase() !==\n address.toLowerCase(),\n );\n }\n\n #getWalletForAccount(account: string | number, opts: KeyringOpt = {}) {\n const address = normalize(account);\n let wallet = this.#wallets.find(\n ({ publicKey }) => bufferToHex(publicToAddress(publicKey)) === address,\n );\n if (!wallet) {\n throw new Error('Simple Keyring - Unable to find matching address.');\n }\n\n if (opts.withAppKeyOrigin) {\n const { privateKey } = wallet;\n const appKeyOriginBuffer = Buffer.from(opts.withAppKeyOrigin, 'utf8');\n const appKeyBuffer = Buffer.concat([privateKey, appKeyOriginBuffer]);\n const appKeyPrivateKey = arrToBufArr(keccak256(appKeyBuffer));\n const appKeyPublicKey = privateToPublic(appKeyPrivateKey);\n wallet = { privateKey: appKeyPrivateKey, publicKey: appKeyPublicKey };\n }\n\n return wallet;\n }\n}\n\n/**\n * Generate and validate a new random key of 32 bytes.\n *\n * @returns Buffer The generated key.\n */\nfunction generateKey(): Buffer {\n const privateKey = randombytes(32);\n\n if (!isValidPrivate(privateKey)) {\n throw new Error(\n 'Private key does not satisfy the curve requirements (ie. it is invalid)',\n );\n }\n return privateKey;\n}\n\n/**\n * Type predicate type guard to check if a string is in the enum SignTypedDataVersion.\n *\n * @param version - The string to check.\n * @returns Whether it's in the enum.\n */\n// TODO: Put this in @metamask/eth-sig-util\nfunction isSignTypedDataVersion(\n version: SignTypedDataVersion | string,\n): version is SignTypedDataVersion {\n return version in SignTypedDataVersion;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metamask-previews/eth-simple-keyring",
|
|
3
|
+
"version": "6.0.2-38c4bd5",
|
|
4
|
+
"description": "A simple standard interface for a series of Ethereum private keys.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ethereum",
|
|
7
|
+
"keyring"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/MetaMask/eth-simple-keyring#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/MetaMask/eth-simple-keyring/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/MetaMask/eth-simple-keyring.git"
|
|
16
|
+
},
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist/"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc --build tsconfig.build.json",
|
|
24
|
+
"build:clean": "rimraf dist && yarn build",
|
|
25
|
+
"build:docs": "typedoc",
|
|
26
|
+
"build:force": "tsc --build tsconfig.build.json --force",
|
|
27
|
+
"changelog:update": "../../scripts/update-changelog.sh @metamask/eth-simple-keyring",
|
|
28
|
+
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/eth-simple-keyring",
|
|
29
|
+
"publish:preview": "yarn npm publish --tag preview",
|
|
30
|
+
"sample": "ts-node src/sample.ts",
|
|
31
|
+
"test": "jest",
|
|
32
|
+
"test:clean": "jest --clearCache",
|
|
33
|
+
"test:verbose": "jest --verbose",
|
|
34
|
+
"test:watch": "jest --watch"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@ethereumjs/util": "^8.1.0",
|
|
38
|
+
"@metamask/eth-sig-util": "^7.0.0",
|
|
39
|
+
"@metamask/utils": "^9.0.0",
|
|
40
|
+
"deepmerge": "^4.2.2",
|
|
41
|
+
"ethereum-cryptography": "^2.1.2",
|
|
42
|
+
"randombytes": "^2.1.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@ethereumjs/tx": "^4.1.1",
|
|
46
|
+
"@lavamoat/allow-scripts": "^3.0.4",
|
|
47
|
+
"@metamask/auto-changelog": "^3.1.0",
|
|
48
|
+
"@types/ethereumjs-tx": "^1.0.1",
|
|
49
|
+
"@types/jest": "^29.5.0",
|
|
50
|
+
"@types/node": "^18.15.10",
|
|
51
|
+
"@types/randombytes": "^2.0.0",
|
|
52
|
+
"depcheck": "^1.4.3",
|
|
53
|
+
"ethereumjs-tx": "^1.3.7",
|
|
54
|
+
"jest": "^29.5.0",
|
|
55
|
+
"ts-jest": "^29.0.5",
|
|
56
|
+
"ts-node": "^10.9.1",
|
|
57
|
+
"typedoc": "^0.25.1",
|
|
58
|
+
"typescript": "~4.8.4"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": "^18.18 || >=20"
|
|
62
|
+
},
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public",
|
|
65
|
+
"registry": "https://registry.npmjs.org/"
|
|
66
|
+
},
|
|
67
|
+
"lavamoat": {
|
|
68
|
+
"allowScripts": {
|
|
69
|
+
"keccak": true,
|
|
70
|
+
"secp256k1": true,
|
|
71
|
+
"@lavamoat/preinstall-always-fail": false,
|
|
72
|
+
"ethereumjs-tx>ethereumjs-util>ethereum-cryptography>keccak": false,
|
|
73
|
+
"ethereumjs-tx>ethereumjs-util>ethereum-cryptography>secp256k1": false
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|