Skip to content

Commit

Permalink
Merge pull request #106 from rkalis/scriptNumber
Browse files Browse the repository at this point in the history
Expose bitcoincashjs-lib Script.number
  • Loading branch information
Gabriel Cardona authored May 22, 2019
2 parents fd09685 + d5cb73c commit 131091a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/Script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,19 @@ 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
public pubKey: pubKey
public pubKeyHash: pubKeyHash
public multisig: multisig
public scriptHash: scriptHash
public number: scriptNumber

constructor() {
this.opcodes = opcodes
Expand Down Expand Up @@ -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<number | Buffer>): Buffer {
Expand Down Expand Up @@ -389,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);
}
}
18 changes: 18 additions & 0 deletions test/unit/Script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,22 @@ describe("#Script", (): void => {
})
})
})

describe("#number", (): void => {
fixtures.scriptNumber.forEach((fixture: any): void => {
it(`should encode number ${fixture.decoded} as bytes ${fixture.encoded}`, (): void => {
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 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)
})
})
})
})
14 changes: 14 additions & 0 deletions test/unit/fixtures/Script.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}

0 comments on commit 131091a

Please sign in to comment.