Created
December 9, 2020 09:06
-
-
Save ccoincash/44327db63463f42ad69f059f7ddbfb4c to your computer and use it in GitHub Desktop.
a helper scrypt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract Backtrace { | |
static int DataLen = 46; | |
static function unpackunsigned(bytes b): int { | |
return unpack(b + b'00'); | |
} | |
static function readOutputScript(bytes tx, int outputIndex): bytes { | |
// first 4 bytes version | |
// 1 byte input num, only support max 253 | |
int pos = 4; | |
int ninputs = this.unpackunsigned(tx[pos: pos + 1]); | |
pos = pos + 1; | |
int i = 0; | |
int scriptLen = 0; | |
int varLen = 0; | |
bytes res = b''; | |
// max support 3 input | |
// input | |
loop(3) { | |
if (i < ninputs) { | |
// output point 36 bytes | |
pos = pos + 36; | |
// 1 byte var | |
// script code + 4 bytes sequence | |
varLen = this.unpackunsigned(tx[pos: pos + 1]); | |
if (varLen < 253) { | |
scriptLen = varLen; | |
pos = pos + 1 + scriptLen + 4; | |
} else if (varLen == 253) { | |
scriptLen = this.unpackunsigned(tx[pos + 1: pos + 3]); | |
pos = pos + 3 + scriptLen + 4; | |
} else if (varLen == 254) { | |
scriptLen = this.unpackunsigned(tx[pos + 1: pos + 5]); | |
pos = pos + 5 + scriptLen + 4; | |
} else { | |
scriptLen = this.unpackunsigned(tx[pos + 1: pos + 9]); | |
pos = pos + 9 + scriptLen + 4; | |
} | |
i = i + 1; | |
} | |
} | |
int noutputs = this.unpackunsigned(tx[pos: pos + 1]); | |
pos = pos + 1; | |
i = 0; | |
loop(3) { | |
if (i < noutputs) { | |
// 8 bytes value | |
pos = pos + 8; | |
// script code | |
varLen = this.unpackunsigned(tx[pos: pos + 1] + b'00'); | |
if (varLen < 253) { | |
scriptLen = varLen; | |
pos = pos + 1 + scriptLen; | |
} else if (varLen == 253) { | |
scriptLen = this.unpackunsigned(tx[pos + 1: pos + 3]); | |
pos = pos + 3 + scriptLen; | |
} else if (varLen == 254) { | |
scriptLen = this.unpackunsigned(tx[pos + 1: pos + 5]); | |
pos = pos + 5 + scriptLen; | |
} else { | |
scriptLen = this.unpackunsigned(tx[pos + 1: pos + 9]); | |
pos = pos + 9 + scriptLen; | |
} | |
if (i == outputIndex) { | |
res = tx[pos - scriptLen: pos]; | |
} | |
i = i + 1; | |
} | |
} | |
// 4 bytes locktime | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment