Skip to content

Commit

Permalink
add code to test partial blinding functionality to qa/rpc-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dgpv committed Feb 18, 2019
1 parent 60c9eee commit 6763ee3
Showing 1 changed file with 116 additions and 7 deletions.
123 changes: 116 additions & 7 deletions qa/rpc-tests/confidential_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

import io

from test_framework.test_framework import BitcoinTestFramework
from test_framework.mininode import *
from test_framework.util import *

class CTTest (BitcoinTestFramework):
Expand Down Expand Up @@ -307,7 +310,8 @@ def run_test(self):
assert_equal(multi_asset_amount['bitcoin'], value1 + value3 )
assert_equal(multi_asset_amount[test_asset], Decimal('0.00000003'))

# Check blinded multisig functionality
# Check blinded multisig functionality and partial blinding functionality

# Get two pubkeys
blinded_addr = self.nodes[0].getnewaddress()
pubkey = self.nodes[0].validateaddress(blinded_addr)["pubkey"]
Expand All @@ -325,16 +329,121 @@ def run_test(self):
# Create blinded address from p2sh address and import corresponding privkey
blinded_multisig_addr = self.nodes[0].createblindedaddress(unconfidential_addr, blinding_pubkey)
self.nodes[0].importblindingkey(blinded_multisig_addr, blinding_key)
self.nodes[1].importblindingkey(blinded_multisig_addr, blinding_key)
# Send coins to blinded multisig address and check that they were received
self.nodes[2].sendtoaddress(blinded_multisig_addr, 1)
# Issue new asset to check that partial blinding works with different assets in one transaction
issued3 = self.nodes[2].issueasset(1, 0)
self.nodes[2].generate(1)
self.sync_all()
assert_equal(len(self.nodes[0].listunspent(0, 0, [unconfidential_addr])), 1)
assert_equal(len(self.nodes[1].listunspent(0, 0, [unconfidential_addr])), 1)
node2_balance = self.nodes[2].getbalance()
assert(issued3['asset'] in node2_balance)
assert_equal(node2_balance[issued3['asset']], Decimal(1))
# Send asset to blinded multisig address and check that they were received
self.nodes[2].sendtoaddress(blinded_multisig_addr, 1, "", "", False, issued3['asset'])
self.sync_all()
unspent_asset = self.nodes[0].listunspent(0, 0, [unconfidential_addr], True, issued3['asset'])
assert_equal(len(unspent_asset), 1)
assert(issued3['asset'] not in self.nodes[2].getbalance())

self.nodes[0].generate(1)
blinded_addr = self.nodes[0].getnewaddress()
blinded_addr2 = self.nodes[1].getnewaddress()
dst_addr = self.nodes[0].getnewaddress()
dst_addr2 = self.nodes[1].getnewaddress()
dst_addr3 = self.nodes[2].getnewaddress()
addr = self.nodes[0].validateaddress(blinded_addr)["unconfidential"]
addr2 = self.nodes[1].validateaddress(blinded_addr2)["unconfidential"]
self.nodes[0].sendtoaddress(blinded_addr, 0.1)
self.nodes[1].sendtoaddress(blinded_addr2, 0.11)

unspent = self.nodes[0].listunspent(0, 0, [addr])
unspent2 = self.nodes[1].listunspent(0, 0, [addr2])
assert_equal(len(unspent), 1)
assert_equal(len(unspent2), 1)

# Create one part of the transaction to partially blind
rawtx = self.nodes[0].createrawtransaction(
[{"txid": unspent2[0]["txid"], "vout": unspent2[0]["vout"]}], {dst_addr2: Decimal("0.01")})

# Create another part of the transaction to partially blind
rawtx2 = self.nodes[0].createrawtransaction(
[{"txid": unspent[0]["txid"], "vout": unspent[0]["vout"]},
{"txid": unspent_asset[0]["txid"], "vout": unspent_asset[0]["vout"]}],
{dst_addr: Decimal("0.1"), dst_addr3: Decimal("1.0")},
0,
{dst_addr: unspent[0]['asset'], dst_addr3: unspent_asset[0]['asset']})

sum_i = unspent2[0]["amount"] + unspent[0]["amount"]
sum_o = 0.01 + 0.10 + 0.1
assert_equal(int(round(sum_i*COIN)), int(round(sum_o*COIN)))

# Blind the first part of the transaction - we need to supply the
# assetcommmitments for all of the inputs, for the surjectionproof
# to be valid after we combine the transactions
blindtx = self.nodes[1].blindrawtransaction(
rawtx, True, [
unspent2[0]['assetcommitment'],
unspent[0]['assetcommitment'],
unspent_asset[0]['assetcommitment']
])

# Combine the transactions
ubtx = CTransaction()
ubtx.deserialize(io.BytesIO(hex_str_to_bytes(rawtx2)))
btx = CTransaction()
btx.deserialize(io.BytesIO(hex_str_to_bytes(blindtx)))
btx.vin.append(ubtx.vin[0])
btx.wit.vtxinwit.append(CTxInWitness())
btx.vout.append(ubtx.vout[0])
btx.wit.vtxoutwit.append(CTxOutWitness())
btx.vin.append(ubtx.vin[1])
btx.wit.vtxinwit.append(CTxInWitness())
btx.vout.append(ubtx.vout[1])
btx.wit.vtxoutwit.append(CTxOutWitness())
btx.vout.append(CTxOut(nValue=CTxOutValue(10000000),
nAsset=CTxOutAsset(BITCOIN_ASSET_OUT)))
btx.wit.vtxoutwit.append(CTxOutWitness())

# Input 0 is already blinded
# Input 2 is our new asset
# Input 3 is fee that we added above

# Blind with wrong order of assetcommitments - such transaction should be rejected
blindtx = self.nodes[0].blindrawtransaction(
bytes_to_hex_str(btx.serialize()), True, [
unspent_asset[0]['assetcommitment'],
unspent[0]['assetcommitment'],
unspent2[0]['assetcommitment']
])

stx2 = self.nodes[1].signrawtransaction(blindtx)
stx = self.nodes[0].signrawtransaction(stx2['hex'])
self.sync_all()
try:
self.nodes[2].sendrawtransaction(stx['hex'])
raise AssertionError(
"Shouldn't be able to send a transaction that was blinded "
"with incorrectly ordered assetcommitments")
except JSONRPCException:
pass

# Blind with correct order of assetcommitments
blindtx = self.nodes[0].blindrawtransaction(
bytes_to_hex_str(btx.serialize()), True, [
unspent2[0]['assetcommitment'],
unspent[0]['assetcommitment'],
unspent_asset[0]['assetcommitment']
])

stx2 = self.nodes[1].signrawtransaction(blindtx)
stx = self.nodes[0].signrawtransaction(stx2['hex'])
self.nodes[2].sendrawtransaction(stx['hex'])
self.sync_all()

unconfidential_dst_addr3 = self.nodes[2].validateaddress(dst_addr3)["unconfidential"]
unspent_asset2 = self.nodes[2].listunspent(0, 0, [unconfidential_dst_addr3], True, issued3['asset'])
assert_equal(len(unspent_asset2), 1)
assert_equal(unspent_asset2[0]['amount'], Decimal(1))
self.nodes[2].generate(1)
assert_equal(self.nodes[2].getbalance()[issued3['asset']], Decimal(1))

# Basic checks of rawblindrawtransaction functionality
blinded_addr = self.nodes[0].getnewaddress()
addr = self.nodes[0].validateaddress(blinded_addr)["unconfidential"]
Expand Down

0 comments on commit 6763ee3

Please sign in to comment.