Skip to content

Commit

Permalink
describe witness types
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Jul 10, 2020
1 parent 89745af commit 3f7033c
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 28 deletions.
99 changes: 92 additions & 7 deletions rust/pkg/cardano_serialization_lib.js.flow
Expand Up @@ -7,15 +7,18 @@

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

/**
* @param {TransactionMetadata} metadata
* @returns {MetadataHash}
*/
declare export function hash_metadata(
metadata: TransactionMetadata
): MetadataHash;

/**
*/
Expand Down Expand Up @@ -736,6 +739,26 @@ declare export class Ipv6 {
*/
static new(data: Uint8Array): Ipv6;
}
declare export class LinearFee {
free(): void;

/**
* @returns {Coin}
*/
constant(): Coin;

/**
* @returns {Coin}
*/
coefficient(): Coin;

/**
* @param {Coin} constant
* @param {Coin} coefficient
* @returns {LinearFee}
*/
static new(constant: Coin, coefficient: Coin): LinearFee;
}
declare export class MapTransactionMetadatumToTransactionMetadatum {
free(): void;

Expand Down Expand Up @@ -1853,6 +1876,68 @@ declare export class TransactionBody {
*/
sign(sk: PrivateKey): Vkeywitness;
}
declare export class TransactionBuilder {
free(): void;

/**
* @param {AddrKeyHash} hash
* @param {TransactionInput} input
*/
add_key_input(hash: AddrKeyHash, input: TransactionInput): void;

/**
* @param {ScriptHash} hash
* @param {TransactionInput} input
*/
add_script_input(hash: ScriptHash, input: TransactionInput): void;

/**
* @param {string} hash
* @param {TransactionInput} input
*/
add_bootstrap_input(hash: string, input: TransactionInput): void;

/**
* @param {TransactionOutput} output
*/
add_output(output: TransactionOutput): void;

/**
* @param {Coin} fee
*/
set_fee(fee: Coin): void;

/**
* @param {Certificates} certs
*/
set_certs(certs: Certificates): void;

/**
* @param {Withdrawals} withdrawals
*/
set_withdrawals(withdrawals: Withdrawals): void;

/**
* @param {TransactionMetadata} metadata
*/
set_metadata(metadata: TransactionMetadata): void;

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

/**
* @returns {TransactionBody}
*/
build(): TransactionBody;

/**
* @param {LinearFee} linear_fee
* @returns {Coin}
*/
calc_fee(linear_fee: LinearFee): Coin;
}
declare export class TransactionHash {
free(): void;

Expand Down
73 changes: 52 additions & 21 deletions rust/src/crypto.rs
Expand Up @@ -284,6 +284,10 @@ impl Vkey {
pub fn new(pk: &PublicKey) -> Self {
Self(pk.clone())
}

pub fn public_key(&self) -> PublicKey {
self.0.clone()
}
}

impl cbor_event::se::Serialize for Vkey {
Expand Down Expand Up @@ -315,6 +319,14 @@ impl Vkeywitness {
signature: signature.clone()
}
}

pub fn vkey(&self) -> Vkey {
self.vkey.clone()
}

pub fn signature(&self) -> Ed25519Signature {
self.signature.clone()
}
}

impl cbor_event::se::Serialize for Vkeywitness {
Expand Down Expand Up @@ -402,28 +414,47 @@ impl Deserialize for Vkeywitnesses {
}
}

// TODO: custom-write the 3 byte objects and generally make this useable for more than deserialization
#[wasm_bindgen]
#[derive(Clone)]
pub struct BootstrapWitness {
vkey: Vkey,
signature: Ed25519Signature,
index_2: Vec<u8>,
index_3: Vec<u8>,
index_4: Vec<u8>,
chain_code: Vec<u8>,
pad_prefix: Vec<u8>,
pad_suffix: Vec<u8>,
}

to_from_bytes!(BootstrapWitness);

#[wasm_bindgen]
impl BootstrapWitness {
pub fn new(vkey: &Vkey, signature: &Ed25519Signature, index_2: Vec<u8>, index_3: Vec<u8>, index_4: Vec<u8>) -> Self {
pub fn vkey(&self) -> Vkey {
self.vkey.clone()
}

pub fn signature(&self) -> Ed25519Signature {
self.signature.clone()
}

pub fn chain_code(&self) -> Vec<u8> {
self.chain_code.clone()
}

pub fn pad_prefix(&self) -> Vec<u8> {
self.pad_prefix.clone()
}

pub fn pad_suffix(&self) -> Vec<u8> {
self.pad_suffix.clone()
}

pub fn new(vkey: &Vkey, signature: &Ed25519Signature, chain_code: Vec<u8>, pad_prefix: Vec<u8>, pad_suffix: Vec<u8>) -> Self {
Self {
vkey: vkey.clone(),
signature: signature.clone(),
index_2: index_2,
index_3: index_3,
index_4: index_4,
chain_code: chain_code,
pad_prefix: pad_prefix,
pad_suffix: pad_suffix,
}
}
}
Expand All @@ -433,9 +464,9 @@ impl cbor_event::se::Serialize for BootstrapWitness {
serializer.write_array(cbor_event::Len::Len(5))?;
self.vkey.serialize(serializer)?;
self.signature.serialize(serializer)?;
serializer.write_bytes(&self.index_2)?;
serializer.write_bytes(&self.index_3)?;
serializer.write_bytes(&self.index_4)?;
serializer.write_bytes(&self.chain_code)?;
serializer.write_bytes(&self.pad_prefix)?;
serializer.write_bytes(&self.pad_suffix)?;
Ok(serializer)
}
}
Expand All @@ -448,7 +479,7 @@ impl Deserialize for BootstrapWitness {
match len {
cbor_event::Len::Len(_) => /* TODO: check finite len somewhere */(),
cbor_event::Len::Indefinite => match raw.special()? {
cbor_event::Special::Break => /* it's ok */(),
CBORSpecial::Break => /* it's ok */(),
_ => return Err(DeserializeFailure::EndingBreakMissing.into()),
},
}
Expand All @@ -465,21 +496,21 @@ impl DeserializeEmbeddedGroup for BootstrapWitness {
let signature = (|| -> Result<_, DeserializeError> {
Ok(Ed25519Signature::deserialize(raw)?)
})().map_err(|e| e.annotate("signature"))?;
let index_2 = (|| -> Result<_, DeserializeError> {
let chain_code = (|| -> Result<_, DeserializeError> {
Ok(raw.bytes()?)
})().map_err(|e| e.annotate("index_2"))?;
let index_3 = (|| -> Result<_, DeserializeError> {
})().map_err(|e| e.annotate("chain_code"))?;
let pad_prefix = (|| -> Result<_, DeserializeError> {
Ok(raw.bytes()?)
})().map_err(|e| e.annotate("index_3"))?;
let index_4 = (|| -> Result<_, DeserializeError> {
})().map_err(|e| e.annotate("pad_prefix"))?;
let pad_suffix = (|| -> Result<_, DeserializeError> {
Ok(raw.bytes()?)
})().map_err(|e| e.annotate("index_4"))?;
})().map_err(|e| e.annotate("pad_suffix"))?;
Ok(BootstrapWitness {
vkey,
signature,
index_2,
index_3,
index_4,
chain_code,
pad_prefix,
pad_suffix,
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions rust/src/prelude.rs
Expand Up @@ -2,6 +2,9 @@ use cbor_event::{self, de::Deserializer, se::{Serialize, Serializer}};
use std::io::{BufRead, Seek, Write};
use wasm_bindgen::prelude::*;

// This file was code-generated using an experimental CDDL to rust tool:
// https://github.com/Emurgo/cddl-codegen

#[derive(Debug)]
pub enum Key {
Str(String),
Expand Down

0 comments on commit 3f7033c

Please sign in to comment.