def get_solver(ContractPrivkey): # todo to fix if & else solvers IFsolver = IfElseSolver(Branch.IF, P2pkhSolver(ContractPrivkey)) #TODO How to check/View ?? IFsolver = P2pkhSolver(ContractPrivkey) #IFscript = P2pkhSolver(ContractPrivkey); # #print(IFscript); Doesnt Work ELSEsolver = IfElseSolver(Branch.ELSE, TimelockSolver(P2pkhSolver(ContractPrivkey))) #TODO How to check/View ?? ELSEsolver = P2pkhSolver(ContractPrivkey) #ELSEscript=TimelockSolver(P2pkhSolver(ContractPrivkey)); #print(ELSEscript); # Doesnt Work return IFsolver, ELSEsolver
def onChainCbTxs(args): """ create on chain coinbase txs for a set of blocks (provided in the bundle arg) :param args: tuple of args :return: list of blocks with transactions that are coinbase spends, list of blocks with chans of the coinbase spent outs that matches lstSpendBlocks """ fee = args[0] objPrivMiner = args[1] dictIdToPub = args[2] iBlocks = args[3] bundle = args[4] cbSolver = P2pkhSolver(objPrivMiner) lstSpendBlocks = [[] for i in range(0, iBlocks)] lstTupTxidChans = [] rewards = [] for i in range(0, len(bundle)): outputs = bundle[i][0][0] reward = bundle[i][0][1] xTx = bundle[i][1] bi = bundle[i][2] objTx = SegWitTransaction.unhexlify(xTx) cbSpend, tupTxidChans = spendCb(fee, reward, objTx, outputs, cbSolver, dictIdToPub) lstTupTxidChans += [tupTxidChans] lstSpendBlocks[bi] += [cbSpend] rewards += [reward] return lstSpendBlocks, lstTupTxidChans, rewards
def test_sign_transaction(): network_params = net_query('tppc') provider = Explorer(network='tppc') key = Kutil(network='tppc', privkey=bytearray.fromhex('9e321f5379c2d1c4327c12227e1226a7c2e08342d88431dcbb0063e1e715a36c') ) dest_address = 'mwn75Gavp6Y1tJxca53HeCj5zzERqWagr6' unspent = provider.select_inputs(key.address, 0.63) # 0.69 output = tx_output(network='tppc', value=Decimal(0.1), n=0, script=p2pkh_script(network='tppc', address=dest_address) ) unsigned = MutableTransaction( version=1, ins=unspent['utxos'], outs=[output], locktime=Locktime(0), network=network_params, timestamp=int(time.time()), ) parent_outputs = [find_parent_outputs(provider, i) for i in unsigned.ins] solver = P2pkhSolver(key._private_key) signed = unsigned.spend(parent_outputs, [solver for i in parent_outputs]) assert isinstance(signed, Transaction)
def _get_signed_txn(self, wif_keys): """ :param wif_keys: list of wif keys corresponding with self.input_addresses addresses, in same order """ # NB: they are not guaranteed to be in order as there # may be more than one utxo associated with a single # address, but there will always be 1 solver associated # a private key unordered_solvers = [] unordered_tx_outs = [] unsigned = self._txn if self.is_signed: raise ValueError('cannot sign txn (already signed)') for key in wif_keys: # create btcpy PrivateKeys from input WIF format keys private_key = PrivateKey.from_wif(key) if self.is_segwit: pub_key = private_key.pub(compressed=True) s_solver = P2shSolver( P2wpkhV0Script(pub_key), P2wpkhV0Solver(private_key) ) unordered_solvers.append(s_solver) else: # create btcpy P2PKH Solvers from those PrivateKeys unordered_solvers.append(P2pkhSolver(private_key)) # a dict that matches the addresses (which are ordered the same as # their above WIF Keys) to their solvers addresses_solvers = dict(zip(self.input_addresses, unordered_solvers)) # from self._specific_utxo_data, take the output num, value and scriptPubKey # and create TxOuts representing the UTXO's that will be spent. # In a tuple with the address of the UTXO so the correct solver # can be found later for t in self._specific_utxo_data: unordered_tx_outs.append((t[2], TxOut(value=t[4], n=t[1], script_pubkey=Script.unhexlify(t[3])))) # unlike the lists defined at the top of the method, these are in # order i.e the solver in solvers[0] is the solver for the TxOut of # tx_outs[0]. this is required to pass them into the spend() method tx_outs = [] solvers = [] for t in unordered_tx_outs: address = t[0] tx_outs.append(t[1]) solvers.append(addresses_solvers[address]) signed = unsigned.spend(tx_outs, solvers) return signed
def solve(self, network: str = config["network"]) -> IfElseSolver: return IfElseSolver( branch=Branch.ELSE, inner_solver=RelativeTimelockSolver( sequence=Sequence(seq=self._sequence), inner_solver=P2pkhSolver(privk=PrivateKey.unhexlify( hexa=Wallet(network=network).from_root_xprivate_key( root_xprivate_key=self._root_xprivate_key).from_path( path=self._path).private_key()))))
def solve(self, network: str = config["network"]) -> IfElseSolver: return IfElseSolver( branch=Branch.IF, inner_solver=HashlockSolver( preimage=self._secret_key.encode(), inner_solver=P2pkhSolver(privk=PrivateKey.unhexlify( hexa=Wallet(network=network).from_root_xprivate_key( root_xprivate_key=self._root_xprivate_key).from_path( path=self._path).private_key()))))
def solve(self, network: str = config["network"]) -> P2pkhSolver: if self._path is None: self._path = config["bip44_path"].format( account=self._account, change=(1 if self._change else 0), address=self._address) return P2pkhSolver(privk=PrivateKey.unhexlify(hexa=Wallet( network=network).from_root_xprivate_key( xprivate_key=self._xprivate_key, strict=self._strict).from_path(path=self._path).private_key()))
def solve(self, network: str = config["network"]) -> IfElseSolver: if self._path is None: self._path = config["bip44_path"].format( account=self._account, change=(1 if self._change else 0), address=self._address) return IfElseSolver( branch=Branch.IF, inner_solver=HashlockSolver( preimage=self._secret_key.encode(), inner_solver=P2pkhSolver(privk=PrivateKey.unhexlify( hexa=Wallet(network=network).from_root_xprivate_key( xprivate_key=self._xprivate_key, strict=self._strict). from_path(path=self._path).private_key()))))
def solve(self, network: str = config["network"]) -> IfElseSolver: if self._path is None: self._path = config["bip44_path"].format( account=self._account, change=(1 if self._change else 0), address=self._address) return IfElseSolver( branch=Branch.ELSE, inner_solver=AbsoluteTimelockSolver( locktime=Locktime(n=self._endtime), inner_solver=P2pkhSolver(privk=PrivateKey.unhexlify( hexa=Wallet(network=network).from_root_xprivate_key( xprivate_key=self._xprivate_key, strict=self._strict). from_path(path=self._path).private_key()))))
def solve(self): return IfElseSolver( Branch.ELSE, RelativeTimelockSolver(Sequence(self.sequence), P2pkhSolver(self.private_key)))
def solve(self): return IfElseSolver( Branch.IF, HashlockSolver(self.secret, P2pkhSolver(self.private_key)))
def solve(self): return P2pkhSolver(self.private_key)
def sign_transaction(self, txins: Union[TxOut], tx: MutableTransaction) -> MutableTransaction: '''sign the parent txn outputs P2PKH''' solver = P2pkhSolver(self._private_key) return tx.spend(txins, [solver for i in txins])
version=2, ins=[ TxIn(txid=to_spend.txid, txout=0, script_sig=ScriptSig.empty(), sequence=Sequence.max()) ], outs=[TxOut(value=penalty - mining_fee, n=0, script_pubkey=script)], locktime=Locktime(0)) # 输入脚本 # Relative - HeightBased else_solver = IfElseSolver( Branch.ELSE, # Branch selection RelativeTimelockSolver(HeightBasedSequence(0x00000002), P2pkhSolver(privk))) # Relative - TimeBased # else_solver = IfElseSolver(Branch.ELSE, # Branch selection # RelativeTimelockSolver(TimeBasedSequence.from_timedelta(datetime.timedelta(minutes=5)), # P2pkhSolver(privk))) # 修改交易 signed = unsigned.spend([to_spend.outs[0]], [else_solver]) # 广播交易 print('pay_desposit_hex:', signed.hexlify()) msg = broadcast_raw_tx(signed.hexlify()) format_output(msg)
def solve(self): return IfElseSolver(branch=Branch.ELSE, inner_solver=RelativeTimelockSolver( sequence=Sequence(self.sequence), inner_solver=P2pkhSolver(self.private_key)))
def solve(self): return IfElseSolver(branch=Branch.IF, inner_solver=HashlockSolver( preimage=self.secret.encode(), inner_solver=P2pkhSolver(self.private_key)))
def sign_transaction(self, txin: TxOut, tx: MutableTransaction) -> MutableTransaction: '''sign the parent txn outputs P2PKH''' solver = P2pkhSolver(self._private_key) return tx.spend([txin], [solver])
def solve(self, network: str = config["network"]) -> P2pkhSolver: return P2pkhSolver(privk=PrivateKey.unhexlify(hexa=Wallet( network=network).from_root_xprivate_key( root_xprivate_key=self._root_xprivate_key).from_path( path=self._path).private_key()))
from utils import * # global setup('testnet', strict=True) coin_symbol = 'btc-testnet' api_key = 'fe4a832ab7d14936b5731aa79cfa58ae' # committer pubk_hex = '0380557a219119218f7830bf3cdb2bb3c8220cac15db97e255498fb992e68c04a9' privk_hex = '385acd25450e50ecd5ad0fffec7b871c8f75eb3ba9ecded8d35a0765f4763d7e' pubk = PublicKey.unhexlify(pubk_hex) privk = PrivateKey.unhexlify(privk_hex) # 创建输入脚本 secret = 'I have an apple' # 需要展示的秘密 p2pkh_solver = P2pkhSolver(privk) hasklock_solver = HashlockSolver(secret.encode(), p2pkh_solver) if_solver = IfElseSolver( Branch.IF, # branch selection hasklock_solver) # 创建输出脚本 script = P2pkhScript(pubk) # 获取commit交易 to_spend_hash = "5a98103afa86f00c54ef8cb971ec5b3ad03404e646c46f006efd654bd46d4073" to_spend_raw = get_raw_tx(to_spend_hash, coin_symbol) to_spend = TransactionFactory.unhexlify(to_spend_raw) # 获取罚金数额 penalty = int(float(to_spend.to_json()['vout'][0]['value']) * (10**8))