コード例 #1
0
def test_verify_scenario_2():
    context = scryptlib.utils.create_dummy_input_context()
    context.utxo.script_pubkey = escrow.locking_script
    context.utxo.value = input_sats

    change_out = TxOutput(int(input_sats - fee),
                          P2PKH_Address(pkh_A, Bitcoin).to_script())
    context.tx.outputs.append(change_out)

    sighash_flag = SigHash(SigHash.ALL | SigHash.FORKID)
    sighash = context.tx.signature_hash(0, input_sats, escrow.locking_script,
                                        sighash_flag)

    sig_A = key_priv_A.sign(sighash, hasher=None)
    sig_A = sig_A + pack_byte(sighash_flag)

    sig_E = key_priv_E.sign(sighash, hasher=None)
    sig_E = sig_E + pack_byte(sighash_flag)

    preimage = scryptlib.utils.get_preimage_from_input_context(
        context, sighash_flag)

    verify_result = escrow.unlock(SigHashPreimage(preimage), PubKey(key_pub_A),
                                  Sig(sig_A), PubKey(key_pub_E), Sig(sig_E),
                                  Bytes(secret0)).verify(context)
    assert verify_result == True

    # Wrong secret
    with pytest.raises(bitcoinx.VerifyFailed):
        verify_result = escrow.unlock(SigHashPreimage(preimage),
                                      PubKey(key_pub_A), Sig(sig_A),
                                      PubKey(key_pub_E), Sig(sig_E),
                                      Bytes(secret1)).verify(context)
コード例 #2
0
def test_verify_correct():
    verify_result = token.transfer(PubKey(key_pub_0), 
            Sig(sig),
            PubKey(key_pub_1),
            40,
            SigHashPreimage(preimage),
            222222).verify(context)
    assert verify_result == True
コード例 #3
0
def test_verify_not_first_call():
    token.first_call = False

    with pytest.raises(bitcoinx.errors.NullFailError):
        token.transfer(PubKey(key_pub_0), 
            Sig(sig),
            PubKey(key_pub_1),
            40,
            SigHashPreimage(preimage),
            222222).verify(context)
コード例 #4
0
def test_verify_wrong_val_2():
    token.first_call = True

    verify_result = token.transfer(PubKey(key_pub_0), 
            Sig(sig),
            PubKey(key_pub_1),
            43,
            SigHashPreimage(preimage),
            222222).verify(context)
    assert verify_result == False
コード例 #5
0
    def test_issue(priv_key,
                   receiver,
                   new_issuer=issuer,
                   next_tok_id=curr_token_id + 1,
                   issued_tok_id=curr_token_id):
        context = scryptlib.utils.create_dummy_input_context()
        context.utxo.script_pubkey = token.locking_script
        context.utxo.value = input_sats

        new_data_part = b'\x23' + scryptlib.utils.get_push_int(next_tok_id)[1:] + \
                        new_issuer.to_bytes() + action_issue
        new_locking_script = Script(token.code_part.to_bytes() + new_data_part)
        tx_out = TxOutput(value=out_sats, script_pubkey=new_locking_script)
        context.tx.outputs.append(tx_out)

        new_data_part = b'\x23' + scryptlib.utils.get_push_int(issued_tok_id)[1:] + \
                        receiver.to_bytes() + action_transfer
        new_locking_script = Script(token.code_part.to_bytes() + new_data_part)
        tx_out = TxOutput(value=out_sats, script_pubkey=new_locking_script)
        context.tx.outputs.append(tx_out)

        sighash_flag = SigHash(SigHash.ALL | SigHash.FORKID)
        sighash = context.tx.signature_hash(0, input_sats,
                                            token.locking_script, sighash_flag)
        sig = priv_key.sign(sighash, hasher=None)
        sig = sig + pack_byte(sighash_flag)

        preimage = scryptlib.utils.get_preimage_from_input_context(
            context, sighash_flag)

        return token.issue(Sig(sig), PubKey(receiver), out_sats, out_sats,
                           SigHashPreimage(preimage)).verify(context)
コード例 #6
0
    def test_finish(key_priv, pkh_B, action_A, action_B, total_sats,
                    input_sats, out_sats, change_sats):
        rps.set_data_part(player_A_data + pkh_B +
                          scryptlib.utils.get_push_int(action_B)[1:])

        context = scryptlib.utils.create_dummy_input_context()
        context.utxo.script_pubkey = rps.locking_script
        context.utxo.value = total_sats

        change_out = TxOutput(change_sats,
                              P2PKH_Address(pkh_A, Bitcoin).to_script())
        context.tx.outputs.append(change_out)

        if out_sats > 0:
            pay_out = TxOutput(out_sats,
                               P2PKH_Address(pkh_B, Bitcoin).to_script())
            context.tx.outputs.append(pay_out)

        preimage = scryptlib.utils.get_preimage_from_input_context(
            context, sighash_flag)

        input_idx = 0
        utxo_satoshis = context.utxo.value
        sighash = context.tx.signature_hash(input_idx, utxo_satoshis,
                                            rps.locking_script, sighash_flag)
        sig = key_priv.sign(sighash, hasher=None)
        sig = sig + pack_byte(sighash_flag)

        return rps.finish(SigHashPreimage(preimage), action_A, Sig(sig),
                          PubKey(key_pub_A), change_sats).verify(context)
