Giter Club home page Giter Club logo

Comments (4)

dgpv avatar dgpv commented on August 16, 2024

You can look into python-elementstx examples for the code that spends p2wsh address: https://github.com/Simplexum/python-elementstx/blob/4858587d27c9cdc16eb4d1556f95be33e7406abe/examples/confidential-cross-chain-atomic-swap.py#L602-L646

or look into implementation of PartiallySignedTransaction.sign() - it has code that signs various types of inputs.

more example code is of course welcome if you or anyone are willing to create that.

from python-bitcointx.

mrx23dot avatar mrx23dot commented on August 16, 2024

Thank you, I will give it a go.

from python-bitcointx.

mrx23dot avatar mrx23dot commented on August 16, 2024

Some minimal help would be appreciated, all I'm trying to do is transfer from bc1xx to bc1xx.

No idea what to do with these, big example mentions 3 alice,bob and contract keys.

  spend_key=None # from privkeyBytes parameter ?
  btc_contract='?'

I guess branch_condition true is to not to use lock times.
Do we know the numof vBytes in advance to calculate the fee?

This is what I have:

from bitcointx.wallet import CCoinAddress
from bitcointx.core import CMutableTransaction,CTxIn, CTxOut,COutPoint,lx,CTxInWitness
from bitcointx.core.script import SIGHASH_ALL,SIGVERSION_WITNESS_V0, DATA,CScriptWitness,CBitcoinScript

def sweep_bech32_tx(utxos, toAddrStr, privkeyB, feeSat=200):

  spend_key=None # from privkeyB parameter ?
  btc_contract='?'

  in_amount = sum([amntSat for txid,vout_n,amntSat in utxos])
  out_amount = in_amount - feeSat

  # Create the unsigned transaction.
  tx = CMutableTransaction(
    vin=[CTxIn(prevout=COutPoint(hash=lx(txid), n=vout_n)) for txid,vout_n,amntSat in utxos],

    vout=[CTxOut(nValue=out_amount, scriptPubKey=CCoinAddress(toAddrStr).to_scriptPubKey())]
  )
  cond = b'\x01' # branch_condition true
  # We used P2WSHCoinAddress to create the address that we sent bitcoin to, so we know that we need to use SIGVERSION_WITNESS_V0
  sighash = btc_contract.sighash(tx, 0, SIGHASH_ALL, amount=in_amount, sigversion=SIGVERSION_WITNESS_V0)
  spend_sig = spend_key.sign(sighash) + bytes([SIGHASH_ALL])
  # This is script witness, not script. The condition for OP_IF
  # in our script is directly encoded as data in the witness.
  # We cannot use OP_TRUE/OP_FALSE here. We use DATA guard is to ensure that.
  witness = CScriptWitness([spend_sig, DATA(cond), btc_contract])
  # empty scriptSig, because segwit
  tx.vin[0].scriptSig = CBitcoinScript([])
  # all data to check the spend conditions is in the witness
  tx.wit.vtxinwit[0] = CTxInWitness(witness)
  # VerifyScript not working
  return tx

# inputs: (txid, vout_n, amntSat)
utxos = [
  ('097fed228b1c4afb9dc8df2ae137e4be5e1c1376541c2db3b1c74a6166e7842b', 0, 178876), 
]
tx = sweep_bech32_tx(utxos, toAddrStr='bc1..', privkeyB=bytes.fromhex('11'*32))
print(tx) 

from python-bitcointx.

dgpv avatar dgpv commented on August 16, 2024

Hmm, I guess linking to python-elementstx example was not helpful, because that example uses custom bitcoin scripts and that is not the same as signing simple keypath spending.

You probably better use PartiallySignedTransaction's sign() method, you can create psbt from CTransaction like in this test code: https://github.com/Simplexum/python-bitcointx/blob/master/bitcointx/tests/test_psbt.py#L401 and then call psbt.sign(keystore) where keystore is an instance of KeyStore which contains your keys

from python-bitcointx.

Related Issues (20)

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.