Skip to content

Commit

Permalink
Merge branch 'main' into NPG-3863-update-get-proposals-by-chain-id-en…
Browse files Browse the repository at this point in the history
…dpoint-with-array-of-attributes-representing-detailed-writeup
  • Loading branch information
FelipeRosa committed Dec 2, 2022
2 parents 99ab442 + 3294fe7 commit e3cfd37
Show file tree
Hide file tree
Showing 22 changed files with 352 additions and 154 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci_tests.yml
Expand Up @@ -224,6 +224,11 @@ jobs:
cd src/chain-wallet-libs/bindings/wallet-js
wasm-pack build --release --target=nodejs -d pkg
- name: Build JS package
run: |
cd src/chain-wallet-libs/bindings/wallet-js/js
npm install
- name: Run JS tests
run: |
cd src/chain-wallet-libs/bindings/wallet-js/js-test
Expand Down
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 43 additions & 16 deletions src/chain-libs/chain-impl-mockchain/src/key.rs
Expand Up @@ -226,22 +226,41 @@ impl<T: Clone, A: VerificationAlgorithm> Clone for Signed<T, A> {
any(test, feature = "property-test-api"),
derive(test_strategy::Arbitrary)
)]
pub struct Hash(crypto::Blake2b256);
pub struct Hash {
hash: crypto::Blake2b256,
}

impl Hash {
pub fn new(hash: crypto::Blake2b256) -> Self {
Self { hash }
}

pub fn get_hash(&self) -> &crypto::Blake2b256 {
&self.hash
}

/// All 0 hash used as a special hash
pub fn zero_hash() -> Self {
Hash(crypto::Blake2b256::from([0; crypto::Blake2b256::HASH_SIZE]))
Hash {
hash: crypto::Blake2b256::from([0; crypto::Blake2b256::HASH_SIZE]),
}
}

pub fn hash_bytes(bytes: &[u8]) -> Self {
Hash(crypto::Blake2b256::new(bytes))
Hash {
hash: crypto::Blake2b256::new(bytes),
}
}

pub fn from_bytes(bytes: [u8; 32]) -> Self {
Hash(crypto::Blake2b256::from(bytes))
Hash {
hash: crypto::Blake2b256::from(bytes),
}
}

#[inline]
pub fn as_bytes(&self) -> &[u8] {
self.0.as_ref()
self.hash.as_ref()
}
}

Expand All @@ -253,63 +272,69 @@ impl From<[u8; 32]> for Hash {

impl From<Hash> for [u8; 32] {
fn from(h: Hash) -> Self {
h.0.into()
h.hash.into()
}
}

impl<'a> From<&'a Hash> for &'a [u8; 32] {
fn from(h: &'a Hash) -> Self {
(&h.0).into()
(&h.hash).into()
}
}

impl Serialize for Hash {
fn serialized_size(&self) -> usize {
self.0.as_hash_bytes().serialized_size()
self.hash.as_hash_bytes().serialized_size()
}

fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
codec.put_bytes(self.0.as_hash_bytes())
codec.put_bytes(self.hash.as_hash_bytes())
}
}

impl Deserialize for Hash {
fn deserialize<R: std::io::Read>(codec: &mut Codec<R>) -> Result<Self, ReadError> {
let bytes = <[u8; crypto::Blake2b256::HASH_SIZE]>::deserialize(codec)?;
Ok(Hash(crypto::Blake2b256::from(bytes)))
Ok(Hash {
hash: crypto::Blake2b256::from(bytes),
})
}
}

impl BlockId for Hash {
fn zero() -> Hash {
Hash(crypto::Blake2b256::from([0; crypto::Blake2b256::HASH_SIZE]))
Hash {
hash: crypto::Blake2b256::from([0; crypto::Blake2b256::HASH_SIZE]),
}
}
}

impl FragmentId for Hash {}

impl AsRef<[u8]> for Hash {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
self.hash.as_ref()
}
}

impl From<crypto::Blake2b256> for Hash {
fn from(hash: crypto::Blake2b256) -> Self {
Hash(hash)
Hash { hash }
}
}