コード例 #7
0
def test_verify_wrong_key():
    MultiSig = scryptlib.contract.build_contract_class(desc)
    multisig = MultiSig([Ripemd160(pkh_0), Ripemd160(pkh_1), Ripemd160(pkh_2)])

    context = scryptlib.utils.create_dummy_input_context()
    sighash = context.tx.signature_hash(input_idx, context.utxo.value,
                                        multisig.locking_script, sighash_flag)

    sig_0 = key_priv_0.sign(sighash, hasher=None) + pack_byte(sighash_flag)
    sig_1 = key_priv_1.sign(sighash, hasher=None) + pack_byte(sighash_flag)
    sig_2 = key_priv_1.sign(sighash, hasher=None) + pack_byte(sighash_flag)

    with pytest.raises(bitcoinx.NullFailError):
        verify_result = multisig.unlock(
            [PubKey(key_pub_0),
             PubKey(key_pub_2),
             PubKey(key_pub_2)],
            [Sig(sig_0), Sig(sig_1), Sig(sig_2)]).verify(context)
コード例 #8
0
def test_verify_correct():
    MultiSig = scryptlib.contract.build_contract_class(desc)
    multisig = MultiSig([Ripemd160(pkh_0), Ripemd160(pkh_1), Ripemd160(pkh_2)])

    context = scryptlib.utils.create_dummy_input_context()
    sighash = context.tx.signature_hash(input_idx, context.utxo.value,
                                        multisig.locking_script, sighash_flag)

    sig_0 = key_priv_0.sign(sighash, hasher=None) + pack_byte(sighash_flag)
    sig_1 = key_priv_1.sign(sighash, hasher=None) + pack_byte(sighash_flag)
    sig_2 = key_priv_2.sign(sighash, hasher=None) + pack_byte(sighash_flag)

    verify_result = multisig.unlock(
        [PubKey(key_pub_0),
         PubKey(key_pub_1),
         PubKey(key_pub_2)],
        [Sig(sig_0), Sig(sig_1), Sig(sig_2)]).verify(context)
    assert verify_result == True
コード例 #9
0
def test_verify_p2pkh_correct():
    context = scryptlib.utils.create_dummy_input_context()
    sighash_flag = SigHash(SigHash.ALL | SigHash.FORKID)
    sighash = context.tx.signature_hash(0, context.utxo.value,
                                        asm.locking_script, sighash_flag)
    sig = key_priv.sign(sighash, hasher=None)
    sig = sig + pack_byte(sighash_flag)
    verify_result = asm.p2pkh(Sig(sig), PubKey(key_pub)).verify(context)
    assert verify_result == True
コード例 #10
0
    def test_split(key_priv, balance0, balance1, balance_input0=None, balance_input1=None):
        if not balance_input0:
            balance_input0 = balance0
        if not balance_input1:
            balance_input1 = balance1

        context = scryptlib.utils.create_dummy_input_context()
        context.utxo.script_pubkey = token.locking_script
        context.utxo.value = in_sats
            
        new_locking_script = Script(token.code_part.to_bytes() + b'\x23' +
                key_pub_1.to_bytes() + b'\x00' + scryptlib.utils.get_push_int(balance0)[1:])
        tx_out = TxOutput(value=out_sats, script_pubkey=new_locking_script)
        context.tx.outputs.append(tx_out)

        if balance1 > 0:
            new_locking_script = Script(token.code_part.to_bytes() + b'\x23' + 
                    key_pub_2.to_bytes() + b'\x00' + scryptlib.utils.get_push_int(balance1)[1:])
            tx_out = TxOutput(value=out_sats, script_pubkey=new_locking_script)
            context.tx.outputs.append(tx_out)

        sighash_flag = SigHash(SigHash.ALL | SigHash.FORKID)
        preimage = scryptlib.utils.get_preimage_from_input_context(context, sighash_flag)

        input_idx = 0
        utxo_satoshis = context.utxo.value
        sighash = context.tx.signature_hash(input_idx, utxo_satoshis, token.locking_script, sighash_flag)
        sig = key_priv.sign(sighash, hasher=None)
        sig = sig + pack_byte(sighash_flag)

        return token.split(
                    Sig(sig),
                    PubKey(key_pub_1),
                    balance_input0,
                    out_sats,
                    PubKey(key_pub_2),
                    balance_input1,
                    out_sats,
                    SigHashPreimage(preimage)
                ).verify(context)
