Skip to content

Commit

Permalink
good progress changing SignedOrder to include Voucher resource and key
Browse files Browse the repository at this point in the history
  • Loading branch information
devmannic committed Mar 10, 2022
1 parent 3ed7449 commit df77759
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
12 changes: 8 additions & 4 deletions 1-exchanges/hareswap/hare/src/harelib/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub struct MakeSignedOrder {
}

use k256::{
ecdsa::{SigningKey, signature::Signer, VerifyingKey, signature::Verifier, Signature},
ecdsa::{SigningKey, signature::Signer, /* VerifyingKey, signature::Verifier, */ Signature},
};
use hex;
// use transaction_manifest::parser::Parser as ManifestParser;
Expand Down Expand Up @@ -140,12 +140,14 @@ impl MakeSignedOrder {
},
};

let voucher_resource: ResourceDef = Address::from_str(&self.voucher_address).map_err(Error::ParseAddressError)?.into();
let voucher_key: NonFungibleKey = 1u128.into(); // TODO make this an parameter
//key: Some(NonFungibleKey::from_str("order1").unwrap()), // TODO choose key and map_err
let nfd = matched_order.as_passthru();

let voucher = Voucher {
resource_def: Address::from_str(&self.voucher_address).map_err(Error::ParseAddressError)?.into(),
//key: Some(NonFungibleKey::from_str("order1").unwrap()), // TODO choose key and map_err
key: None,
resource_def: voucher_resource.clone(),
key: Some(voucher_key.clone()),
nfd,
};

Expand Down Expand Up @@ -174,6 +176,8 @@ impl MakeSignedOrder {

let signed_order = SignedOrder {
order: matched_order,
voucher_resource,
voucher_key,
signature: sig_bytes,
};

Expand Down
18 changes: 15 additions & 3 deletions 1-exchanges/hareswap/src/maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use scrypto::prelude::*;
use sbor::*;

use super::transporter::blueprint::{Transporter, SealedVoucher};
use super::transporter::voucher::{Voucher, IsPassThruNFD};
use super::requirement::{BucketRequirement, BucketContents};
use super::account::*;

Expand Down Expand Up @@ -40,10 +41,13 @@ pub struct MatchedOrder {
// in a seperate module to deal with conflicting `decode` for sbor::Decode and NonFungibleData on MatchedOrder during derive
mod signed_order {
use super::MatchedOrder;
use super::{ResourceDef, NonFungibleKey};
use sbor::{TypeId, Encode, Decode, Describe};
#[derive(Debug, Clone, TypeId, Encode, Decode, PartialEq, Eq, Describe)]
pub struct SignedOrder {
pub order: MatchedOrder,
pub voucher_resource: ResourceDef,
pub voucher_key: NonFungibleKey,
pub signature: Vec<u8>,
}
}
Expand Down Expand Up @@ -185,19 +189,27 @@ blueprint! {
pub fn tokenize_order(&mut self, signed_order: SignedOrder, taker_auth: BucketRef) -> Bucket {
let SignedOrder {
order,
voucher_resource,
voucher_key,
signature,
} = signed_order;
// check taker_auth matches the order before redeeming it. (if it matches but the signature is bad it wont redeem properly anyway)
// check binding to taker - stops frontrunning the (public) SignedOrder (along with using redeem_auth)
assert_eq!(order.partial_order.taker_auth.check_at_least_ref(&taker_auth), true, "tokenize_order: taker_auth not accepted");

let voucher = SealedVoucher {
serialized: scrypto_encode(&order),
let voucher = Voucher {
resource_def: voucher_resource,
key: Some(voucher_key),
nfd: order.as_passthru(),
};

let sealed_voucher = SealedVoucher {
serialized: scrypto_encode(&voucher),
signature
};

self.redeem_auth.authorize(|auth|
self.transporter.redeem(voucher, None, auth) // panics on bad vouchers
self.transporter.redeem(sealed_voucher, None, auth) // panics on bad vouchers
)
}

Expand Down
3 changes: 2 additions & 1 deletion 1-exchanges/hareswap/src/transporter/voucher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub struct PassThruNFD {
mutable_data: Vec<u8>,
}

pub trait IsPassThruNFD: NonFungibleData {
// note: can't implement From, this works just as well with a specific method name
pub trait IsPassThruNFD: NonFungibleData + Sized {
fn as_passthru(&self) -> PassThruNFD {
PassThruNFD {
immutable_data: self.immutable_data(),
Expand Down
3 changes: 2 additions & 1 deletion 1-exchanges/hareswap/testing/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ $HARE request-for-quote partial_order.txt $TAKER_AMOUNT $T $M $TAKER_AUTH
# simulate send to maker
# maker decide on price and sign order
MAKER_AMOUNT=200
$HARE make-signed-order partial_order.txt $MAKER_AMOUNT $MAKER_COMPONENT $MAKER_OFFLINE_KEY_PRI > signed_order.txt
# TODO get VOUCHER_ADDRESS or don't pass it along let the maker look it up
$HARE make-signed-order partial_order.txt $MAKER_AMOUNT $MAKER_COMPONENT $VOUCHER_ADDRESS $MAKER_OFFLINE_KEY_PRI > signed_order.txt
SIGNED_ORDER=$(cat signed_order.txt)

## 4-A taker: OPTION 1 - simple execution
Expand Down

0 comments on commit df77759

Please sign in to comment.