mruddy / bip65-demos Goto Github PK
View Code? Open in Web Editor NEWDemonstrate uses of the CheckLockTimeVerify (aka CLTV or OP_CHECKLOCKTIMEVERIFY) opcode BIP65 BIP0065
License: MIT License
Demonstrate uses of the CheckLockTimeVerify (aka CLTV or OP_CHECKLOCKTIMEVERIFY) opcode BIP65 BIP0065
License: MIT License
Hi,
I'm trying to implement an hash time locked contract (HTLC) using the nodejs bitcore lib.
My work is based on leon-do's work.
I've therefore implemented a script very similar to your redeemScript. Here it is :
function createTransaction (privateKey, ammount, toAddress, timeLock, metadata, utxo, key) {
var privateKeyWIF = privateKey.toWIF();
var myPublicKey = new bitcore.PublicKey(privateKey,'testnet');
var fromAddress = privateKey.toAddress().toString();
var hashKey = bitcore.crypto.Hash.sha256(new Buffer(key)).toString('hex');
// build the script
var script = bitcore
.Script()
.add('OP_IF')
.add('OP_SHA256')
.add(new Buffer(hashKey.toString(), 'hex'))
.add('OP_EQUALVERIFY')
.add(bitcore.Script.buildPublicKeyHashOut(toAddress))
.add('OP_ELSE')
.add(bitcore.crypto.BN.fromNumber(timeLock).toScriptNumBuffer())
.add('OP_CHECKLOCKTIMEVERIFY')
.add('OP_DROP')
.add(bitcore.Script.buildPublicKeyHashOut(fromAddress))
.add('OP_ENDIF');
const scriptAddress = bitcore.Address.payingTo(script, 'testnet');
const newTransaction = bitcore
.Transaction()
.from(utxo)
.addOutput(
new bitcore.Transaction.Output({
script: script,
satoshis: ammount,
})
)
.change(fromAddress)
.addData(metadata)
.sign(privateKey);
return newTransaction;
}
The issue is that I don't understand what the timelock is supoosed to be, an absolute UNIX timestamp, a relative UNIX timestamp, an absolute block height, or a relative block height.
I've read somewhere that if the specified timelock is inferior or equal to 500M the it is considered as a block height and that if it is superior to 500M then it is considered as a UNIX timestamp.
So this leads me to believe that the specified timelock has to be a absolute whether it is a block height or a UNIX timestamp.
I've been testing several values, and the result I got are the following :
non-mandatory-script-verify-flag (Locktime requirement not satisfied) (code -26)
64: non-mandatory-script-verify-flag (Data push larger than necessary) (code -26)
non-mandatory-script-verify-flag (Locktime requirement not satisfied) (code -26)
error.If you have any clue on what the problem could be or a script definition of what the timelock should be like that would be very helpful.
Here is the redeem script I'm using :
function redeemTransaction(privateKey, transactionNumber, utxoData){
// get public key
var myPublicKey = new bitcore.PublicKey(privateKey);
// convert priv key to address
var fromAddress = privateKey.toAddress().toString();
//Format utxoData
var inputAmount = utxoData.value_int;
var scriptPubKey = utxoData.script_pub_key;
var sequenceNumber = utxoData.sequence;
var vout = utxoData.vout;
var refundTransaction = new bitcore.Transaction().from({
txid: transactionNumber,
vout: vout,
scriptPubKey: new bitcore.Script(scriptPubKey).toHex(),
satoshis: inputAmount,
})
.to(fromAddress, inputAmount - 1000) // send back to the original address for ease of testing only, the 1000 is the fee
.lockUntilDate(Math.floor(Date.now() / 1001)); // CLTV requires the transaction nLockTime to be >= the stack argument in the redeem script
refundTransaction.inputs[0].sequenceNumber = 0; // the CLTV opcode requires that the input's sequence number not be finalized
//Sign transaction
var signature = bitcore.Transaction.sighash.sign(refundTransaction, privateKey, bitcore.crypto.Signature.SIGHASH_ALL, 0, scriptPubKey);
// setup the scriptSig of the spending transaction to spend the p2sh-cltv-p2pkh redeem script
refundTransaction.inputs[0].setScript(
bitcore.Script.empty()
.add(signature.toTxFormat())
.add(new Buffer(myPublicKey.toString(), 'hex'))
.add('OP_FALSE') // choose the time-delayed refund code path
);
console.log('refundTransaction =', refundTransaction.toString());
}
Thank you
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.