Skip to content

Commit 263ff69

Browse files
committed
Tests for encode_partially_signed_transaction; code cleanup
1 parent f82973c commit 263ff69

File tree

15 files changed

+560
-241
lines changed

15 files changed

+560
-241
lines changed

common/src/chain/partially_signed_transaction/additional_info.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ use crate::{
2626
primitives::Amount,
2727
};
2828

29+
// Note: PoolAdditionalInfo and OrderAdditionalInfo below are identical to the corresponding
30+
// structs in `input_commitments/info_providers.rs` (except that those don't derive Encode/Decode)
31+
// and basically serve the same purpose.
32+
// We keep them separate because:
33+
// 1) Technically we may want to have even more info inside partially signed transaction in
34+
// the future, which may not be needed by the input commitments.
35+
// 2) We want to be able to refactor input commitments structs without worrying about breaking
36+
// PartiallySignedTransaction's backward compatibility.
37+
2938
/// Pool additional info, which must be present for each ProduceBlockFromStake UTXO consumed by
3039
/// the transaction. Transaction's signature commits to this info since SighashInputCommitments::V1.
31-
// TODO: rename to DecommissionedPoolAdditionalInfo? (or even DecommissionedPoolCommitmentInfo,
32-
// to suggest that it's related to commitments and isn't some arbitrary info about the pool).
33-
#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode)]
40+
#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, serde::Serialize)]
3441
pub struct PoolAdditionalInfo {
3542
pub staker_balance: Amount,
3643
}
@@ -42,29 +49,23 @@ pub struct PoolAdditionalInfo {
4249
/// while FillOrder commitments only include the initial ones. So this info representation
4350
/// is not ideal, as it forces the caller to provide additional info that will not actually
4451
/// be used.
45-
// TODO: perhaps it's better to split the struct and the corresponding map into two -
46-
// for the initial and current balances. Also, perhaps the naming should suggest that it
47-
// contains commitments for signing and not just arbitrary info.
48-
#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode)]
52+
#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, serde::Serialize)]
4953
pub struct OrderAdditionalInfo {
5054
pub initially_asked: OutputValue,
5155
pub initially_given: OutputValue,
5256
pub ask_balance: Amount,
5357
pub give_balance: Amount,
5458
}
5559

56-
// FIXME: Rename to PtxAdditionalInfo ?
57-
#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode)]
60+
#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, serde::Serialize)]
5861
pub struct TxAdditionalInfo {
59-
// token_info: BTreeMap<TokenId, TokenAdditionalInfo>,
6062
pool_info: BTreeMap<PoolId, PoolAdditionalInfo>,
6163
order_info: BTreeMap<OrderId, OrderAdditionalInfo>,
6264
}
6365

6466
impl TxAdditionalInfo {
6567
pub fn new() -> Self {
6668
Self {
67-
// token_info: BTreeMap::new(),
6869
pool_info: BTreeMap::new(),
6970
order_info: BTreeMap::new(),
7071
}
@@ -89,11 +90,9 @@ impl TxAdditionalInfo {
8990
}
9091

9192
pub fn join(mut self, other: Self) -> Self {
92-
// self.token_info.extend(other.token_info);
9393
self.pool_info.extend(other.pool_info);
9494
self.order_info.extend(other.order_info);
9595
Self {
96-
// token_info: self.token_info,
9796
pool_info: self.pool_info,
9897
order_info: self.order_info,
9998
}

common/src/chain/partially_signed_transaction/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub enum PartiallySignedTransactionConsistencyCheck {
7373
/// via the wasm call `encode_partially_signed_transaction` and the latter pass it to
7474
/// wallet-rpc-daemon. The transaction is treated as a black box, so a breaking change is
7575
/// technically possible, though it'll require synchronization between multiple teams.
76-
#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode)]
76+
#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode, serde::Serialize)]
7777
pub struct PartiallySignedTransaction {
7878
tx: Transaction,
7979
witnesses: Vec<Option<InputWitness>>,
@@ -163,8 +163,9 @@ impl PartiallySignedTransaction {
163163
Ok(())
164164
}
165165

166-
// FIXME tests
167166
fn ensure_additional_info_completeness(&self) -> Result<(), PartiallySignedTransactionError> {
167+
// TODO: try to re-use the input commitments machinery here instead of doing custom checks.
168+
168169
let ensure_order_info_present =
169170
|order_id: &OrderId| -> Result<_, PartiallySignedTransactionError> {
170171
ensure!(
@@ -235,7 +236,6 @@ impl PartiallySignedTransaction {
235236
}
236237
}
237238
TxInput::OrderAccountCommand(command) => {
238-
// FIXME somehow re-use input commitment machinery?
239239
match command {
240240
OrderAccountCommand::FillOrder(id, _)
241241
| OrderAccountCommand::ConcludeOrder(id) => ensure_order_info_present(id)?,

wallet/wallet-controller/src/helpers/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ pub async fn fetch_input_infos<T: NodeInterface, B: storage::Backend>(
315315

316316
Ok((input_utxos, ptx_additional_info, destinations))
317317
}
318+
318319
async fn into_utxo_and_destination<T: NodeInterface, B: storage::Backend>(
319320
rpc_client: &T,
320321
wallet: &RuntimeWallet<B>,
@@ -402,10 +403,8 @@ fn collect_referenced_token_ids_from_ptx(
402403
ptx: &PartiallySignedTransaction,
403404
dest: &mut BTreeSet<TokenId>,
404405
) {
405-
for input_utxo in ptx.input_utxos() {
406-
if let Some(tx_output) = input_utxo {
407-
collect_referenced_token_ids_from_tx_output(tx_output, dest);
408-
}
406+
for input_utxo in ptx.input_utxos().iter().flatten() {
407+
collect_referenced_token_ids_from_tx_output(input_utxo, dest);
409408
}
410409

411410
for tx_output in ptx.tx().outputs() {

wasm-wrappers/WASM-API.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,43 @@ and a network type (mainnet, testnet, etc), this function returns a witness to b
271271

272272
Given an unsigned transaction and signatures, this function returns a SignedTransaction object as bytes.
273273

274+
### Function: `encode_partially_signed_transaction`
275+
276+
Return a PartiallySignedTransaction object as bytes.
277+
278+
`transaction` is an encoded `Transaction` (which can be produced via `encode_transaction`).
279+
280+
`signatures`, `input_utxos`, `input_destinations` and `htlc_secrets` are encoded lists of
281+
optional objects of the corresponding type. To produce such a list, iterate over your
282+
original list of optional objects and then:
283+
1) emit byte 0 if the current object is null;
284+
2) otherwise emit byte 1 followed by the object in its encoded form.
285+
286+
Each individual object in each of the lists corresponds to the transaction input with the same
287+
index and its meaning is as follows:
288+
1) `signatures` - the signature for the input;
289+
2) `input_utxos`- the utxo for the input (if it's utxo-based);
290+
3) `input_destinations` - the destination (address) corresponding to the input; this determines
291+
the key(s) with which the input has to be signed. Note that for utxo-based inputs the
292+
corresponding destination can usually be extracted from the utxo itself (the exception
293+
being the `ProduceBlockFromStake` utxo, which doesn't contain the pool's decommission key).
294+
However, PartiallySignedTransaction requires that *all* input destinations are provided
295+
explicitly anyway.
296+
4) `htlc_secrets` - if the input is an HTLC one and if the transaction is spending the HTLC,
297+
this should be the HTLC secret. Otherwise it should be null.
298+
299+
The number of items in each list must be equal to the number of transaction inputs.
300+
301+
`additional_info` has the same meaning as in `encode_witness`.
302+
303+
### Function: `decode_partially_signed_transaction_to_js`
304+
305+
Decodes a partially signed transaction from its binary encoding into a JavaScript object.
306+
307+
### Function: `encode_destination`
308+
309+
Convert the specified string address into a Destination object, encoded as bytes.
310+
274311
### Function: `get_transaction_id`
275312

276313
Given a `Transaction` encoded in bytes (not a signed transaction, but a signed transaction is tolerated by ignoring the extra bytes, by choice)
@@ -338,6 +375,7 @@ Note:
338375
instead of `encode_witness`).
339376
Note that in orders v0 FillOrder inputs can technically have a signature, it's just not checked.
340377
But in orders V1 we actually require that those inputs don't have signatures.
378+
Also, in orders V1 the provided destination is always ignored.
341379

342380
### Function: `encode_input_for_freeze_order`
343381

wasm-wrappers/js-bindings-test/tests/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { test_misc } from "./test_misc.js";
2626
import { test_orders } from "./test_orders.js";
2727
import { test_signed_transaction_intent } from "./test_signed_transaction_intent.js";
2828
import { test_transaction_and_witness_encoding } from "./test_transaction_and_witness_encoding.js";
29+
import { test_partially_signed_transaction_encoding } from "./test_partially_signed_transaction_encoding.js";
2930

3031
/** @public */
3132
export function run_all_tests() {
@@ -38,4 +39,5 @@ export function run_all_tests() {
3839
run_one_test(test_orders);
3940
run_one_test(test_signed_transaction_intent);
4041
run_one_test(test_transaction_and_witness_encoding);
42+
run_one_test(test_partially_signed_transaction_encoding);
4143
}

wasm-wrappers/js-bindings-test/tests/test_encode_other_outputs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,11 @@ function issue_fungible_token_test() {
380380
}
381381

382382
function issue_nft_test() {
383-
const account_pubkey = make_default_account_privkey(
383+
const account_privkey = make_default_account_privkey(
384384
MNEMONIC,
385385
Network.Testnet
386386
);
387-
const receiving_privkey = make_receiving_address(account_pubkey, 0);
387+
const receiving_privkey = make_receiving_address(account_privkey, 0);
388388
const receiving_pubkey = public_key_from_private_key(receiving_privkey);
389389

390390
let encoded_nft = encode_output_issue_nft(

wasm-wrappers/js-bindings-test/tests/test_htlc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ import {
5454
} from "./test_encode_other_outputs.js";
5555

5656
export function test_htlc() {
57-
const account_pubkey = make_default_account_privkey(
57+
const account_privkey = make_default_account_privkey(
5858
MNEMONIC,
5959
Network.Testnet
6060
);
61-
const receiving_privkey = make_receiving_address(account_pubkey, 0);
61+
const receiving_privkey = make_receiving_address(account_privkey, 0);
6262

6363
const htlc_coins_output = encode_output_htlc(
6464
Amount.from_atoms("40000"),

0 commit comments

Comments
 (0)