"url": "git+https://zoso.dev/libnemo.git"
},
"scripts": {
- "build": "rm -rf dist && tsc && esbuild main.min=dist/main.js global.min=dist/global.js --outdir=dist --target=es2022 --format=esm --platform=browser --bundle --minify --sourcemap",
+ "build": "rm -rf dist && tsc && esbuild main.min=dist/main.js global.min=dist/global.js ckd=dist/lib/ckd.js --outdir=dist --target=es2022 --format=esm --platform=browser --bundle --minify --sourcemap",
"test": "npm run build && node --test --env-file .env",
"test:coverage": "npm run test -- --experimental-test-coverage",
"test:coverage:report": "npm run test:coverage -- --test-reporter=lcov --test-reporter-destination=coverage.info && genhtml coverage.info --output-directory test/coverage && rm coverage.info && xdg-open test/coverage/index.html"
* @returns {Promise<Account>}
*/
globalThis.onmessage = (event) => {
- let result = null
const { type, seed, index } = event.data
switch (type) {
case 'bip44': {
- result = ckdBip44(seed, index).then(postMessage)
+ ckdBip44(seed, index).then(postMessage)
break
}
case 'blake2b': {
- result = ckdBlake2b(seed, index).then(postMessage)
- break
- }
- case 'ledger': {
- result = ckdLedger(seed, index).then(postMessage)
+ ckdBlake2b(seed, index).then(postMessage)
break
}
}
* @param {number} index - Index of the account
* @returns {Promise<Account>}
*/
-async function ckdBip44 (seed: string, index: number): Promise<Account> {
- const key = await nanoCKD(seed, index)
- return await Account.fromPrivateKey(key, index)
+async function ckdBip44 (seed: string, index: number): Promise<string> {
+ return nanoCKD(seed, index)
}
/**
* @param {number} index - Index of the account
* @returns {Promise<Account>}
*/
-async function ckdBlake2b (seed: string, index: number): Promise<Account> {
+async function ckdBlake2b (seed: string, index: number): Promise<string> {
const hash = await Tools.blake2b([seed, dec.toHex(index, 8)])
- const key = bytes.toHex(hash)
- return await Account.fromPrivateKey(key, index)
-}
-
-/**
-* Gets the public key for an account from the Ledger device.
-*
-* @param {number} index - Index of the account
-* @returns {Promise<Account>}
-*/
-export async function ckdLedger (ledger: Ledger, index: number): Promise<Account> {
- const { status, publicKey } = await ledger.account(index)
- if (status === 'OK' && publicKey != null) {
- return await Account.fromPublicKey(publicKey, index)
- }
- throw new Error(`Error getting Ledger account: ${status}`)
+ return bytes.toHex(hash)
}
#assign (thread: Thread, task: Task) {
thread.isAvailable = false
thread.worker.onmessage = (event) => {
- console.log('work done')
if (this.#tasks.length > 0) {
const next = this.#tasks.shift()
this.#assign(thread, next)
}
task.resolve(event.data)
}
- console.log('posting')
thread.worker.postMessage(task.data)
}
* @returns {Promise<Account>}\r
*/\r
async ckd (index: number): Promise<Account> {\r
- // const key = await nanoCKD(this.seed, index)\r
- // return await Account.fromPrivateKey(key, index)\r
- return await ckdPool.work({ type: 'bip44', seed: this.seed, index })\r
+ const key = await ckdPool.work({ type: 'bip44', seed: this.seed, index })\r
+ if (typeof key !== 'string') {\r
+ throw new TypeError('BIP-44 child key derivation returned invalid data')\r
+ }\r
+ return await Account.fromPrivateKey(key, index)\r
}\r
}\r
\r
* @returns {Promise<Account>}\r
*/\r
async ckd (index: number): Promise<Account> {\r
- const hash = await Tools.blake2b([this.seed, dec.toHex(index, 8)])\r
- const key = bytes.toHex(hash)\r
+ const key = await ckdPool.work({ type: 'blake2b', seed: this.seed, index })\r
+ if (typeof key !== 'string') {\r
+ throw new TypeError('BLAKE2b child key derivation returned invalid data')\r
+ }\r
return await Account.fromPrivateKey(key, index)\r
- // return await ckdPool.work({ type: 'blake2b', seed: this.seed, index })\r
}\r
}\r
\r