Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub fn validate(scriptSig: []const u8, scriptPubKey: []const u8, transaction: ?*Tx, input_index: ?usize, alloc: mem.Allocator) Error!bool {
- const Op = Script.Opcode;
- var stack = try Stack.init(scriptSig.len + scriptPubKey.len, alloc);
- defer stack.deinit(alloc);
- stack.setAlloc(alloc);
- const Local = struct {
- fn handleError(err: Script.RunError) !bool {
- return switch (err) {
- error.Internal, error.StackFull => error.Internal,
- mem.Allocator.Error.OutOfMemory => error.OutOfMemory,
- error.BadScript, error.VerifyFailed => false,
- };
- }
- };
- // detect and handle special p2sh pattern
- const is_p2sh = scriptPubKey[0] == Op.HASH_160 and
- scriptPubKey[1] == Op.OP_PUSHDATA1 and
- scriptPubKey[2] == 0x14 and
- scriptPubKey[22] == Op.OP_EQUAL;
- var redeem_buf: [Stack.MAX_STACK_ELEMENT_SIZE]u8 = undefined;
- var redeem_script: []u8 = undefined;
- Script.run(scriptSig, &stack, transaction, input_index, alloc) catch |err| return Local.handleError(err);
- if (is_p2sh) {
- redeem_script = stack.pop(&redeem_buf) catch |err| return Local.handlePopError(err);
- stack.push(redeem_script);
- }
- Script.run(scriptPubKey, &stack, transaction, input_index, alloc) catch |err| return Local.handleError(err);
- if (is_p2sh) {
- Script.run(&redeem_script, &stack, transaction, input_index, alloc) catch |err| return Local.handleError(err);
- }
- return stack.verify();
- }
Advertisement
Add Comment
Please, Sign In to add comment