generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7525e99
commit ccd708e
Showing
13 changed files
with
485 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Changelog | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
ISC License | ||
|
||
Copyright (c) 2020 MetaMask | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# QR Keyring |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* For a detailed explanation regarding each configuration property and type check, visit: | ||
* https://jestjs.io/docs/configuration | ||
*/ | ||
|
||
const merge = require('deepmerge'); | ||
const path = require('path'); | ||
|
||
const baseConfig = require('../../jest.config.packages'); | ||
|
||
const displayName = path.basename(__dirname); | ||
|
||
module.exports = merge(baseConfig, { | ||
// The display name when running multiple projects | ||
displayName, | ||
|
||
// An array of regexp pattern strings used to skip coverage collection | ||
coveragePathIgnorePatterns: ['./src/tests'], | ||
|
||
// An object that configures minimum threshold enforcement for coverage results | ||
coverageThreshold: { | ||
global: { | ||
branches: 100, | ||
functions: 100, | ||
lines: 100, | ||
statements: 100, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{ | ||
"name": "@metamask/eth-qr-keyring", | ||
"version": "1.0.0", | ||
"description": "A simple standard interface for a series of Ethereum private keys.", | ||
"keywords": [ | ||
"ethereum", | ||
"keyring", | ||
"qr", | ||
"metamask" | ||
], | ||
"homepage": "https://github.com/MetaMask/accounts/packages/keyring-eth-qr#readme", | ||
"bugs": { | ||
"url": "https://github.com/MetaMask/accounts/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/MetaMask/accounts.git" | ||
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist/" | ||
], | ||
"scripts": { | ||
"build": "tsc --build tsconfig.build.json", | ||
"build:clean": "rimraf dist && yarn build", | ||
"build:docs": "typedoc", | ||
"build:force": "tsc --build tsconfig.build.json --force", | ||
"changelog:update": "../../scripts/update-changelog.sh @metamask/eth-simple-keyring", | ||
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/eth-simple-keyring", | ||
"publish:preview": "yarn npm publish --tag preview", | ||
"sample": "ts-node src/sample.ts", | ||
"test": "jest", | ||
"test:clean": "jest --clearCache", | ||
"test:verbose": "jest --verbose", | ||
"test:watch": "jest --watch" | ||
}, | ||
"dependencies": { | ||
"@ethereumjs/util": "^9.1.0", | ||
"@keystonehq/bc-ur-registry-eth": "^0.21.0", | ||
"@keystonehq/ur-decoder": "^0.12.2", | ||
"@metamask/utils": "^9.2.1", | ||
"@ngraveio/bc-ur": "^1.1.13", | ||
"hdkey": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"@lavamoat/allow-scripts": "^3.2.1", | ||
"@metamask/auto-changelog": "^3.4.4", | ||
"@types/hdkey": "^2.0.1", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^20.12.12", | ||
"deepmerge": "^4.2.2", | ||
"depcheck": "^1.4.7", | ||
"jest": "^29.5.0", | ||
"ts-jest": "^29.0.5", | ||
"ts-node": "^10.9.2", | ||
"typedoc": "^0.25.13", | ||
"typescript": "~4.8.4" | ||
}, | ||
"engines": { | ||
"node": "^18.18 || >=20" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"lavamoat": { | ||
"allowScripts": { | ||
"keccak": true, | ||
"secp256k1": true, | ||
"@lavamoat/preinstall-always-fail": false, | ||
"ethereumjs-tx>ethereumjs-util>ethereum-cryptography>keccak": false, | ||
"ethereumjs-tx>ethereumjs-util>ethereum-cryptography>secp256k1": false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { pubToAddress } from '@ethereumjs/util'; | ||
import { | ||
CryptoAccount, | ||
type CryptoHDKey, | ||
type CryptoOutput, | ||
} from '@keystonehq/bc-ur-registry-eth'; | ||
import { add0x, getChecksumAddress, type Hex } from '@metamask/utils'; | ||
import HDKey from 'hdkey'; | ||
|
||
export type RootAccount = { fingerprint: Hex } & ( | ||
| { | ||
type: 'hd'; | ||
hdPath: string; | ||
childrenPath: string; | ||
bip32xPub: string; | ||
} | ||
| { | ||
type: 'account'; | ||
addressPaths: Record<Hex, string>; | ||
} | ||
); | ||
|
||
export class AccountDeriver { | ||
#root?: RootAccount; | ||
|
||
init(source: CryptoAccount | CryptoHDKey) { | ||
const fingerprint = this.#getFingerprintFromSource(source); | ||
if (source instanceof CryptoAccount) { | ||
this.#root = { | ||
fingerprint, | ||
type: 'account', | ||
addressPaths: this.#getPathsFromCryptoOutputDescriptors( | ||
source.getOutputDescriptors(), | ||
), | ||
}; | ||
} else { | ||
this.#root = { | ||
fingerprint, | ||
type: 'hd', | ||
hdPath: `m/${source.getOrigin().getPath()}`, | ||
childrenPath: source.getChildren().getPath(), | ||
bip32xPub: source.getBip32Key(), | ||
}; | ||
} | ||
} | ||
|
||
deriveIndex(index: number): Hex { | ||
if (!this.#root) { | ||
throw new Error('AccountDeriver not initialized'); | ||
} | ||
|
||
if (this.#root.type === 'account') { | ||
const address = Object.keys(this.#root.addressPaths)[index]; | ||
if (!address) { | ||
throw new Error(`Address not found for index ${index}`); | ||
} | ||
return add0x(address); | ||
} else { | ||
const hdKey = HDKey.fromExtendedKey(this.#root.bip32xPub); | ||
hdKey.derive(`m/${index}`); | ||
const address = getChecksumAddress( | ||
add0x(Buffer.from(pubToAddress(hdKey.publicKey, true)).toString('hex')), | ||
); | ||
return add0x(address); | ||
} | ||
} | ||
|
||
#getFingerprintFromSource(source: CryptoAccount | CryptoHDKey): Hex { | ||
return source instanceof CryptoAccount | ||
? add0x(source.getMasterFingerprint().toString('hex')) | ||
: add0x(source.getOrigin().getSourceFingerprint().toString('hex')); | ||
} | ||
|
||
#getPathsFromCryptoOutputDescriptors( | ||
descriptors: CryptoOutput[], | ||
): Record<Hex, string> { | ||
return descriptors.reduce((paths: Record<Hex, string>, current) => { | ||
const hdKey = current.getHDKey(); | ||
if (hdKey) { | ||
const path = `M/${hdKey.getOrigin().getPath()}`; | ||
const address = getChecksumAddress( | ||
add0x( | ||
Buffer.from(pubToAddress(hdKey.getKey(), true)).toString('hex'), | ||
), | ||
); | ||
paths[address] = path; | ||
} | ||
return paths; | ||
}, {}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './qr-keyring'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { CryptoAccount, CryptoHDKey } from '@keystonehq/bc-ur-registry-eth'; | ||
import { URRegistryDecoder } from '@keystonehq/ur-decoder'; | ||
import type { Hex, Keyring } from '@metamask/utils'; | ||
import { UR } from '@ngraveio/bc-ur'; | ||
import { AccountDeriver } from './account-deriver'; | ||
|
||
export const QR_KEYRING_TYPE = 'QrKeyring' as const; | ||
|
||
export const SUPPORTED_UR_TYPE = { | ||
CRYPTO_HDKEY: 'crypto-hdkey', | ||
CRYPTO_ACCOUNT: 'crypto-account', | ||
ETH_SIGNATURE: 'eth-signature', | ||
}; | ||
|
||
/** | ||
* The state of the QrKeyring | ||
* | ||
* @property accounts - The accounts in the QrKeyring | ||
*/ | ||
export type QrKeyringState = { | ||
accounts: Record<number, Hex>; | ||
cbor?: Hex; | ||
}; | ||
|
||
export class QrKeyring implements Keyring<QrKeyringState> { | ||
type = QR_KEYRING_TYPE; | ||
|
||
#accounts: Record<number, Hex> = {}; | ||
|
||
#deriver: AccountDeriver = new AccountDeriver(); | ||
|
||
async serialize(): Promise<QrKeyringState> { | ||
return { | ||
accounts: Object.values(this.#accounts), | ||
cbor: '', // TODO: Serialize deriver | ||
}; | ||
} | ||
|
||
async deserialize(state: QrKeyringState): Promise<void> { | ||
this.#accounts = state.accounts; | ||
if (state.cbor) { | ||
this.submitCBOR(state.cbor); | ||
} | ||
} | ||
|
||
async addAccounts(_accountsToAdd: number): Promise<Hex[]> { | ||
// TODO: Implement | ||
} | ||
|
||
async getAccounts(): Promise<Hex[]> { | ||
return Object.values(this.#accounts); | ||
} | ||
|
||
submitCBOR(cbor: Hex) { | ||
const ur = URRegistryDecoder.decode(cbor); | ||
const bufferedCbor = Buffer.from(cbor, 'hex'); | ||
let derivationSource: CryptoHDKey | CryptoAccount; | ||
|
||
switch (ur.type) { | ||
case SUPPORTED_UR_TYPE.CRYPTO_HDKEY: | ||
derivationSource = CryptoHDKey.fromCBOR(bufferedCbor); | ||
break; | ||
case SUPPORTED_UR_TYPE.CRYPTO_ACCOUNT: | ||
derivationSource = CryptoAccount.fromCBOR(bufferedCbor); | ||
break; | ||
default: | ||
throw new Error('Unsupported UR type'); | ||
} | ||
|
||
this.#deriver.init(derivationSource); | ||
} | ||
|
||
async setAccountToUnlock(index: number): Promise<void> { | ||
// TODO: Implement | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "../../tsconfig.packages.build.json", | ||
"compilerOptions": { | ||
"baseUrl": "./", | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
"target": "es2020" | ||
}, | ||
"include": ["./src/**/*.ts"], | ||
"exclude": ["./src/**/*.test.ts", "./src/sample.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.packages.json", | ||
"compilerOptions": { | ||
"baseUrl": "./" | ||
}, | ||
"include": ["./src"], | ||
"exclude": ["./dist/**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"entryPoints": ["./src/index.ts"], | ||
"excludePrivate": true, | ||
"hideGenerator": true, | ||
"out": "docs" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.