Skip to content

Instantly share code, notes, and snippets.

@xhliu

xhliu/scriptEval.js

Last active Dec 2, 2019
Embed
What would you like to do?
Sample code to evaluate bitcoin script using Bitcoin SV Library (bsv) https://docs.moneybutton.com/docs/bsv-overview.html
let bsv = require('bsv')
const slockStr = "OP_2 OP_EQUAL"
const slock = bsv.Script.fromASM(slockStr)
console.log("\nLocking script loaded\n")
const sunlockStr = "OP_2"
const sunlock = bsv.Script.fromASM(sunlockStr)
console.log("\nUnlocking script loaded\n")
const opcode_cnt = slock.chunks.length + sunlock.chunks.length
console.log(opcode_cnt + " opcodes to process ...")
const si = bsv.Script.Interpreter()
const verified = si.verify(sunlock, slock, null, null, bsv.Script.Interpreter.SCRIPT_VERIFY_P2SH |
bsv.Script.Interpreter.SCRIPT_ENABLE_MAGNETIC_OPCODES | bsv.Script.Interpreter.SCRIPT_ENABLE_MONOLITH_OPCODES)
if (verified) {
console.log("\n\x1b[32m%s\x1b[0m", "True")
} else {
console.log("\n\x1b[31m%s\x1b[0m", "False: " + si.errstr)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment