from cfd.key import SignParameter, SigHashType def test_script_func(obj, name, case, req, exp, error): try: if name == 'Script.Parse': resp = Script(req['script']) elif name == 'Script.Create': resp = Script.from_asm(req['items']) elif name == 'Script.CreateMultisigScriptSig': _sign_list = [] for data in req.get('signParams', []): sighash_type = SigHashType.get( data.get('sighashType', 'all'), data.get('sighashAnyoneCanPay', False)) _sign = SignParameter(data['hex'], data.get('relatedPubkey', ''), sighash_type) if data.get('derEncode', False): _sign.set_der_encode() _sign_list.append(_sign) resp = Script.create_multisig_scriptsig(req['redeemScript'], _sign_list) else: raise Exception('unknown name: ' + name) assert_error(obj, name, case, error) assert_equal(obj, name, case, exp, str(resp), 'hex')
def test_ct_transaction_func2(obj, name, case, req, exp, error): try: def get_tx(): resp, txin = None, None if 'tx' in req: resp = ConfidentialTransaction.from_hex(req['tx']) if 'txin' in req: txin = req['txin'] return resp, txin if name == 'ConfidentialTransaction.SignWithPrivkey': resp, txin = get_tx() _sighashtype = SigHashType.get( txin.get('sighashType', 'all'), txin.get('sighashAnyoneCanPay', False)) resp.sign_with_privkey( OutPoint(txin['txid'], txin['vout']), txin['hashType'], txin['privkey'], value=txin.get('confidentialValueCommitment', txin.get('amount', 0)), sighashtype=_sighashtype) elif name == 'ConfidentialTransaction.AddSign': resp, txin = get_tx() hash_type = HashType.P2SH if txin.get('isWitness', True): hash_type = HashType.P2WSH for param in txin.get('signParam', []): _sighashtype = SigHashType.get(
assert_equal(obj, name, case, exp, err.message) return True def test_transaction_func2(obj, name, case, req, exp, error): try: resp = None txin = {} if 'tx' in req: resp = Transaction.from_hex(req['tx']) if 'txin' in req: txin = req['txin'] if name == 'Transaction.SignWithPrivkey': utxos = load_utxo_list(req) _sighashtype = SigHashType.get( txin.get('sighashType', 'all'), anyone_can_pay=txin.get('sighashAnyoneCanPay', False), is_rangeproof=txin.get('sighashRangeproof', False)) resp.sign_with_privkey(OutPoint(txin['txid'], txin['vout']), txin['hashType'], txin['privkey'], amount=txin.get('amount', 0), sighashtype=_sighashtype, grind_r=txin.get('isGrindR', True), utxos=utxos, aux_rand=txin.get('auxRand', None), annex=txin.get('annex', None)) elif name == 'Transaction.AddSign': hash_type = HashType.P2SH if txin.get('isWitness', True): hash_type = HashType.P2WSH for param in txin.get('signParams', []):
assert_equal(obj, name, case, exp, str(resp), 'hex') elif isinstance(resp, bool): assert_equal(obj, name, case, exp, resp, 'bool') else: assert_equal(obj, name, case, exp, resp, 'hex') except CfdError as err: if not error: raise err assert_equal(obj, name, case, exp, err.message) def test_signature_func(obj, name, case, req, exp, error): try: if name == 'Signature.EncodeByDer': sighash_type = SigHashType.get( req['sighashType'], req.get('sighashAnyoneCanPay', False)) resp = SignParameter.encode_by_der(req['signature'], sighash_type) elif name == 'Signature.DecodeDerToRaw': resp = SignParameter.decode_from_der(req['signature']) elif name == 'Signature.Normalize': resp = SignParameter.normalize(req['signature']) else: raise Exception('unknown name: ' + name) assert_error(obj, name, case, error) assert_equal(obj, name, case, exp, str(resp), 'signature') if isinstance(resp, SignParameter): assert_equal(obj, name, case, exp, resp.sighashtype.get_type_object().as_str(), 'sighashType') assert_equal(obj, name, case, exp,
utxo = TxOut(0) if utxo is None else utxo psbt.set_input_utxo(outpoint, utxo, full_tx) for bip32_data in input.get('bip32Derives', []): if 'descriptor' in bip32_data: psbt.set_input_bip32_key( outpoint, pubkey=bip32_data['descriptor']) else: psbt.set_input_bip32_key( outpoint, key_data=KeyData(Pubkey(bip32_data['pubkey']), fingerprint=ByteData( bip32_data['master_fingerprint']), bip32_path=bip32_data['path'])) if 'sighash' in input: psbt.set_input_sighash_type( outpoint, SigHashType.get(input['sighash'])) for sig_data in input.get('partialSignature', []): psbt.set_input_signature( outpoint, sig_data['pubkey'], sig_data['signature']) for record in input.get('unknown', []): psbt.set_input_record( outpoint, record['key'], record['value']) for output_data in req.get('outputs', []): output = output_data['output'] index = output_data.get('index', 0) if 'redeemScript' in output: psbt.set_output_script(index, output['redeemScript']) for bip32_data in output.get('bip32Derives', []): if 'descriptor' in bip32_data: psbt.set_output_bip32_key( index, pubkey=bip32_data['descriptor'])
elif isinstance(resp, bool): assert_equal(obj, name, case, exp, resp, 'bool') else: assert_equal(obj, name, case, exp, resp, 'hex') except CfdError as err: if not error: raise err assert_equal(obj, name, case, exp, err.message) def test_signature_func(obj, name, case, req, exp, error): try: if name == 'Signature.EncodeByDer': sighash_type = SigHashType.get( req['sighashType'], anyone_can_pay=req.get('sighashAnyoneCanPay', False), is_rangeproof=req.get('sighashRangeproof', False)) resp = SignParameter.encode_by_der(req['signature'], sighash_type) elif name == 'Signature.DecodeDerToRaw': resp = SignParameter.decode_from_der(req['signature']) elif name == 'Signature.Normalize': resp = SignParameter.normalize(req['signature']) else: raise Exception('unknown name: ' + name) assert_error(obj, name, case, error) assert_equal(obj, name, case, exp, str(resp), 'signature') if isinstance(resp, SignParameter): assert_equal(obj, name, case, exp, resp.sighashtype.get_type_object().as_str(), 'sighashType')
from cfd.key import SignParameter, SigHashType def test_script_func(obj, name, case, req, exp, error): try: if name == 'Script.Parse': resp = Script(req['script']) elif name == 'Script.Create': resp = Script.from_asm(req['items']) elif name == 'Script.CreateMultisigScriptSig': _sign_list = [] for data in req.get('signParams', []): sighash_type = SigHashType.get( data.get('sighashType', 'all'), anyone_can_pay=data.get('sighashAnyoneCanPay', False), is_rangeproof=data.get('sighashRangeproof', False)) _sign = SignParameter(data['hex'], data.get('relatedPubkey', ''), sighash_type) if data.get('derEncode', False): _sign.set_der_encode() _sign_list.append(_sign) resp = Script.create_multisig_scriptsig(req['redeemScript'], _sign_list) else: raise Exception('unknown name: ' + name) assert_error(obj, name, case, error) assert_equal(obj, name, case, exp, str(resp), 'hex')