From 96082841239a9407fcdaa7e3cb65f945ccd513aa Mon Sep 17 00:00:00 2001 From: naach Date: Tue, 27 Oct 2020 16:09:47 +0700 Subject: [PATCH 1/3] fix(regtest): network are not address prefix and refactor for clear easy to read and extend --- lib/Address.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/Address.ts b/lib/Address.ts index 84d36acb..cd9daf32 100644 --- a/lib/Address.ts +++ b/lib/Address.ts @@ -237,15 +237,29 @@ export class Address { network: string = "mainnet" ): string { let netParam: any - if (network !== "bitcoincash" && network !== "mainnet") - netParam = Bitcoin.networks.testnet - - const regtest: boolean = network === "bchreg" + let isRegtest: boolean + switch (network) { + case 'mainnet': + netParam = Bitcoin.networks.bitcoin + isRegtest = false + break + case 'testnet': + netParam = Bitcoin.networks.testnet + isRegtest = false + break + case 'regtest': + netParam = Bitcoin.networks.testnet + isRegtest = true + break + default: + netParam = Bitcoin.networks.bitcoin + isRegtest = false + } return this.toCashAddress( Bitcoin.address.fromOutputScript(scriptPubKey, netParam), true, - regtest + isRegtest ) } From ecc00e89f826650ee6cfa8cc2dd0fc49f302c79c Mon Sep 17 00:00:00 2001 From: naach Date: Tue, 27 Oct 2020 16:38:07 +0700 Subject: [PATCH 2/3] test(fromOutputScript): add regtest case to fromOutputScript test --- test/unit/Address.ts | 12 ++++++++++++ test/unit/fixtures/Address.json | 3 +++ 2 files changed, 15 insertions(+) diff --git a/test/unit/Address.ts b/test/unit/Address.ts index 28ddd878..68e452d3 100644 --- a/test/unit/Address.ts +++ b/test/unit/Address.ts @@ -1082,6 +1082,18 @@ describe("#Address", (): void => { }) } ) + + fixtures.p2shRegTest.forEach( + (address: string): void => { + const p2shAddress: any = bitbox.Address.fromOutputScript( + scriptPubKey, + "regtest" + ) + it(`generate regtest address from output script`, (): void => { + assert.equal(p2shAddress, address) + }) + } + ) }) describe("#details", () => { diff --git a/test/unit/fixtures/Address.json b/test/unit/fixtures/Address.json index 1d3a53e3..73cced88 100644 --- a/test/unit/fixtures/Address.json +++ b/test/unit/fixtures/Address.json @@ -225,6 +225,9 @@ "p2shTestnet": [ "bchtest:pz0qcslrqn7hr44hsszwl4lw5r6udkg6zqh2hmtpyr" ], + "p2shRegTest": [ + "bchreg:pz0qcslrqn7hr44hsszwl4lw5r6udkg6zqdkp6gj89" + ], "mainnetXPriv": [ { "xpriv": "xprvA2WwD9mk1Qd3rMjQ4ZRHvCWCj47jbXjY9Nf7npNRBmGUJngpRAvJzNpNgt7h2dDQ5huG7yFwYfz4PFJDPzkqfvBNPHnaio4yAbbUuv3EBnL", From 47a6cc285674ea2d125487de239dafbb78e32e4d Mon Sep 17 00:00:00 2001 From: naach Date: Tue, 27 Oct 2020 16:52:04 +0700 Subject: [PATCH 3/3] fix(tsc error): fix Cannot find name XXX in .d.ts tsc Version 3.9.7 --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 8e7a6d25..f0557db4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "target": "es5", "downlevelIteration": true, "moduleResolution": "node", - "lib": ["es2017"], + "lib": ["es2017", "dom"], "sourceMap": true, "esModuleInterop": true },