Skip to content

Commit

Permalink
Merge pull request #99 from Bitcoin-com/update-script-class
Browse files Browse the repository at this point in the history
Updtes to the Script class and tests.
  • Loading branch information
Gabriel Cardona authored May 14, 2019
2 parents f8151b5 + 7a9ad39 commit b584db9
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 282 deletions.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ interface NewOptions extends ConsoleOptions {
restURL: string
}

program.version("7.2.1 ", "-v, --version")
program.version("7.3.0 ", "-v, --version")

program
.command("new <name>")
Expand Down
259 changes: 221 additions & 38 deletions lib/Script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export interface opcodes {
OP_ENDIF: 104
OP_VERIFY: 105
OP_RETURN: 106

OP_TOALTSTACK: 107
OP_FROMALTSTACK: 108
OP_2DROP: 109
Expand All @@ -58,23 +57,19 @@ export interface opcodes {
OP_ROT: 123
OP_SWAP: 124
OP_TUCK: 125

OP_CAT: 126

OP_SPLIT: 127
OP_NUM2BIN: 128
OP_BIN2NUM: 129
OP_SIZE: 130

OP_INVERT: 131
OP_AND: 132 // May 2018 reenabled
OP_OR: 133 // May 2018 reenabled
OP_XOR: 134 // May 2018 reenabled
OP_AND: 132
OP_OR: 133
OP_XOR: 134
OP_EQUAL: 135
OP_EQUALVERIFY: 136
OP_RESERVED1: 137
OP_RESERVED2: 138

OP_1ADD: 139
OP_1SUB: 140
OP_2MUL: 141
Expand All @@ -86,11 +81,10 @@ export interface opcodes {
OP_ADD: 147
OP_SUB: 148
OP_MUL: 149
OP_DIV: 150 // May 2018 reenabled
OP_MOD: 151 // May 2018 reenabled
OP_DIV: 150
OP_MOD: 151
OP_LSHIFT: 152
OP_RSHIFT: 153

OP_BOOLAND: 154
OP_BOOLOR: 155
OP_NUMEQUAL: 156
Expand All @@ -102,9 +96,7 @@ export interface opcodes {
OP_GREATERTHANOREQUAL: 162
OP_MIN: 163
OP_MAX: 164

OP_WITHIN: 165

OP_RIPEMD160: 166
OP_SHA1: 167
OP_SHA256: 168
Expand All @@ -115,15 +107,11 @@ export interface opcodes {
OP_CHECKSIGVERIFY: 173
OP_CHECKMULTISIG: 174
OP_CHECKMULTISIGVERIFY: 175

OP_NOP1: 176

OP_NOP2: 177
OP_CHECKLOCKTIMEVERIFY: 177

OP_NOP3: 178
OP_CHECKSEQUENCEVERIFY: 178

OP_NOP4: 179
OP_NOP5: 180
OP_NOP6: 181
Expand All @@ -133,26 +121,113 @@ export interface opcodes {
OP_NOP10: 185
OP_CHECKDATASIG: 186
OP_CHECKDATASIGVERIFY: 187

OP_PUBKEYHASH: 253
OP_PUBKEY: 254
OP_INVALIDOPCODE: 255
}

export interface DecodedP2PKHInput {
signature: Buffer
pubKey: Buffer
}

export interface DecodedP2MSOutput {
m: number
pubKeys: Buffer[]
}

export interface DecodedP2SHInput {
redeemScript: Buffer
redeemScriptSig: Buffer
}

export interface nullData {
output: {
encode(data: Buffer): Buffer
decode(output: Buffer): Buffer
check(output: Buffer): boolean
}
}

export interface pubKey {
input:
{
encode(signature: Buffer): Buffer
decode(input: Buffer): Buffer
check(input: Buffer): boolean
decodeStack(data: Buffer): Buffer
encodeStack(data: Buffer): Buffer
},
output: {
encode(pubKey: Buffer): Buffer
decode(output: Buffer): Buffer
check(output: Buffer): boolean
}
}

export interface pubKeyHash {
input:
{
encode(signature: Buffer, pubKey: Buffer): Buffer
decode(data: Buffer): DecodedP2PKHInput
check(data: Buffer): boolean
decodeStack(data: Buffer): Buffer
encodeStack(data: Buffer): Buffer
},
output:
{
encode(identifier: Buffer): Buffer
decode(output: Buffer): Buffer
check(output: Buffer): boolean
}
}

export interface multisig {
input:
{
encode(signatures: Buffer[]): Buffer
decode(input: Buffer): Buffer[]
check(input: Buffer): boolean
},
output:
{
encode(m: number, pubKeys: Buffer[]): Buffer
decode(output: Buffer): DecodedP2MSOutput
check(output: Buffer): boolean
}
}

export interface scriptHash {
input:
{
encode(redeemScriptSig: Buffer, redeemScript: Buffer): Buffer
decode(input: Buffer): DecodedP2SHInput
check(data: Buffer): boolean
decodeStack(data: Buffer): Buffer
encodeStack(data: Buffer): Buffer
},
output:
{
encode(scriptHash: Buffer): Buffer
decode(output: Buffer): Buffer
check(output: Buffer): boolean
}
}

export class Script {
opcodes: opcodes
nullData: any
multisig: any
pubKey: any
pubKeyHash: any
scriptHash: any
public opcodes: opcodes
public nullData: nullData
public pubKey: pubKey
public pubKeyHash: pubKeyHash
public multisig: multisig
public scriptHash: scriptHash

constructor() {
this.opcodes = opcodes
this.nullData = Bitcoin.script.nullData
this.multisig = {
input: {
encode: (signatures: any) => {
encode: (signatures: Buffer[]): Buffer => {
const sigs: any[] = []
signatures.forEach((sig: any) => {
sigs.push(sig)
Expand All @@ -163,7 +238,7 @@ export class Script {
check: Bitcoin.script.multisig.input.check
},
output: {
encode: (m: any, pubKeys: any) => {
encode: (m: number, pubKeys: Buffer[]): Buffer => {
const pks: any[] = []
pubKeys.forEach((pubKey: any) => {
pks.push(pubKey)
Expand All @@ -179,6 +254,26 @@ export class Script {
this.scriptHash = Bitcoin.script.scriptHash
}

public encode(scriptChunks: Array<number | Buffer>): Buffer {
const arr: Array<number | Buffer> = []
scriptChunks.forEach((chunk: number | Buffer) => {
arr.push(chunk)
})
return Bitcoin.script.compile(arr)
}

public decode(scriptBuffer: Buffer): Array<number | Buffer> {
return Bitcoin.script.decompile(scriptBuffer)
}

public toASM(buffer: Buffer): string {
return Bitcoin.script.toASM(buffer)
}

public fromASM(asm: string): Buffer {
return Bitcoin.script.fromASM(asm)
}

public classifyInput(script: Buffer): string {
return Bitcoin.script.classifyInput(script)
}
Expand All @@ -187,23 +282,111 @@ export class Script {
return Bitcoin.script.classifyOutput(script)
}

public decode(scriptBuffer: any): any[] {
return Bitcoin.script.decompile(scriptBuffer)
public encodeNullDataOutput(data: Buffer): Buffer {
return this.nullData.output.encode(data)
}

public encode(scriptChunks: any): any {
const arr: any[] = []
scriptChunks.forEach((chunk: any) => {
arr.push(chunk)
})
return Bitcoin.script.compile(arr)
public decodeNullDataOutput(output: Buffer): Buffer {
return this.nullData.output.decode(output)
}

public toASM(buffer: Buffer): string {
return Bitcoin.script.toASM(buffer)
public checkNullDataOutput(output: Buffer): boolean {
return this.nullData.output.check(output)
}

public fromASM(asm: string): any {
return Bitcoin.script.fromASM(asm)
public encodeP2PKInput(signature: Buffer): Buffer {
return this.pubKey.input.encode(signature)
}

public decodeP2PKInput(input: Buffer): Buffer {
return this.pubKey.input.decode(input)
}

public checkP2PKInput(input: Buffer): boolean {
return this.pubKey.input.check(input)
}

public encodeP2PKOutput(pubKey: Buffer): Buffer {
return this.pubKey.output.encode(pubKey)
}

public decodeP2PKOutput(output: Buffer): Buffer {
return this.pubKey.output.decode(output)
}

public checkP2PKOutput(output: Buffer): boolean {
return this.pubKey.output.check(output);
}

public encodeP2PKHInput(signature: Buffer, pubKey: Buffer): Buffer {
return this.pubKeyHash.input.encode(signature, pubKey)
}

public decodeP2PKHInput(input: Buffer): DecodedP2PKHInput {
return this.pubKeyHash.input.decode(input)
}

public checkP2PKHInput(input: Buffer): boolean {
return this.pubKeyHash.input.check(input)
}

public encodeP2PKHOutput(identifier: Buffer): Buffer {
return this.pubKeyHash.output.encode(identifier)
}

public decodeP2PKHOutput(output: Buffer): Buffer {
return this.pubKeyHash.output.decode(output)
}

public checkP2PKHOutput(output: Buffer): boolean {
return this.pubKeyHash.output.check(output);
}

public encodeP2MSInput(signatures: Buffer[]): Buffer {
return this.multisig.input.encode(signatures)
}

public decodeP2MSInput(input: Buffer): Buffer[] {
return this.multisig.input.decode(input);
}

public checkP2MSInput(input: Buffer): boolean {
return this.multisig.input.check(input)
}

public encodeP2MSOutput(m: number, pubKeys: Buffer[]): Buffer {
return this.multisig.output.encode(m, pubKeys)
}

public decodeP2MSOutput(output: Buffer): DecodedP2MSOutput {
return this.multisig.output.decode(output)
}

public checkP2MSOutput(output: Buffer): boolean {
return this.multisig.output.check(output);
}

public encodeP2SHInput(redeemScriptSig: Buffer, redeemScript: Buffer): Buffer {
return this.scriptHash.input.encode(redeemScriptSig, redeemScript)
}

public decodeP2SHInput(input: Buffer): DecodedP2SHInput {
return this.scriptHash.input.decode(input)
}

public checkP2SHInput(input: Buffer): boolean {
return this.scriptHash.input.check(input);
}

public encodeP2SHOutput(scriptHash: Buffer): Buffer {
return this.scriptHash.output.encode(scriptHash)
}

public decodeP2SHOutput(output: Buffer): Buffer {
return this.scriptHash.output.decode(output);
}

public checkP2SHOutput(output: Buffer): boolean {
return this.scriptHash.output.check(output)
}
}
5 changes: 3 additions & 2 deletions lib/TransactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class TransactionBuilder {
txHash: string,
vout: number,
sequence: number = this.DEFAULT_SEQUENCE,
prevOutScript: string | null = null
prevOutScript: string | Buffer | null = null
): void {
this.transaction.addInput(txHash, vout, sequence, prevOutScript)
}
Expand All @@ -86,9 +86,10 @@ export class TransactionBuilder {
this.p2shInput = true
}

public addOutput(scriptPubKey: string, amount: number): void {
public addOutput(scriptPubKey: string | Buffer, amount: number): void {
try {
this.transaction.addOutput(
// @ts-ignore
this._address.toLegacyAddress(scriptPubKey),
amount
)
Expand Down
Loading

0 comments on commit b584db9

Please sign in to comment.