impl std::fmt::Display for Hash {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
write!(f, "{}", self.hash)
}
}

impl FromStr for Hash {
type Err = crypto::hash::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Hash(crypto::Blake2b256::from_str(s)?))
Ok(Hash {
hash: crypto::Blake2b256::from_str(s)?,
})
}
}

Expand Down Expand Up @@ -408,7 +433,9 @@ mod tests {

impl Arbitrary for Hash {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
Hash(Arbitrary::arbitrary(g))
Hash {
hash: Arbitrary::arbitrary(g),
}
}
}
impl Arbitrary for EitherEd25519SecretKey {
Expand Down
10 changes: 10 additions & 0 deletions src/chain-libs/chain-time/src/era.rs
Expand Up @@ -85,6 +85,16 @@ impl TimeEra {
}
}

/// retrieve the epoch start during a given Era
pub fn epoch_start(&self) -> Epoch {
self.epoch_start
}

/// retrieve the slot start of an epoch during a given Era
pub fn slot_start(&self) -> Slot {
self.slot_start
}

/// retrieve the number of slots in an epoch during a given Era
pub fn slots_per_epoch(&self) -> u32 {
self.slots_per_epoch
Expand Down
2 changes: 1 addition & 1 deletion src/chain-libs/chain-time/src/timeframe.rs
Expand Up @@ -10,7 +10,7 @@ use std::time::{Duration, SystemTime};
any(test, feature = "property-test-api"),
derive(test_strategy::Arbitrary)
)]
pub struct Slot(pub(crate) u64);
pub struct Slot(pub u64);

impl From<u64> for Slot {
fn from(slot_number: u64) -> Slot {
Expand Down
3 changes: 1 addition & 2 deletions src/chain-wallet-libs/bindings/wallet-core/src/wallet.rs
Expand Up @@ -32,15 +32,14 @@ impl Wallet {
self.account.account_id()
}

/// Retrieve a wallet from a list of free keys used as utxo's
/// Retrieve a wallet from a key used as utxo's
///
/// You can also use this function to recover a wallet even after you have
/// transferred all the funds to the new format (see the [Self::convert] function).
///
/// Parameters
///
/// * `account_key`: the private key used for voting
/// * `keys`: unused
///
/// # Errors
///
Expand Down
1 change: 1 addition & 0 deletions src/chain-wallet-libs/bindings/wallet-js/Cargo.toml
Expand Up @@ -30,6 +30,7 @@ wallet-core = {path = "../wallet-core"}
wasm-bindgen = "0.2"
js-sys = "0.3.40"
bech32 = "0.7.2"
serde_json = "1.0"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand Down
Expand Up @@ -13,6 +13,6 @@
],
"devDependencies": {
"mocha": "^10.1.0",
"wallet-js": "file:../pkg"
"wallet-js": "file:../js"
}
}
76 changes: 16 additions & 60 deletions src/chain-wallet-libs/bindings/wallet-js/js-test/test/vote_cast.js
@@ -1,79 +1,50 @@
var assert = require("assert");

const MAX_LANES = 8;

const private_key = Buffer.from(
"c86596c2d1208885db1fe3658406aa0f7cc7b8e13c362fe46a6db277fc5064583e487588c98a6c36e2e7445c0add36f83f171cb5ccfd815509d19cd38ecb0af3",
"hex"
);

