Skip to content

Commit

Permalink
Add keys() method to map structs (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
rooooooooob committed Aug 8, 2020
1 parent ae9343b commit d0e4a36
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 14 deletions.
151 changes: 144 additions & 7 deletions rust/pkg/cardano_serialization_lib.js.flow
Expand Up @@ -5,6 +5,13 @@
* @flow
*/

/**
* @param {Transaction} tx
* @param {LinearFee} linear_fee
* @returns {BigNum}
*/
declare export function min_fee(tx: Transaction, linear_fee: LinearFee): BigNum;

/**
* @param {TransactionHash} tx_body_hash
* @param {ByronAddress} addr
Expand Down Expand Up @@ -67,13 +74,6 @@ declare export function get_implicit_input(
key_deposit: BigNum
): BigNum;

/**
* @param {Transaction} tx
* @param {LinearFee} linear_fee
* @returns {BigNum}
*/
declare export function min_fee(tx: Transaction, linear_fee: LinearFee): BigNum;

/**
*/

Expand Down Expand Up @@ -1016,6 +1016,11 @@ declare export class MapTransactionMetadatumToTransactionMetadatum {
* @returns {TransactionMetadatum | void}
*/
get(key: TransactionMetadatum): TransactionMetadatum | void;

/**
* @returns {TransactionMetadatums}
*/
keys(): TransactionMetadatums;
}
/**
*/
Expand Down Expand Up @@ -1066,6 +1071,17 @@ declare export class MoveInstantaneousReward {
* @returns {BigNum | void}
*/
insert(key: StakeCredential, value: BigNum): BigNum | void;

/**
* @param {StakeCredential} key
* @returns {BigNum | void}
*/
get(key: StakeCredential): BigNum | void;

/**
* @returns {StakeCredentials}
*/
keys(): StakeCredentials;
}
/**
*/
Expand Down Expand Up @@ -1824,6 +1840,43 @@ declare export class RewardAddress {
*/
static from_address(addr: Address): RewardAddress | void;
}
/**
*/
declare export class RewardAddresses {
free(): void;

/**
* @returns {Uint8Array}
*/
to_bytes(): Uint8Array;

/**
* @param {Uint8Array} bytes
* @returns {RewardAddresses}
*/
static from_bytes(bytes: Uint8Array): RewardAddresses;

/**
* @returns {RewardAddresses}
*/
static new(): RewardAddresses;

/**
* @returns {number}
*/
len(): number;

/**
* @param {number} index
* @returns {RewardAddress}
*/
get(index: number): RewardAddress;

/**
* @param {RewardAddress} elem
*/
add(elem: RewardAddress): void;
}
/**
*/
declare export class ScriptHash {
Expand Down Expand Up @@ -1955,6 +2008,43 @@ declare export class StakeCredential {
*/
static from_bytes(bytes: Uint8Array): StakeCredential;
}
/**
*/
declare export class StakeCredentials {
free(): void;

/**
* @returns {Uint8Array}
*/
to_bytes(): Uint8Array;

/**
* @param {Uint8Array} bytes
* @returns {StakeCredentials}
*/
static from_bytes(bytes: Uint8Array): StakeCredentials;

/**
* @returns {StakeCredentials}
*/
static new(): StakeCredentials;

/**
* @returns {number}
*/
len(): number;

/**
* @param {number} index
* @returns {StakeCredential}
*/
get(index: number): StakeCredential;

/**
* @param {StakeCredential} elem
*/
add(elem: StakeCredential): void;
}
/**
*/
declare export class StakeDelegation {
Expand Down Expand Up @@ -2416,6 +2506,11 @@ declare export class TransactionMetadata {
* @returns {TransactionMetadatum | void}
*/
get(key: BigNum): TransactionMetadatum | void;

/**
* @returns {TransactionMetadatumLabels}
*/
keys(): TransactionMetadatumLabels;
}
/**
*/
Expand Down Expand Up @@ -2497,6 +2592,43 @@ declare export class TransactionMetadatum {
*/
as_text(): string | void;
}
/**
*/
declare export class TransactionMetadatumLabels {
free(): void;

/**
* @returns {Uint8Array}
*/
to_bytes(): Uint8Array;

/**
* @param {Uint8Array} bytes
* @returns {TransactionMetadatumLabels}
*/
static from_bytes(bytes: Uint8Array): TransactionMetadatumLabels;

/**
* @returns {TransactionMetadatumLabels}
*/
static new(): TransactionMetadatumLabels;

/**
* @returns {number}
*/
len(): number;

/**
* @param {number} index
* @returns {BigNum}
*/
get(index: number): BigNum;

/**
* @param {BigNum} elem
*/
add(elem: BigNum): void;
}
/**
*/
declare export class TransactionMetadatums {
Expand Down Expand Up @@ -2828,4 +2960,9 @@ declare export class Withdrawals {
* @returns {BigNum | void}
*/
get(key: RewardAddress): BigNum | void;

/**
* @returns {RewardAddresses}
*/
keys(): RewardAddresses;
}
14 changes: 7 additions & 7 deletions rust/src/crypto.rs
Expand Up @@ -97,8 +97,8 @@ impl Bip32PrivateKey {
}

pub fn chaincode(&self) -> Vec<u8> {
let ED25519_PRIVATE_KEY_LENGTH = 64;
let XPRV_SIZE = 96;
const ED25519_PRIVATE_KEY_LENGTH: usize = 64;
const XPRV_SIZE: usize = 96;
self.0.as_ref()[ED25519_PRIVATE_KEY_LENGTH..XPRV_SIZE].to_vec()
}
}
Expand Down Expand Up @@ -163,8 +163,8 @@ impl Bip32PublicKey {
}

pub fn chaincode(&self) -> Vec<u8> {
let ED25519_PRIVATE_KEY_LENGTH = 64;
let XPRV_SIZE = 96;
const ED25519_PRIVATE_KEY_LENGTH: usize = 64;
const XPRV_SIZE: usize = 96;
self.0.as_ref()[ED25519_PRIVATE_KEY_LENGTH..XPRV_SIZE].to_vec()
}
}
Expand Down Expand Up @@ -359,7 +359,7 @@ impl Deserialize for Vkeywitness {
match len {
cbor_event::Len::Len(n) => match n {
2 => (),
m => return Err(DeserializeFailure::CBOR(cbor_event::Error::WrongLen(2, len, "")).into()),
_ => return Err(DeserializeFailure::CBOR(cbor_event::Error::WrongLen(2, len, "")).into()),
},
cbor_event::Len::Indefinite => match raw.special()? {
cbor_event::Special::Break => /* it's ok */(),
Expand Down Expand Up @@ -715,8 +715,8 @@ impl LegacyDaedalusPrivateKey {
}

pub fn chaincode(&self) -> Vec<u8> {
let ED25519_PRIVATE_KEY_LENGTH = 64;
let XPRV_SIZE = 96;
const ED25519_PRIVATE_KEY_LENGTH: usize = 64;
const XPRV_SIZE: usize = 96;
self.0.as_ref()[ED25519_PRIVATE_KEY_LENGTH..XPRV_SIZE].to_vec()
}
}
Expand Down
96 changes: 96 additions & 0 deletions rust/src/lib.rs
Expand Up @@ -732,6 +732,14 @@ impl MoveInstantaneousReward {
pub fn insert(&mut self, key: &StakeCredential, value: &Coin) -> Option<Coin> {
self.rewards.insert(key.clone(), value.clone())
}

pub fn get(&self, key: &StakeCredential) -> Option<Coin> {
self.rewards.get(key).map(|v| v.clone())
}

pub fn keys(&self) -> StakeCredentials {
StakeCredentials(self.rewards.iter().map(|(k, _v)| k.clone()).collect::<Vec<StakeCredential>>())
}
}

type Port = u16;
Expand Down Expand Up @@ -939,6 +947,57 @@ impl PoolMetadata {

type Url = String;

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct StakeCredentials(Vec<StakeCredential>);

to_from_bytes!(StakeCredentials);

#[wasm_bindgen]
impl StakeCredentials {
pub fn new() -> Self {
Self(Vec::new())
}

pub fn len(&self) -> usize {
self.0.len()
}

pub fn get(&self, index: usize) -> StakeCredential {
self.0[index].clone()
}

pub fn add(&mut self, elem: &StakeCredential) {
self.0.push(elem.clone());
}
}


#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct RewardAddresses(Vec<RewardAddress>);

to_from_bytes!(RewardAddresses);

#[wasm_bindgen]
impl RewardAddresses {
pub fn new() -> Self {
Self(Vec::new())
}

pub fn len(&self) -> usize {
self.0.len()
}

pub fn get(&self, index: usize) -> RewardAddress {
self.0[index].clone()
}

pub fn add(&mut self, elem: &RewardAddress) {
self.0.push(elem.clone());
}
}

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Withdrawals(std::collections::BTreeMap<RewardAddress, Coin>);
Expand All @@ -962,6 +1021,10 @@ impl Withdrawals {
pub fn get(&self, key: &RewardAddress) -> Option<Coin> {
self.0.get(key).map(|v| v.clone())
}

pub fn keys(&self) -> RewardAddresses {
RewardAddresses(self.0.iter().map(|(k, _v)| k.clone()).collect::<Vec<RewardAddress>>())
}
}

#[wasm_bindgen]
Expand Down Expand Up @@ -1057,6 +1120,10 @@ impl MapTransactionMetadatumToTransactionMetadatum {
pub fn get(&self, key: &TransactionMetadatum) -> Option<TransactionMetadatum> {
self.0.get(key).map(|v| v.clone())
}

pub fn keys(&self) -> TransactionMetadatums {
TransactionMetadatums(self.0.iter().map(|(k, _v)| k.clone()).collect::<Vec<TransactionMetadatum>>())
}
}

#[wasm_bindgen]
Expand Down Expand Up @@ -1179,6 +1246,31 @@ impl TransactionMetadatum {

type TransactionMetadatumLabel = BigNum;

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct TransactionMetadatumLabels(Vec<TransactionMetadatumLabel>);

to_from_bytes!(TransactionMetadatumLabels);

#[wasm_bindgen]
impl TransactionMetadatumLabels {
pub fn new() -> Self {
Self(Vec::new())
}

pub fn len(&self) -> usize {
self.0.len()
}

pub fn get(&self, index: usize) -> TransactionMetadatumLabel {
self.0[index].clone()
}

pub fn add(&mut self, elem: &TransactionMetadatumLabel) {
self.0.push(elem.clone());
}
}

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct TransactionMetadata(std::collections::BTreeMap<TransactionMetadatumLabel, TransactionMetadatum>);
Expand All @@ -1202,6 +1294,10 @@ impl TransactionMetadata {
pub fn get(&self, key: &TransactionMetadatumLabel) -> Option<TransactionMetadatum> {
self.0.get(key).map(|v| v.clone())
}

pub fn keys(&self) -> TransactionMetadatumLabels {
TransactionMetadatumLabels(self.0.iter().map(|(k, _v)| k.clone()).collect::<Vec<TransactionMetadatumLabel>>())
}
}

#[wasm_bindgen]
Expand Down

0 comments on commit d0e4a36

Please sign in to comment.