コード例 #11
0
    def test_merge(input_idx, balance0, balance1):
        context = scryptlib.utils.create_dummy_input_context()
        context.utxo.value = in_sats
        context.input_index = input_idx

        tx_in = TxInput(context.tx.inputs[0].prev_hash, 1, Script(), 0xffffffff)
        context.tx.inputs.append(tx_in)

        prev_txid = context.tx.inputs[0].prev_hash
        prevouts = prev_txid + b'\x00\x00\x00\x00' + prev_txid + b'\x01\x00\x00\x00'

        new_locking_script = Script(token.code_part.to_bytes() + b'\x23' +
                key_pub_2.to_bytes() + scryptlib.utils.get_push_int(balance0)[1:] + scryptlib.utils.get_push_int(balance1)[1:])
        tx_out = TxOutput(value=out_sats, script_pubkey=new_locking_script)
        context.tx.outputs.append(tx_out)

        if input_idx == 0:
            balance = balance1
            context.utxo.script_pubkey = locking_script_0
            key_to_sign = key_priv_0
            token.set_data_part(b'\x23' + data_part_0)
        else:
            balance = balance0
            context.utxo.script_pubkey = locking_script_1
            key_to_sign = key_priv_1
            token.set_data_part(b'\x23' + data_part_1)

        sighash_flag = SigHash(SigHash.ALL | SigHash.FORKID)
        #preimage = scryptlib.utils.get_preimage_from_input_context(context, sighash_flag)
        if input_idx == 0:
            preimage = scryptlib.utils.get_preimage(context.tx, input_idx, in_sats, locking_script_0, sighash_flag=sighash_flag)
        else:
            preimage = scryptlib.utils.get_preimage(context.tx, input_idx, in_sats, locking_script_1, sighash_flag=sighash_flag)

        if input_idx == 0:
            sighash = context.tx.signature_hash(input_idx, in_sats, locking_script_0, sighash_flag)
        else:
            sighash = context.tx.signature_hash(input_idx, in_sats, locking_script_1, sighash_flag)
        sig = key_to_sign.sign(sighash, hasher=None)
        sig = sig + pack_byte(sighash_flag)


        return token.merge(
                    Sig(sig),
                    PubKey(key_pub_2),
                    Bytes(prevouts),
                    balance,
                    out_sats,
                    SigHashPreimage(preimage)
                ).verify(context)
コード例 #12
0
def test_verify_wrong():
    sig = wrong_key_priv.sign(sighash, hasher=None)
    sig = sig + pack_byte(sighash_flag)
    with pytest.raises(bitcoinx.VerifyFailed):
        p2pkh_obj.unlock(Sig(sig), PubKey(wrong_key_pub)).verify(context)
コード例 #13
0
def test_verify_correct_key():
    sig = key_priv.sign(sighash, hasher=None)
    sig = sig + pack_byte(sighash_flag)
    verify_result = p2pkh_obj.unlock(Sig(sig), PubKey(key_pub)).verify(context)
    assert verify_result == True
コード例 #14
0
def test_verify_correct():
    preimage = get_preimage_after_purchase(key_pub)

    verify_result = token_sale.buy(PubKey(key_pub), n_tokens,
                                   SigHashPreimage(preimage)).verify(context)
    assert verify_result == True
コード例 #15
0
def test_verify_correct():
    verify_result = r_puzzle.unlock(Sig(sig), PubKey(key_pub_R),
                                    Sig(sig_r)).verify(context)
    assert verify_result == True
コード例 #16
0
key_priv_0 = PrivateKey.from_arbitrary_bytes(b'test123')
key_pub_0 = key_priv_0.public_key
key_priv_1 = PrivateKey.from_arbitrary_bytes(b'123test')
key_pub_1 = key_priv_1.public_key

contract = './test/res/token.scrypt'

compiler_result = scryptlib.utils.compile_contract(contract)
desc = compiler_result.to_desc()

Token = scryptlib.contract.build_contract_class(desc)
type_classes = scryptlib.contract.build_type_classes(desc)
Account = type_classes['Account']

accounts = [Account({
                'pubKey': PubKey(key_pub_0),
                'balance': 100
                }),
            Account({
                'pubKey': PubKey(key_pub_1),
                'balance': 0
                })]
token = Token(accounts)

# Create context and set prev locking script. 
context = scryptlib.utils.create_dummy_input_context()
context.utxo.script_pubkey = token.locking_script

# Create new locking script.
new_accounts = [Account({
                'pubKey': PubKey(key_pub_0),