我将使用 OP_RETURN (testnet) 将数据嵌入到区块链中。
我在一个目录中有两个文件。第一个keys.js
包含为比特币测试网交易生成地址和私钥的代码。
键.js:
const bitcoin = require('bitcoinjs-lib');
const { testnet } = bitcoin.networks
const myKeyPair = bitcoin.ECPair.makeRandom({ network: testnet });
//extract the publickey
const publicKey = myKeyPair.publicKey;
//get the private key
const myWIF = myKeyPair.toWIF();
//get an address from the myKeyPair we generated above.
const { address } = bitcoin.payments.p2pkh({
pubkey: publicKey,
network: testnet
});
console.log("myAdress: " + address + " \nmyWIF: " + myWIF);
第二个op_return.js
包含允许我将随机文本嵌入区块链的方法。
这是 op_return.js 的结尾:
const importantMessage = 'RANDOM TEXT INTO BLOCKCHAIN'
buildOpReturnTransaction(myKeyPair, importantMessage)
.then(pushTransaction)
.then(response => console.log(response.data))
问题在于常量myKeyPair
,op_return.js
因为在输入node op_return
node.js 命令提示符后出现错误:
buildOpReturnTransaction(myKeyPair, importantMessage)
^
ReferenceError: myKeyPair is not defined
at Object.<anonymous> (C:\Users\Paul\Desktop\mydir\op_return:71:26)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47