From a0c346e7cfe0ba2cc8c9aeb86bf7482d2047d2a6 Mon Sep 17 00:00:00 2001 From: Rosco Kalis Date: Mon, 20 May 2019 21:43:37 +0900 Subject: [PATCH 1/2] Expose bitcoincashjs-lib Script.number --- lib/Script.ts | 7 +++++++ test/unit/Script.ts | 14 ++++++++++++++ test/unit/fixtures/Script.json | 14 ++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/lib/Script.ts b/lib/Script.ts index 6aba722a..79a2246b 100644 --- a/lib/Script.ts +++ b/lib/Script.ts @@ -214,6 +214,11 @@ export interface scriptHash { } } +export interface scriptNumber { + encode(number: number): Buffer + decode(buffer: Buffer, maxLength?: number, minimal?: boolean): number +} + export class Script { public opcodes: opcodes public nullData: nullData @@ -221,6 +226,7 @@ export class Script { public pubKeyHash: pubKeyHash public multisig: multisig public scriptHash: scriptHash + public number: scriptNumber constructor() { this.opcodes = opcodes @@ -252,6 +258,7 @@ export class Script { this.pubKey = Bitcoin.script.pubKey this.pubKeyHash = Bitcoin.script.pubKeyHash this.scriptHash = Bitcoin.script.scriptHash + this.number = Bitcoin.script.number } public encode(scriptChunks: Array): Buffer { diff --git a/test/unit/Script.ts b/test/unit/Script.ts index 8bdec629..ef95dad3 100644 --- a/test/unit/Script.ts +++ b/test/unit/Script.ts @@ -429,4 +429,18 @@ describe("#Script", (): void => { }) }) }) + + describe("#scriptNumber", (): void => { + fixtures.scriptNumber.forEach((fixture: any): void => { + it(`should encode number ${fixture.decoded} as bytes ${fixture.encoded}`, (): void => { + const encoded = bitbox.Script.number.encode(fixture.decoded) + assert.equal(encoded.toString("hex"), fixture.encoded) + }) + + it(`should decode bytes ${fixture.encoded} as number ${fixture.decoded}`, (): void => { + const decoded = bitbox.Script.number.decode(Buffer.from(fixture.encoded, 'hex')) + assert.equal(decoded, fixture.decoded) + }) + }) + }) }) diff --git a/test/unit/fixtures/Script.json b/test/unit/fixtures/Script.json index db95b382..ab45e230 100644 --- a/test/unit/fixtures/Script.json +++ b/test/unit/fixtures/Script.json @@ -654,5 +654,19 @@ "output": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", "hex": "a9141b61ebed0c2a16c699a99c3d5ef4d08de7fb1cb887" } + ], + "scriptNumber": [ + { + "decoded": 1, + "encoded": "01" + }, + { + "decoded": -1, + "encoded": "81" + }, + { + "decoded": 1000, + "encoded": "e803" + } ] } From d5cb73cfa35a90535c31362c443b46465efe8506 Mon Sep 17 00:00:00 2001 From: Rosco Kalis Date: Tue, 21 May 2019 12:01:50 +0900 Subject: [PATCH 2/2] Add wrapper methods around number.{en|de}code to Script --- lib/Script.ts | 8 ++++++++ test/unit/Script.ts | 14 +++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/Script.ts b/lib/Script.ts index 79a2246b..7c61e143 100644 --- a/lib/Script.ts +++ b/lib/Script.ts @@ -396,4 +396,12 @@ export class Script { public checkP2SHOutput(output: Buffer): boolean { return this.scriptHash.output.check(output) } + + public encodeNumber(number: number): Buffer { + return this.number.encode(number); + } + + public decodeNumber(buffer: Buffer, maxLength?: number, minimal?: boolean): number { + return this.number.decode(buffer, maxLength, minimal); + } } diff --git a/test/unit/Script.ts b/test/unit/Script.ts index ef95dad3..3da586ef 100644 --- a/test/unit/Script.ts +++ b/test/unit/Script.ts @@ -430,16 +430,20 @@ describe("#Script", (): void => { }) }) - describe("#scriptNumber", (): void => { + describe("#number", (): void => { fixtures.scriptNumber.forEach((fixture: any): void => { it(`should encode number ${fixture.decoded} as bytes ${fixture.encoded}`, (): void => { - const encoded = bitbox.Script.number.encode(fixture.decoded) - assert.equal(encoded.toString("hex"), fixture.encoded) + const encoded1 = bitbox.Script.number.encode(fixture.decoded) + const encoded2 = bitbox.Script.encodeNumber(fixture.decoded) + assert.equal(encoded1.toString("hex"), fixture.encoded) + assert.equal(encoded2.toString("hex"), fixture.encoded) }) it(`should decode bytes ${fixture.encoded} as number ${fixture.decoded}`, (): void => { - const decoded = bitbox.Script.number.decode(Buffer.from(fixture.encoded, 'hex')) - assert.equal(decoded, fixture.decoded) + const decoded1 = bitbox.Script.number.decode(Buffer.from(fixture.encoded, 'hex')) + const decoded2 = bitbox.Script.decodeNumber(Buffer.from(fixture.encoded, 'hex')) + assert.equal(decoded1, fixture.decoded) + assert.equal(decoded2, fixture.decoded) }) }) })