Merge pull request #1502 from antonilol/script-display-2

fix wrong display of other sighash types + p2sh redeemScript when <=4 bytes + consistency with esplora backend + tapscript fixes
This commit is contained in:
softsimon 2022-04-12 22:32:21 +04:00 committed by GitHub
commit cb894221dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 33 deletions

View File

@ -147,7 +147,7 @@ class BitcoinApi implements AbstractBitcoinApi {
scriptpubkey: vout.scriptPubKey.hex, scriptpubkey: vout.scriptPubKey.hex,
scriptpubkey_address: vout.scriptPubKey && vout.scriptPubKey.address ? vout.scriptPubKey.address scriptpubkey_address: vout.scriptPubKey && vout.scriptPubKey.address ? vout.scriptPubKey.address
: vout.scriptPubKey.addresses ? vout.scriptPubKey.addresses[0] : '', : vout.scriptPubKey.addresses ? vout.scriptPubKey.addresses[0] : '',
scriptpubkey_asm: vout.scriptPubKey.asm ? this.convertScriptSigAsm(vout.scriptPubKey.asm) : '', scriptpubkey_asm: vout.scriptPubKey.asm ? this.convertScriptSigAsm(vout.scriptPubKey.hex) : '',
scriptpubkey_type: this.translateScriptPubKeyType(vout.scriptPubKey.type), scriptpubkey_type: this.translateScriptPubKeyType(vout.scriptPubKey.type),
}; };
}); });
@ -157,7 +157,7 @@ class BitcoinApi implements AbstractBitcoinApi {
is_coinbase: !!vin.coinbase, is_coinbase: !!vin.coinbase,
prevout: null, prevout: null,
scriptsig: vin.scriptSig && vin.scriptSig.hex || vin.coinbase || '', scriptsig: vin.scriptSig && vin.scriptSig.hex || vin.coinbase || '',
scriptsig_asm: vin.scriptSig && this.convertScriptSigAsm(vin.scriptSig.asm) || '', scriptsig_asm: vin.scriptSig && this.convertScriptSigAsm(vin.scriptSig.hex) || '',
sequence: vin.sequence, sequence: vin.sequence,
txid: vin.txid || '', txid: vin.txid || '',
vout: vin.vout || 0, vout: vin.vout || 0,
@ -290,38 +290,68 @@ class BitcoinApi implements AbstractBitcoinApi {
return transaction; return transaction;
} }
private convertScriptSigAsm(str: string): string { private convertScriptSigAsm(hex: string): string {
const a = str.split(' '); const buf = Buffer.from(hex, 'hex');
const b: string[] = []; const b: string[] = [];
a.forEach((chunk) => {
if (chunk.substr(0, 3) === 'OP_') { let i = 0;
chunk = chunk.replace(/^OP_(\d+)$/, 'OP_PUSHNUM_$1'); while (i < buf.length) {
chunk = chunk.replace('OP_CHECKSEQUENCEVERIFY', 'OP_CSV'); const op = buf[i];
chunk = chunk.replace('OP_CHECKLOCKTIMEVERIFY', 'OP_CLTV'); if (op >= 0x01 && op <= 0x4e) {
b.push(chunk); i++;
let push: number;
if (op === 0x4c) {
push = buf.readUInt8(i);
b.push('OP_PUSHDATA1');
i += 1;
} else if (op === 0x4d) {
push = buf.readUInt16LE(i);
b.push('OP_PUSHDATA2');
i += 2;
} else if (op === 0x4e) {
push = buf.readUInt32LE(i);
b.push('OP_PUSHDATA4');
i += 4;
} else { } else {
chunk = chunk.replace('[ALL]', '01'); push = op;
if (chunk === '0') { b.push('OP_PUSHBYTES_' + push);
}
const data = buf.slice(i, i + push);
if (data.length !== push) {
break;
}
b.push(data.toString('hex'));
i += data.length;
} else {
if (op === 0x00) {
b.push('OP_0'); b.push('OP_0');
} else if (chunk.match(/^[^0]\d*$/)) { } else if (op === 0x4f) {
const chunkInt = parseInt(chunk, 10); b.push('OP_PUSHNUM_NEG1');
if (chunkInt < 0) { } else if (op === 0xb1) {
b.push('OP_PUSHNUM_NEG' + -chunkInt); b.push('OP_CLTV');
} else if (op === 0xb2) {
b.push('OP_CSV');
} else if (op === 0xba) {
b.push('OP_CHECKSIGADD');
} else { } else {
b.push('OP_PUSHNUM_' + chunk); const opcode = bitcoinjs.script.toASM([ op ]);
if (opcode && op < 0xfd) {
if (/^OP_(\d+)$/.test(opcode)) {
b.push(opcode.replace(/^OP_(\d+)$/, 'OP_PUSHNUM_$1'));
} else {
b.push(opcode);
} }
} else { } else {
const dataLength = Math.round(chunk.length / 2); b.push('OP_RETURN_' + op);
if (dataLength > 255) {
b.push('OP_PUSHDATA2' + ' ' + chunk);
} else if (dataLength > 75) {
b.push('OP_PUSHDATA1' + ' ' + chunk);
} else {
b.push('OP_PUSHBYTES_' + dataLength + ' ' + chunk);
} }
} }
i += 1;
} }
}); }
return b.join(' '); return b.join(' ');
} }
@ -332,21 +362,21 @@ class BitcoinApi implements AbstractBitcoinApi {
if (vin.prevout.scriptpubkey_type === 'p2sh') { if (vin.prevout.scriptpubkey_type === 'p2sh') {
const redeemScript = vin.scriptsig_asm.split(' ').reverse()[0]; const redeemScript = vin.scriptsig_asm.split(' ').reverse()[0];
vin.inner_redeemscript_asm = this.convertScriptSigAsm(bitcoinjs.script.toASM(Buffer.from(redeemScript, 'hex'))); vin.inner_redeemscript_asm = this.convertScriptSigAsm(redeemScript);
if (vin.witness && vin.witness.length > 2) { if (vin.witness && vin.witness.length > 2) {
const witnessScript = vin.witness[vin.witness.length - 1]; const witnessScript = vin.witness[vin.witness.length - 1];
vin.inner_witnessscript_asm = this.convertScriptSigAsm(bitcoinjs.script.toASM(Buffer.from(witnessScript, 'hex'))); vin.inner_witnessscript_asm = this.convertScriptSigAsm(witnessScript);
} }
} }
if (vin.prevout.scriptpubkey_type === 'v0_p2wsh' && vin.witness) { if (vin.prevout.scriptpubkey_type === 'v0_p2wsh' && vin.witness) {
const witnessScript = vin.witness[vin.witness.length - 1]; const witnessScript = vin.witness[vin.witness.length - 1];
vin.inner_witnessscript_asm = this.convertScriptSigAsm(bitcoinjs.script.toASM(Buffer.from(witnessScript, 'hex'))); vin.inner_witnessscript_asm = this.convertScriptSigAsm(witnessScript);
} }
if (vin.prevout.scriptpubkey_type === 'v1_p2tr' && vin.witness && vin.witness.length > 1) { if (vin.prevout.scriptpubkey_type === 'v1_p2tr' && vin.witness && vin.witness.length > 1) {
const witnessScript = vin.witness[vin.witness.length - 2]; const witnessScript = vin.witness[vin.witness.length - 2];
vin.inner_witnessscript_asm = this.convertScriptSigAsm(bitcoinjs.script.toASM(Buffer.from(witnessScript, 'hex'))); vin.inner_witnessscript_asm = this.convertScriptSigAsm(witnessScript);
} }
} }

