Giter Site home page Giter Site logo

bip65-demos's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

bip65-demos's Issues

Lock time absolute or relative

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 :

  • If the value is a unix timestamp older than 16 days, the redeem transaction works
  • If the value is a unix timestamp 15 days old or younger, the redeem transactions does not works and returns this error non-mandatory-script-verify-flag (Locktime requirement not satisfied) (code -26)
  • If the value is a relative block height (I've tested 1 for example) redeem script does not work and I get this error 64: non-mandatory-script-verify-flag (Data push larger than necessary) (code -26)
  • If the value is an absolute block height (1,2M for example) the redeem script does not work and I get a 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.