Script

解鎖 P2WSH 的問題

  • January 3, 2022

我從腳本中使用 bitcoinjs-lib 創建了一個 P2WSH 地址。相同的腳本導致與比特幣核心相同的地址。我製作了一個解鎖腳本並測試了它的組合,btcdeb一切看起來都很好,所以我發送了一些 sats 到該地址並嘗試解鎖它。但是,在嘗試廣播交易時,比特幣核心給了我這個錯誤。

non-mandatory-script-verify-flag (unknown error)

腳本是通過添加t時間OP_OVER OP_SUB OP_1 OP_EQUALVERIFYOP_0 OP_EQUAL結尾來製作的

您可以通過將程式碼放入文件中https://github.com/antonilol/btc_stuff並執行npm i. (用於bitcoinjs-lib包和文件btc.jsbtc2.js)在執行測試網的節點上

我的程式碼:

const { send } = require('./btc')();
const { bech32toScriptPubKey } = require('./btc2');
const bitcoin = require('bitcoinjs-lib');
const network = bitcoin.networks.testnet;

const witnessScriptPiece = bitcoin.script.compile([
   bitcoin.opcodes.OP_OVER,
   bitcoin.opcodes.OP_SUB,
   bitcoin.opcodes.OP_1,
   bitcoin.opcodes.OP_EQUALVERIFY
]);

const l = witnessScriptPiece.length;

const witnessScriptEnd = bitcoin.script.compile([
   bitcoin.opcodes.OP_0,
   bitcoin.opcodes.OP_EQUAL
]);

const t = 3;

const witnessScript = Buffer.allocUnsafe(l * t + witnessScriptEnd.length);

for (var i = 0; i < t; i++) {
   witnessScriptPiece.copy(witnessScript, i * l);
}

witnessScriptEnd.copy(witnessScript, t * l);

console.log('Locking script: ' + witnessScript.toString('hex'));

console.log(
   'send 1000 sat to ' +
   bitcoin.payments.p2wsh({ redeem: { output: witnessScript, network }, network }).address
);

const tx = new bitcoin.Transaction(network);

// const txid = 'b37fc02fda463403a4d077863d163f6c2738659a4bba77284711ea8eeb95fc7d'; // t=17 vout=0
const txid = '407e7513afdd4baedeac62870acb7e426a6f7aa85ca45f7e54411e8b74d8876c'; // t=3 vout=1
const vout = 1;

tx.addInput(Buffer.from(txid, 'hex').reverse(), vout);
tx.setWitness(0, [
   ...Array(t + 1).fill().map((x, i) => {
       const b = Buffer.allocUnsafe(1);
       b.writeInt8(i);
       return b;
   }),
   witnessScript
]);

const fee_sat = 110;
const input_sat = 1000;

tx.addOutput(bech32toScriptPubKey('tb1q3my0fxc7n5sfx9c76p9mdyk6ly6j22wgp644q6'), input_sat-fee_sat);

send(tx.toHex()).then(console.log)

它產生的原始 tx 十六進制t=3

010000000001016c87d8748b1e41547e5fa45ca87a6f6a427ecb0a8762acdeae4bddaf13757e400100000000ffffffff017a030000000000001600148ec8f49b1e9d2093171ed04bb692daf9352529c80501000101010201030e789451887894518878945188008700000000

t=17:

010000000001017dfc95eb8eea11472877ba4b9a6538276c3f163d8677d0a4033446da2fc07fb30000000000ffffffff017a030000000000001600148ec8f49b1e9d2093171ed04bb692daf9352529c8130100010101020103010401050106010701080109010a010b010c010d010e010f01100111467894518878945188789451887894518878945188789451887894518878945188789451887894518878945188789451887894518878945188789451887894518878945188008700000000

您遇到的錯誤是對數據進行最小編碼的要求。具體來說,您的見證堆棧包含0x00數字 0。但這不是 0 的最小編碼。相反,0 的最小編碼是一個空向量。所以你的交易實際上應該是

010000000001016c87d8748b1e41547e5fa45ca87a6f6a427ecb0a8762acdeae4bddaf13757e400100000000ffffffff017a030000000000001600148ec8f49b1e9d2093171ed04bb692daf9352529c805000101010201030e789451887894518878945188008700000000
010000000001017dfc95eb8eea11472877ba4b9a6538276c3f163d8677d0a4033446da2fc07fb30000000000ffffffff017a030000000000001600148ec8f49b1e9d2093171ed04bb692daf9352529c81300010101020103010401050106010701080109010a010b010c010d010e010f01100111467894518878945188789451887894518878945188789451887894518878945188789451887894518878945188789451887894518878945188789451887894518878945188008700000000

引用自:https://bitcoin.stackexchange.com/questions/111590