View File

@ -111,7 +111,10 @@
<td style="text-align: left;" [innerHTML]="vin.inner_redeemscript_asm | asmStyler"></td> <td style="text-align: left;" [innerHTML]="vin.inner_redeemscript_asm | asmStyler"></td>
</tr> </tr>
<tr *ngIf="vin.inner_witnessscript_asm"> <tr *ngIf="vin.inner_witnessscript_asm">
<td *ngIf="vin.prevout && vin.prevout.scriptpubkey_type == 'v1_p2tr'; else p2wsh" i18n="transactions-list.p2tr-tapscript">P2TR tapscript</td>
<ng-template #p2wsh>
<td i18n="transactions-list.p2wsh-witness-script">P2WSH witness script</td> <td i18n="transactions-list.p2wsh-witness-script">P2WSH witness script</td>
</ng-template>
<td style="text-align: left;" [innerHTML]="vin.inner_witnessscript_asm | asmStyler"></td> <td style="text-align: left;" [innerHTML]="vin.inner_witnessscript_asm | asmStyler"></td>
</tr> </tr>
<tr> <tr>

View File

@ -281,6 +281,7 @@ export class AsmStylerPipe implements PipeTransform {
case 'CHECKSIGVERIFY': case 'CHECKSIGVERIFY':
case 'CHECKMULTISIG': case 'CHECKMULTISIG':
case 'CHECKMULTISIGVERIFY': case 'CHECKMULTISIGVERIFY':
case 'CHECKSIGADD':
style = 'crypto'; style = 'crypto';
break; break;