const block0 = Buffer.from(
"000000000415000000000000000000000000fb92f9369b2588b899cbb60c72a7cd5b666302dacd40f6fa159fe4cf46910d750000000000000000000000000000000000000000000000000000000000000000000000ac0000000f0408000000005e922c70020101060200011c18000000000000000a000000000000000200000000000000640804000000b40a011420040000a8c0100800000000000000641204000190000c04000000641e04000000642c0101260800005af3107a40002821020000000000000064000000000000000d0000000000000013000000010000000316200258e06557efa50c2b94a585c49f45abf67ade94174e6ea6426d126ab36176a60000003500020000000000000000000105a6a3c0447aeb9cc54cf6422ba32b294e5e1c3ef6d782f2acff4a70694c4d166300000000000027100000003a000d040000000000a6a3c0447aeb9cc54cf6422ba32b294e5e1c3ef6d782f2acff4a70694c4d166300000000000f424000000001000000000000000002ea000a0000000000000000000000010000000000000002000000000111000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000400000000000000010000000000000258e06557efa50c2b94a585c49f45abf67ade94174e6ea6426d126ab36176a684c21a73ddd5b9d84d9d4a9a26a6c6f158100ff634f4850eb791464c622d1e44b9e8e0a95b6464541493227627b01ae58640f83eda755fe18589fd371a91100b",
"hex"
);
const settings_json =
'{"fees":{"constant":10,"coefficient":2,"certificate":100},"discrimination":"production","block0_initial_hash":{"hash":"baf6b54817cf2a3e865f432c3922d28ac5be641e66662c66d445f141e409183e"},"block0_date":1586637936,"slot_duration":20,"time_era":{"epoch_start":0,"slot_start":0,"slots_per_epoch":180},"transaction_max_expiry_epochs":1}';

function generate_wallet(wasm_wallet) {
let wallet = wasm_wallet.Wallet.import_keys(private_key);

let spending_counters = wasm_wallet.SpendingCounters.new();
for (let lane = 0; lane < MAX_LANES; lane++) {
let spending_counter = wasm_wallet.SpendingCounter.new(lane, 1);
spending_counters.add(spending_counter);
}

wallet.set_state(BigInt(1000), spending_counters);

let wallet = new wasm_wallet.Wallet(private_key, BigInt(1000));
assert(wallet.total_value() === BigInt(1000));

return wallet;
}

function generate_settings(wasm_wallet) {
let settings = wasm_wallet.Settings.new(block0);
return settings;
}

describe("vote cast certificate tests", function () {
it("public", async function () {
const wasm_wallet = await import("wallet-js");

let wallet = generate_wallet(wasm_wallet);
let settings = generate_settings(wasm_wallet);
let vote_plan = wasm_wallet.VotePlanId.from_bytes(
let settings = new wasm_wallet.Settings(settings_json);
let vote = wasm_wallet.Vote.public(
Buffer.from(
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"hex"
)
),
8,
0
);
let payload = wasm_wallet.Payload.new_public(0);
let vote_cast = wasm_wallet.VoteCast.new(vote_plan, 8, payload);

let block_date = wasm_wallet.BlockDate.new(0, 1);
let certificate = wasm_wallet.Certificate.vote_cast(vote_cast);
let fragment = wallet.sign_transaction(
settings,
block_date,
0,
certificate
);

wallet.confirm_transaction(fragment.id());
let fragments = wallet.signVotes([vote], settings, block_date, 0);
assert(fragments.length == 1);
});

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

let wallet = generate_wallet(wasm_wallet);
let settings = generate_settings(wasm_wallet);

let vote_plan = wasm_wallet.VotePlanId.from_bytes(
let settings = new wasm_wallet.Settings(settings_json);
let vote = wasm_wallet.Vote.private(
Buffer.from(
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"hex"
)
);
let payload = wasm_wallet.Payload.new_private(
vote_plan,
),
8,
4,
0,
Buffer.from(
Expand All @@ -82,23 +53,8 @@ describe("vote cast certificate tests", function () {
)
);

vote_plan = wasm_wallet.VotePlanId.from_bytes(
Buffer.from(
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"hex"
)
);
let vote_cast = wasm_wallet.VoteCast.new(vote_plan, 8, payload);

let block_date = wasm_wallet.BlockDate.new(0, 1);
let certificate = wasm_wallet.Certificate.vote_cast(vote_cast);
let fragment = wallet.sign_transaction(
settings,
block_date,
0,
certificate
);

wallet.confirm_transaction(fragment.id());
let fragments = wallet.signVotes([vote], settings, block_date, 0);
assert(fragments.length == 1);
});
});
3 changes: 3 additions & 0 deletions src/chain-wallet-libs/bindings/wallet-js/js/README.md
@@ -0,0 +1,3 @@
# Jormungandr wallet SDK for JavaScript

This package provide jormungandr wallet capabilities for Javascript

0 comments on commit e3cfd37

Please sign in to comment.