From d7c12bc94ae247c01674f07011071e37043a8195 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Tue, 1 Nov 2022 12:30:41 +0100 Subject: [PATCH] Bump libwally-core to 0.8.6 The secp256k1-zkp submodule at CLibWally/libwally-core/src/secp256k1 is updated along with libwally-core. In build-libwally.sh script we switch back to a vanilla libwally at the same commit that Bitcoin Core uses. Although Bitcoin Core bumped that commit a few months ago, we don't change it here because secp256k1-zkp has not yet been rebased on it. There were two breaking change in libwally-core: 1. wally_psbt_from_bytes got an additional FLAGS argument in 0fb94751def8b2c767680e2428ebe2fdabd436c7 (ElementsProject/libwally-core#336): trivially fixed by setting it to 0 2. wally_psbt_output no longer has a witness_script field as of 8f8481a3c509fc4a02425db3839f92594e6ea852 (ElementsProject/libwally-core#330). This required a more tedious workaround. --- CLibWally/libwally-core | 2 +- CLibWally/module.modulemap | 1 + LibWally/PSBT.swift | 33 +++++++++++++++++++++++++-------- README.md | 2 +- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/CLibWally/libwally-core b/CLibWally/libwally-core index f7c0824e..26f588f1 160000 --- a/CLibWally/libwally-core +++ b/CLibWally/libwally-core @@ -1 +1 @@ -Subproject commit f7c0824e56a068c4d9c27cb2e8b26e2a9b8ea3b3 +Subproject commit 26f588f175670fbcdf2e12771addf58819d39a40 diff --git a/CLibWally/module.modulemap b/CLibWally/module.modulemap index aaf3b526..2d79b8e2 100644 --- a/CLibWally/module.modulemap +++ b/CLibWally/module.modulemap @@ -6,6 +6,7 @@ module CLibWally { header "libwally-core/include/wally_bip38.h" header "libwally-core/include/wally_bip39.h" header "libwally-core/include/wally_psbt.h" + header "libwally-core/include/wally_psbt_members.h" header "libwally-core/include/wally_script.h" header "libwally-core/include/wally_transaction.h" export * diff --git a/LibWally/PSBT.swift b/LibWally/PSBT.swift index 5c19a8f7..11721e33 100644 --- a/LibWally/PSBT.swift +++ b/LibWally/PSBT.swift @@ -97,7 +97,7 @@ public struct PSBTOutput : Identifiable { return self.txOutput.scriptPubKey.bytes.hexString + String(self.txOutput.amount) } - init(_ wally_psbt_outputs: UnsafeMutablePointer, tx: wally_tx, index: Int, network: Network) { + init(_ wally_psbt: wally_psbt, _ wally_psbt_outputs: UnsafeMutablePointer, tx: wally_tx, index: Int, network: Network) { precondition(index >= 0 && index < tx.num_outputs) precondition(tx.num_outputs != 0 ) self.wally_psbt_output = wally_psbt_outputs[index] @@ -107,11 +107,28 @@ public struct PSBTOutput : Identifiable { self.origins = nil } let output = tx.outputs![index] - let scriptPubKey: ScriptPubKey - if let scriptPubKeyBytes = self.wally_psbt_output.witness_script { - scriptPubKey = ScriptPubKey(Data(bytes: scriptPubKeyBytes, count: self.wally_psbt_output.witness_script_len)) - } else { - scriptPubKey = ScriptPubKey(Data(bytes: output.script, count: output.script_len)) + // This is overriden with the witness script if found in the PSBT + var scriptPubKey = ScriptPubKey(Data(bytes: output.script, count: output.script_len)) + + let witnessScriptLen = UnsafeMutablePointer.allocate(capacity: 1) + let witnessScriptWritten = UnsafeMutablePointer.allocate(capacity: 1) + defer { + witnessScriptLen.deallocate() + witnessScriptWritten.deallocate() + } + // const struct wally_psbt *psbt, size_t index, size_t *written + var wally_psbt_tmp = wally_psbt // Copy to allow unsafe access + precondition(wally_psbt_get_output_witness_script_len(&wally_psbt_tmp, index, witnessScriptLen) == WALLY_OK) + + if witnessScriptLen.pointee > 0 { + let witnessScriptBytes = UnsafeMutablePointer.allocate(capacity: witnessScriptLen.pointee) + defer { + witnessScriptBytes.deallocate() + } + precondition(wally_psbt_get_output_witness_script(&wally_psbt_tmp, index, witnessScriptBytes, witnessScriptLen.pointee, witnessScriptWritten) == WALLY_OK) + + precondition(witnessScriptLen.pointee == witnessScriptWritten.pointee) + scriptPubKey = ScriptPubKey(Data(bytes: witnessScriptBytes, count: witnessScriptLen.pointee)) } self.txOutput = TxOutput(tx_output: output, scriptPubKey: scriptPubKey, network: network) @@ -263,7 +280,7 @@ public struct PSBT : Equatable { wally_psbt.deallocate() } } - guard wally_psbt_from_bytes(psbt_bytes, psbt_bytes_len, &output) == WALLY_OK else { + guard wally_psbt_from_bytes(psbt_bytes, psbt_bytes_len, UInt32(0), &output) == WALLY_OK else { // libwally-core returns WALLY_EINVAL regardless of why parsing fails throw ParseError.invalid } @@ -277,7 +294,7 @@ public struct PSBT : Equatable { self.inputs = inputs var outputs: [PSBTOutput] = [] for i in 0..