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 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"]) -> 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, 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): 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 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)))
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)) # 估算挖矿费用 print('estimating mining fee...') mining_fee_per_kb = get_mining_fee_per_kb(coin_symbol,
unsigned = MutableTransaction( 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)