hex memos

This commit is contained in:
Aditya Kulkarni 2020-07-21 10:12:09 -07:00
parent f0e13e3caf
commit 4c87e55afa
2 changed files with 62 additions and 7 deletions

View File

@ -1842,13 +1842,26 @@ impl LightWallet {
// Compute memo if it exists
let encoded_memo = match memo {
None => None,
Some(s) => match Memo::from_bytes(s.as_bytes()) {
None => {
let e = format!("Error creating output. Memo {:?} is too long", s);
error!("{}", e);
return Err(e);
},
Some(m) => Some(m)
Some(s) => {
// If the string starts with an "0x", and contains only hex chars ([a-f0-9]+) then
// interpret it as a hex
let s_bytes = if s.to_lowercase().starts_with("0x") {
match hex::decode(&s[2..s.len()]) {
Ok(data) => data,
Err(_) => Vec::from(s.as_bytes())
}
} else {
Vec::from(s.as_bytes())
};
match Memo::from_bytes(&s_bytes) {
None => {
let e = format!("Error creating output. Memo {:?} is too long", s);
error!("{}", e);
return Err(e);
},
Some(m) => Some(m)
}
}
};

View File

@ -1295,6 +1295,48 @@ fn test_z_incoming_memo() {
}
}
#[test]
fn test_z_incoming_hex_memo() {
const AMOUNT1: u64 = 50000;
let (wallet, _txid1, block_hash) = get_test_wallet(AMOUNT1);
let my_address = encode_payment_address(wallet.config.hrp_sapling_address(),
&wallet.extfvks.read().unwrap()[0].default_address().unwrap().1);
let orig_memo = "hello world".to_string();
let memo = format!("0x{}", hex::encode(&orig_memo));
let fee: u64 = DEFAULT_FEE.try_into().unwrap();
let branch_id = u32::from_str_radix("2bb40e60", 16).unwrap();
let (ss, so) = get_sapling_params().unwrap();
// Create a tx and send to address
let raw_tx = wallet.send_to_address(branch_id, &ss, &so,
vec![(&my_address, AMOUNT1 - fee, Some(memo.clone()))]).unwrap();
let sent_tx = Transaction::read(&raw_tx[..]).unwrap();
let sent_txid = sent_tx.txid();
// Add it to a block
let mut cb3 = FakeCompactBlock::new(2, block_hash);
cb3.add_tx(&sent_tx);
wallet.scan_block(&cb3.as_bytes()).unwrap();
// And scan the Full Tx to get the memo
wallet.scan_full_tx(&sent_tx, 2, 0);
{
let txs = wallet.txs.read().unwrap();
assert_eq!(txs[&sent_txid].notes.len(), 1);
assert_eq!(txs[&sent_txid].notes[0].extfvk, wallet.extfvks.read().unwrap()[0]);
assert_eq!(txs[&sent_txid].notes[0].note.value, AMOUNT1 - fee);
assert_eq!(LightWallet::note_address(wallet.config.hrp_sapling_address(), &txs[&sent_txid].notes[0]), Some(my_address));
assert_eq!(LightWallet::memo_str(&txs[&sent_txid].notes[0].memo), Some(orig_memo));
}
}
#[test]
fn test_add_new_zt_hd_after_incoming() {
// When an address recieves funds, a new, unused address should automatically get added