def build_transactions(self): assert self.role == Role.SELLER and self.stage == Stage.LOCK, "Incorrect stage" # Check output range proof output = Output(OutputFeatures.DEFAULT_OUTPUT, self.commit, self.range_proof) assert output.verify(self.secp), "Invalid bulletproof" # Build refund tx refund_input = Input(OutputFeatures.DEFAULT_OUTPUT, self.commit) public_refund_nonce_sum = PublicKey.from_combination( self.secp, [self.public_refund_nonce, self.foreign_public_refund_nonce]) refund_signature = aggsig.add_partials(self.secp, [ self.partial_refund_signature, self.foreign_partial_refund_signature ], public_refund_nonce_sum) assert aggsig.verify( self.secp, refund_signature, self.public_refund_excess, self.refund_fee_amount, self.refund_lock_height), "Unable to verify refund signature" refund_kernel = Kernel(0, self.refund_fee_amount, self.refund_lock_height, None, None) self.refund_tx = Transaction([refund_input], [self.refund_output], [refund_kernel], self.refund_offset) refund_kernel.excess = self.refund_tx.sum_commitments(self.secp) refund_kernel.excess_signature = refund_signature assert self.refund_tx.verify_kernels( self.secp), "Unable to verify refund kernel" # Build multisig tx public_nonce_sum = PublicKey.from_combination( self.secp, [self.public_nonce, self.foreign_public_nonce]) signature = aggsig.add_partials( self.secp, [self.partial_signature, self.foreign_partial_signature], public_nonce_sum) assert aggsig.verify(self.secp, signature, self.public_excess, self.fee_amount, self.lock_height), \ "Unable to verify signature" kernel = Kernel(0, self.fee_amount, self.lock_height, None, None) self.tx = Transaction(self.inputs, [self.change_output, output], [kernel], self.offset) kernel.excess = self.tx.sum_commitments(self.secp) kernel.excess_signature = signature assert self.tx.verify_kernels(self.secp), "Unable to verify kernel" self.swap_nonce = SecretKey.random(self.secp) self.public_swap_nonce = self.swap_nonce.to_public_key(self.secp)
def test_sig(): # Test Grin-like signature scheme secp = Secp256k1(None, FLAG_ALL) nonce_a = SecretKey.random(secp) public_nonce_a = nonce_a.to_public_key(secp) nonce_b = SecretKey.random(secp) public_nonce_b = nonce_b.to_public_key(secp) public_nonce_sum = PublicKey.from_combination( secp, [public_nonce_a, public_nonce_b]) excess_a = SecretKey.random(secp) public_excess_a = excess_a.to_public_key(secp) excess_b = SecretKey.random(secp) public_excess_b = excess_b.to_public_key(secp) public_excess_sum = PublicKey.from_combination( secp, [public_excess_a, public_excess_b]) fee = randint(1, 999999) lock_height = randint(1, 999999) # Partial signature for A sig_a = aggsig.calculate_partial(secp, excess_a, nonce_a, public_excess_sum, public_nonce_sum, fee, lock_height) assert aggsig.verify_partial(secp, sig_a, public_excess_a, public_excess_sum, public_nonce_sum, fee, lock_height) # Partial signature for B sig_b = aggsig.calculate_partial(secp, excess_b, nonce_b, public_excess_sum, public_nonce_sum, fee, lock_height) assert aggsig.verify_partial(secp, sig_b, public_excess_b, public_excess_sum, public_nonce_sum, fee, lock_height) # Total signature sig = aggsig.add_partials(secp, [sig_a, sig_b], public_nonce_sum) assert aggsig.verify(secp, sig, public_excess_sum, fee, lock_height) rnd_sig = Signature(bytearray(urandom(64))) assert not aggsig.verify(secp, rnd_sig, public_excess_sum, fee, lock_height) public_rnd = SecretKey.random(secp).to_public_key(secp) assert not aggsig.verify(secp, sig, public_rnd, fee, lock_height) assert not aggsig.verify(secp, sig, public_excess_sum, 0, lock_height) assert not aggsig.verify(secp, sig, public_excess_sum, fee, 0)
def finalize_signature(self, secp: Secp256k1) -> Signature: self.verify_partial_signatures(secp) partial_signatures = [ x.partial_signature for x in self.participant_data ] public_nonce_sum = self.public_nonce_sum(secp) public_key_sum = self.public_blind_excess_sum(secp) signature = aggsig.add_partials(secp, partial_signatures, public_nonce_sum) if not aggsig.verify(secp, signature, public_key_sum, self.fee, self.lock_height): raise InvalidSignatureException() return signature
def finalize_swap(self): seller = self.role == Role.SELLER assert (seller and self.stage == Stage.DONE) or ( not seller and self.stage == Stage.SWAP), "Incorrect stage" if seller: self.secret_lock = self.foreign_partial_swap_adaptor.scalar( self.secp).add( self.secp, self.foreign_partial_swap_signature.scalar( self.secp).negate(self.secp)) public_lock = self.secret_lock.to_public_key(self.secp) assert self.public_lock == public_lock, "Invalid secret lock, this should never happen" if self.is_ether_swap(): self.claim = self.secp.sign_recoverable( self.secret_lock, bytearray([0] * 32)) else: swap_input = Input(OutputFeatures.DEFAULT_OUTPUT, self.commit) public_swap_nonce_sum = PublicKey.from_combination( self.secp, [self.public_swap_nonce, self.foreign_public_swap_nonce]) swap_signature = aggsig.add_partials(self.secp, [ self.partial_swap_signature, self.foreign_partial_swap_signature ], public_swap_nonce_sum) assert aggsig.verify( self.secp, swap_signature, self.public_swap_excess, self.swap_fee_amount, self.swap_lock_height), "Unable to verify swap signature" swap_kernel = Kernel(0, self.swap_fee_amount, self.swap_lock_height, None, None) self.swap_tx = Transaction([swap_input], [self.swap_output], [swap_kernel], self.swap_offset) swap_kernel.excess = self.swap_tx.sum_commitments(self.secp) swap_kernel.excess_signature = swap_signature assert self.swap_tx.verify_kernels( self.secp), "Unable to verify swap kernel"
def send(node_url: str): global secp, wallet, proof_builder now = int(time()) send_amount = GRIN_UNIT lock_height = 1 refund_lock_height = lock_height + 1440 # ~24 hours dest_url = "http://127.0.0.1:18185" fluff = True secp = Secp256k1(None, FLAG_ALL) wallet = Wallet.open(secp, "wallet_a") print("Preparing to create multisig with {}".format(dest_url)) input_entries = wallet.select_outputs(send_amount + tx_fee(1, 2, MILLI_GRIN_UNIT)) fee_amount = tx_fee(len(input_entries), 2, MILLI_GRIN_UNIT) input_amount = sum(x.value for x in input_entries) change_amount = input_amount - send_amount - fee_amount refund_fee_amount = tx_fee(1, 1, MILLI_GRIN_UNIT) print("Selected {} inputs".format(len(input_entries))) tx = Transaction.empty(secp, 0, fee_amount, lock_height) refund_tx = Transaction.empty(secp, 0, refund_fee_amount, refund_lock_height) blind_sum = BlindSum() # Inputs inputs = [] for entry in input_entries: entry.mark_locked() blind_sum.sub_child_key(wallet.derive_from_entry(entry)) input = wallet.entry_to_input(entry) tx.add_input(secp, input) inputs.append(input) # Change output change_child, change_entry = wallet.create_output(change_amount) blind_sum.add_child_key(change_child) change_output = wallet.entry_to_output(change_entry) tx.add_output(secp, change_output) # Multisig output partial_child, partial_entry = wallet.create_output(send_amount) partial_entry.mark_locked() blind_sum.add_child_key(partial_child) public_partial_commit = wallet.commit_with_child_key(0, partial_child) # Refund output refund_amount = send_amount - refund_fee_amount refund_child, refund_entry = wallet.create_output(refund_amount) refund_output = wallet.entry_to_output(refund_entry) refund_tx.add_output(secp, refund_output) # Offset blind_sum.sub_blinding_factor(tx.offset) # Excess excess = wallet.chain.blind_sum(blind_sum).to_secret_key(secp) public_excess = excess.to_public_key(secp) # Nonce nonce = SecretKey.random(secp) public_nonce = nonce.to_public_key(secp) # Refund nonce refund_nonce = SecretKey.random(secp) refund_public_nonce = refund_nonce.to_public_key(secp) dct = { "amount": send_amount, "fee": fee_amount, "refund_fee": refund_fee_amount, "lock_height": lock_height, "refund_lock_height": refund_lock_height, "public_partial_commit": public_partial_commit.to_hex(secp).decode(), "public_nonce": public_nonce.to_hex(secp).decode(), "refund_public_nonce": refund_public_nonce.to_hex(secp).decode() } f = open("logs/{}_multisig_1.json".format(now), "w") f.write(json.dumps(dct, indent=2)) f.close() print("Sending to receiver..") req = urlopen(dest_url, json.dumps(dct).encode(), 60) dct2 = json.loads(req.read().decode()) f = open("logs/{}_multisig_2.json".format(now), "w") f.write(json.dumps(dct2, indent=2)) f.close() print("Received response, processing..") public_partial_commit_recv = Commitment.from_hex( secp, dct2['public_partial_commit'].encode()) public_partial_recv = public_partial_commit_recv.to_public_key(secp) public_nonce_recv = PublicKey.from_hex(secp, dct2['public_nonce'].encode()) public_excess_recv = public_partial_commit_recv.to_public_key(secp) partial_signature_recv = Signature.from_hex( dct2['partial_signature'].encode()) refund_public_nonce_recv = PublicKey.from_hex( secp, dct2['refund_public_nonce'].encode()) refund_public_excess_recv = PublicKey.from_hex( secp, dct2['refund_public_excess'].encode()) refund_partial_signature_recv = Signature.from_hex( dct2['refund_partial_signature'].encode()) # Commitment commit = secp.commit_sum( [public_partial_commit_recv, wallet.commit(partial_entry)], []) print("Total commit: {}".format(commit)) # Nonce sums public_nonce_sum = PublicKey.from_combination( secp, [public_nonce_recv, public_nonce]) refund_public_nonce_sum = PublicKey.from_combination( secp, [refund_public_nonce_recv, refund_public_nonce]) # Step 2 of bulletproof proof_builder = MultiPartyBulletProof(secp, partial_child, public_partial_recv, send_amount, commit) t_1_recv = PublicKey.from_hex(secp, dct2['t_1'].encode()) t_2_recv = PublicKey.from_hex(secp, dct2['t_2'].encode()) t_1, t_2 = proof_builder.step_1() proof_builder.fill_step_1(t_1_recv, t_2_recv) tau_x = proof_builder.step_2() dct3 = { "t_1": t_1.to_hex(secp).decode(), "t_2": t_2.to_hex(secp).decode(), "tau_x": tau_x.to_hex().decode() } f = open("logs/{}_multisig_3.json".format(now), "w") f.write(json.dumps(dct3, indent=2)) f.close() print("Sending bulletproof component..") req2 = urlopen(dest_url, json.dumps(dct3).encode(), 60) dct4 = json.loads(req2.read().decode()) print("Received response") f = open("logs/{}_multisig_4.json".format(now), "w") f.write(json.dumps(dct4, indent=2)) f.close() # Bulletproof proof = RangeProof.from_bytearray( bytearray(unhexlify(dct4['proof'].encode()))) output = Output(OutputFeatures.DEFAULT_OUTPUT, commit, proof) assert output.verify(secp), "Invalid bulletproof" tx.add_output(secp, output) print("Created bulletproof") # First we finalize the refund tx, and check its validity refund_input = Input(OutputFeatures.DEFAULT_OUTPUT, commit) refund_tx.add_input(secp, refund_input) # Refund excess refund_blind_sum = BlindSum() refund_blind_sum.sub_child_key(partial_child) refund_blind_sum.add_child_key(refund_child) refund_blind_sum.sub_blinding_factor(refund_tx.offset) refund_excess = wallet.chain.blind_sum(refund_blind_sum).to_secret_key( secp) refund_public_excess = refund_excess.to_public_key(secp) # Refund partial signature refund_partial_signature = aggsig.calculate_partial( secp, refund_excess, refund_nonce, refund_public_nonce_sum, refund_fee_amount, refund_lock_height) # Refund final signature refund_public_excess_sum = PublicKey.from_combination( secp, [refund_public_excess_recv, refund_public_excess]) refund_signature = aggsig.add_partials( secp, [refund_partial_signature_recv, refund_partial_signature], refund_public_nonce_sum) assert aggsig.verify(secp, refund_signature, refund_public_excess_sum, refund_fee_amount, refund_lock_height), \ "Unable to verify refund signature" refund_kernel = refund_tx.kernels[0] refund_kernel.excess = refund_tx.sum_commitments(secp) refund_kernel.excess_signature = refund_signature assert refund_tx.verify_kernels(secp), "Unable to verify refund kernel" print("Refund tx is valid") f = open("logs/{}_refund.json".format(now), "w") f.write(json.dumps(refund_tx.to_dict(secp), indent=2)) f.close() refund_tx_wrapper = {"tx_hex": refund_tx.to_hex(secp).decode()} f = open("logs/{}_refund_hex.json".format(now), "w") f.write(json.dumps(refund_tx_wrapper, indent=2)) f.close() print("Finalizing multisig tx..") # Partial signature partial_signature = aggsig.calculate_partial(secp, excess, nonce, public_nonce_sum, fee_amount, lock_height) # Final signature public_excess_sum = PublicKey.from_combination( secp, [public_excess_recv, public_excess]) signature = aggsig.add_partials( secp, [partial_signature_recv, partial_signature], public_nonce_sum) assert aggsig.verify(secp, signature, public_excess_sum, fee_amount, lock_height), "Unable to verify signature" kernel = tx.kernels[0] kernel.excess = tx.sum_commitments(secp) kernel.excess_signature = signature assert tx.verify_kernels(secp), "Unable to verify kernel" f = open("logs/{}_tx.json".format(now), "w") f.write(json.dumps(tx.to_dict(secp), indent=2)) f.close() tx_wrapper = {"tx_hex": tx.to_hex(secp).decode()} f = open("logs/{}_tx_hex.json".format(now), "w") f.write(json.dumps(tx_wrapper, indent=2)) f.close() print("Submitting to node..") urlopen("{}/v1/pool/push".format(node_url) + ("?fluff" if fluff else ""), json.dumps(tx_wrapper).encode(), 600) wallet.save() print("Transaction complete!")
def finalize_swap(self): seller = self.role == Role.SELLER assert (seller and self.stage == Stage.DONE) or ( not seller and self.stage == Stage.SWAP), "Incorrect stage" if seller: self.secret_lock = self.foreign_partial_swap_adaptor.scalar( self.secp).add( self.secp, self.foreign_partial_swap_signature.scalar( self.secp).negate(self.secp)) public_lock = self.secret_lock.to_public_key(self.secp) assert self.public_lock == public_lock, "Invalid secret lock, this should never happen" if self.is_bitcoin_swap(): tx = BitcoinTransaction(2, [], [], int(time())) input_script = self.generate_btc_script() for output_point in self.btc_output_points: tx.add_input( BitcoinInput(output_point.txid, output_point.index, input_script, bytearray(), None)) output = BitcoinOutput( 1, Script.p2( Address.from_base58check( self.swap_receive_address.encode()))) tx.add_output(output) tx_size = len(tx.to_bytearray()) + 270 * len( self.btc_output_points) # estimate total tx size fee = 2 * tx_size # 2 sat/B output.value = self.swap_amount - fee for i in range(len(tx.inputs)): signature_a = tx.raw_signature(self.secp, i, self.swap_cosign) signature_b = tx.raw_signature(self.secp, i, self.secret_lock) prev_script = self.generate_btc_script() script_sig = bytearray() script_sig.append(OP_FALSE) script_sig.extend(script_write_bytes(len(signature_a))) script_sig.extend(signature_a) script_sig.extend(script_write_bytes(len(signature_b))) script_sig.extend(signature_b) script_sig.append(OP_FALSE) script_sig.extend(script_write_bytes(len(prev_script))) script_sig.extend(prev_script) tx.inputs[i].script_sig = script_sig self.claim = hexlify(tx.to_bytearray()) if self.is_ether_swap(): self.claim = self.secp.sign_recoverable( self.secret_lock, bytearray([0] * 32)) else: swap_input = Input(OutputFeatures.DEFAULT_OUTPUT, self.commit) public_swap_nonce_sum = PublicKey.from_combination( self.secp, [self.public_swap_nonce, self.foreign_public_swap_nonce]) swap_signature = aggsig.add_partials(self.secp, [ self.partial_swap_signature, self.foreign_partial_swap_signature ], public_swap_nonce_sum) assert aggsig.verify( self.secp, swap_signature, self.public_swap_excess, self.swap_fee_amount, self.swap_lock_height), "Unable to verify swap signature" swap_kernel = Kernel(0, self.swap_fee_amount, self.swap_lock_height, None, None) self.swap_tx = Transaction([swap_input], [self.swap_output], [swap_kernel], self.swap_offset) swap_kernel.excess = self.swap_tx.sum_commitments(self.secp) swap_kernel.excess_signature = swap_signature assert self.swap_tx.verify_kernels( self.secp), "Unable to verify swap kernel"
def verify(self, secp: Secp256k1) -> bool: return aggsig.verify(secp, self.excess_signature, self.excess.to_public_key(secp), self.fee, self.lock_height)