Skip to content

Commit 129edb4

Browse files
committed
add new Proposal type
1 parent 056044e commit 129edb4

File tree

4 files changed

+87
-82
lines changed

4 files changed

+87
-82
lines changed

src/chain-wallet-libs/bindings/wallet-wasm-js/js-test/test/vote_cast.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,39 @@ describe("vote cast certificate tests", function () {
1313
const wallet = await import("wallet-js");
1414

1515
let settings = new wallet.Settings(settings_json);
16-
let vote = wallet.Vote.public(
17-
wallet.SpendingCounter.new(1, 1),
16+
let proposal = new wallet.Proposal(
1817
Buffer.from(
1918
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
2019
"hex"
2120
),
22-
8,
23-
0
21+
2,
22+
true,
23+
8
2424
);
25-
26-
let block_date = new wallet.BlockDate(0, 1);
27-
let fragments = wallet.signVotes([vote], settings, block_date, private_key);
25+
let vote = proposal.vote(1, 1, new wallet.BlockDate(0, 1), 0);
26+
let fragments = wallet.signVotes([vote], settings, private_key);
2827
assert(fragments.length == 1);
2928
});
3029

3130
it("private", async function () {
3231
const wallet = await import("wallet-js");
3332

3433
let settings = new wallet.Settings(settings_json);
35-
let vote = wallet.Vote.private(
36-
wallet.SpendingCounter.new(1, 1),
34+
let proposal = new wallet.Proposal(
3735
Buffer.from(
3836
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
3937
"hex"
4038
),
41-
8,
4239
4,
43-
0,
40+
true,
41+
8,
4442
Buffer.from(
4543
"bed88887abe0a84f64691fe0bdfa3daf1a6cd697a13f07ae07588910ce39c927",
4644
"hex"
4745
)
4846
);
49-
50-
let block_date = new wallet.BlockDate(0, 1);
51-
let fragments = wallet.signVotes([vote], settings, block_date, private_key);
47+
let vote = proposal.vote(1, 1, new wallet.BlockDate(0, 1), 0);
48+
let fragments = wallet.signVotes([vote], settings, private_key);
5249
assert(fragments.length == 1);
5350
});
5451
});

src/chain-wallet-libs/bindings/wallet-wasm-js/js/index.js

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const wasm = require("wallet-wasm-js");
22

