From 656cbcad49fe04cbc0cb2874cc21cf1c56b5e31b Mon Sep 17 00:00:00 2001 From: homura Date: Tue, 21 May 2024 16:00:48 +0900 Subject: [PATCH] BREAKING CHANGE: drop fs support in xpubstore (#691) --- packages/hd/src/xpub_store.ts | 18 +----------------- packages/hd/tests/xpub_store.test.ts | 2 +- website/docs/migrations/migrate-to-v0.23.md | 2 ++ 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/packages/hd/src/xpub_store.ts b/packages/hd/src/xpub_store.ts index c205c051e..4168052c2 100644 --- a/packages/hd/src/xpub_store.ts +++ b/packages/hd/src/xpub_store.ts @@ -1,5 +1,4 @@ import { AccountExtendedPublicKey } from "./extended_key"; -import fs from "fs"; export class XPubStore { private accountExtendedPublicKey: AccountExtendedPublicKey; @@ -12,28 +11,13 @@ export class XPubStore { return this.accountExtendedPublicKey; } - save( - path: string, - { - overwrite = false, - }: { - overwrite?: boolean; - } = {} - ) { - if (!overwrite && fs.existsSync(path)) { - throw new Error("XPub file already exists!"); - } - fs.writeFileSync(path, this.toJson()); - } - toJson(): string { return JSON.stringify({ xpubkey: this.accountExtendedPublicKey.serialize().slice(2), }); } - static load(path: string): XPubStore { - const json = fs.readFileSync(path, "utf-8"); + static fromJson(json: string): XPubStore { const xpub = JSON.parse(json).xpubkey; const accountExtendedPublicKey = AccountExtendedPublicKey.parse( "0x" + xpub diff --git a/packages/hd/tests/xpub_store.test.ts b/packages/hd/tests/xpub_store.test.ts index 4539df1ac..711df452b 100644 --- a/packages/hd/tests/xpub_store.test.ts +++ b/packages/hd/tests/xpub_store.test.ts @@ -12,7 +12,7 @@ const accountExtendedPublicKey = new AccountExtendedPublicKey( const filePath = __dirname + "/fixtures/xpub.json"; test("load", (t) => { - const xpub = XPubStore.load(filePath); + const xpub = XPubStore.fromJson(fs.readFileSync(filePath).toString()); const a = xpub.toAccountExtendedPublicKey(); t.is(a.publicKey, accountExtendedPublicKey.publicKey); diff --git a/website/docs/migrations/migrate-to-v0.23.md b/website/docs/migrations/migrate-to-v0.23.md index 472090fe8..83eaa8afa 100644 --- a/website/docs/migrations/migrate-to-v0.23.md +++ b/website/docs/migrations/migrate-to-v0.23.md @@ -15,6 +15,8 @@ Lumos dropped the support of reading configuration from `process.env.LUMOS_CONFI ### `@ckb-lumos/hd`.`Keystore`.`load` and ``save` Are Dropped +### `@ckb-lumos/hd`.`XPubStore`.`load` and ``save` Are Dropped + ### `#ckb-lumos/hd-cache`.`CacheManager`.`loadFromKeystore` Is Dropped Please migrate to `loadFromKeystoreJson`