3-
module.exports.SpendingCounter = wasm.SpendingCounter;
4-
53
class BlockDate {
64
constructor(epoch, slot) {
75
this.epoch = epoch;
@@ -22,52 +20,91 @@ class Settings {
2220
module.exports.Settings = Settings;
2321

2422
class Vote {
25-
static public(spending_counter, vote_plan_bytes, proposal_index, choice) {
26-
let res = new Vote();
27-
let vote_plan = wasm.VotePlanId.from_bytes(vote_plan_bytes);
28-
let payload = wasm.Payload.new_public(choice);
29-
let vote_cast = wasm.VoteCast.new(vote_plan, proposal_index, payload);
30-
res.vote_cast = vote_cast;
31-
res.spending_counter = spending_counter;
32-
return res;
33-
}
34-
35-
static private(
23+
constructor(
3624
spending_counter,
37-
vote_plan_bytes,
25+
spending_counter_lane,
26+
valid_until,
27+
vote_plan,
3828
proposal_index,
39-
options,
4029
choice,
30+
options,
4131
public_key
4232
) {
43-
let res = new Vote();
44-
let vote_plan = wasm.VotePlanId.from_bytes(vote_plan_bytes);
45-
let payload = wasm.Payload.new_private(
46-
vote_plan,
47-
options,
48-
choice,
49-
public_key
50-
);
51-
vote_plan = wasm.VotePlanId.from_bytes(vote_plan_bytes);
52-
let vote_cast = wasm.VoteCast.new(vote_plan, proposal_index, payload);
53-
res.vote_cast = vote_cast;
54-
res.spending_counter = spending_counter;
55-
return res;
33+
this.spending_counter = spending_counter;
34+
this.spending_counter_lane = spending_counter_lane;
35+
this.valid_until = valid_until;
36+
if (options != undefined && public_key != undefined) {
37+
let payload = wasm.Payload.new_private(
38+
vote_plan,
39+
options,
40+
choice,
41+
public_key
42+
);
43+
this.vote_cast = wasm.VoteCast.new(vote_plan, proposal_index, payload);
44+
} else {
45+
let payload = wasm.Payload.new_public(choice);
46+
this.vote_cast = wasm.VoteCast.new(vote_plan, proposal_index, payload);
47+
}
5648
}
5749
}
5850
module.exports.Vote = Vote;
5951

60-
function signVotes(votes, settings, valid_until, private_key) {
52+
class Proposal {
53+
constructor(
54+
vote_plan_bytes,
55+
options_count,
56+
vote_public,
57+
proposal_index,
58+
committee_public_key
59+
) {
60+
this.vote_plan = wasm.VotePlanId.from_bytes(vote_plan_bytes);
61+
this.options_count = options_count;
62+
this.vote_public = vote_public;
63+
this.proposal_index = proposal_index;
64+
this.committee_public_key = committee_public_key;
65+
}
66+
67+
vote(spending_counter, spending_counter_lane, valid_until, choice) {
68+
if (this.vote_public) {
69+
return new Vote(
70+
spending_counter,
71+
spending_counter_lane,
72+
valid_until,
73+
this.vote_plan,
74+
this.proposal_index,
75+
choice
76+
);
77+
} else {
78+
return new Vote(
79+
spending_counter,
80+
spending_counter_lane,
81+
valid_until,
82+
this.vote_plan,
83+
this.proposal_index,
84+
choice,
85+
this.options_count,
86+
this.committee_public_key
87+
);
88+
}
89+
}
90+
}
91+
module.exports.Proposal = Proposal;
92+
93+
function signVotes(votes, settings, private_key) {
6194
let fragments = [];
6295
for (let i = 0; i < votes.length; i++) {
6396
let builder = wasm.VoteCastTxBuilder.new(
6497
settings.settings,
65-
valid_until.epoch,
66-
valid_until.slot,
98+
votes[i].valid_until.epoch,
99+
votes[i].valid_until.slot,
67100
votes[i].vote_cast
68101
);
69102
let fragment = builder
70-
.build_tx(private_key, votes[i].spending_counter)
103+
.build_tx(
104+
private_key,
105+
votes[i].spending_counter,
106+
votes[i].spending_counter_lane
107+
)
71108
.finalize_tx();
72109
fragments.push(fragment);
73110
}

src/chain-wallet-libs/bindings/wallet-wasm-js/src/lib.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ pub use certificates::{
55
vote_cast::{Payload, VoteCast},
66
vote_plan::VotePlanId,
77
};
8+
use chain_impl_mockchain::account::SpendingCounter;
89
use chain_impl_mockchain::header::BlockDate;
910
use chain_impl_mockchain::{
1011
certificate::VoteCast as VoteCastLib, fragment::Fragment as FragmentLib,
1112
};
1213
pub use fragment::{Fragment, FragmentId};
13-
pub use utils::set_panic_hook;
1414
use wasm_bindgen::prelude::*;
1515

1616
mod certificates;
@@ -37,22 +37,6 @@ impl Settings {
3737
}
3838
}
3939

40-
#[derive(Clone)]
41-
#[wasm_bindgen]
42-
pub struct SpendingCounter(chain_impl_mockchain::account::SpendingCounter);
43-
44-
#[wasm_bindgen]
45-
impl SpendingCounter {
46-
pub fn new(lane: usize, counter: u32) -> Result<SpendingCounter, JsValue> {
47-
Ok(Self(
48-
chain_impl_mockchain::account::SpendingCounter::new(lane, counter)
49-
.map_err(|e| JsValue::from(e.to_string()))?,
50-
))
51-
}
52-
}
53-
54-
impl_collection!(SpendingCounters, SpendingCounter);
55-
5640
#[wasm_bindgen]
5741
impl VoteCastTxBuilder {
5842
/// Initializing of the VoteCastTxBuilder
@@ -80,11 +64,15 @@ impl VoteCastTxBuilder {
8064
pub fn build_tx(
8165
mut self,
8266
account: &[u8],
83-
spending_counter: SpendingCounter,
67+
counter: u32,
68+
lane: usize,
8469
) -> Result<VoteCastTxBuilder, JsValue> {
8570
self.0 = self
8671
.0
87-
.build_tx(account, spending_counter.0)
72+
.build_tx(
73+
account,
74+
SpendingCounter::new(lane, counter).map_err(|e| JsValue::from(e.to_string()))?,
75+
)
8876
.map_err(|e| JsValue::from(e.to_string()))?;
8977
Ok(self)
9078
}

src/chain-wallet-libs/bindings/wallet-wasm-js/src/utils.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
use wasm_bindgen::prelude::*;
2-
3-
/// `set_panic_hook` function can be called at least once during initialization,
4-
/// to get better error messages if the code ever panics.
5-
/// The function has no parameters.
6-
#[wasm_bindgen]
7-
pub fn set_panic_hook() {
8-
// When the `console_error_panic_hook` feature is enabled, we can call the
9-
// `set_panic_hook` function at least once during initialization, and then
10-
// we will get better error messages if our code ever panics.
11-
//
12-
// For more details see
13-
// https://github.com/rustwasm/console_error_panic_hook#readme
14-
#[cfg(feature = "console_error_panic_hook")]
15-
console_error_panic_hook::set_once();
16-
}
17-
181
// taken from:
192
// https://github.com/input-output-hk/js-chain-libs/blob/cc463b59fdc64a4fff63f67901118f60b783520c/src/utils.rs#L12
203
#[macro_export]

0 commit comments

Comments
 (0)