Skip to content

Commit

Permalink
Add bech32 conversion function to hashes (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Oct 22, 2020
1 parent 57cae64 commit 1cebd05
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
120 changes: 120 additions & 0 deletions rust/pkg/cardano_serialization_lib.js.flow
Expand Up @@ -525,6 +525,18 @@ declare export class BlockHash {
*/
to_bytes(): Uint8Array;

/**
* @param {string} prefix
* @returns {string}
*/
to_bech32(prefix: string): string;

/**
* @param {string} bech_str
* @returns {BlockHash}
*/
static from_bech32(bech_str: string): BlockHash;

/**
* @param {Uint8Array} bytes
* @returns {BlockHash}
Expand Down Expand Up @@ -833,6 +845,18 @@ declare export class Ed25519KeyHash {
*/
to_bytes(): Uint8Array;

/**
* @param {string} prefix
* @returns {string}
*/
to_bech32(prefix: string): string;

/**
* @param {string} bech_str
* @returns {Ed25519KeyHash}
*/
static from_bech32(bech_str: string): Ed25519KeyHash;

/**
* @param {Uint8Array} bytes
* @returns {Ed25519KeyHash}
Expand Down Expand Up @@ -952,6 +976,18 @@ declare export class GenesisDelegateHash {
*/
to_bytes(): Uint8Array;

/**
* @param {string} prefix
* @returns {string}
*/
to_bech32(prefix: string): string;

/**
* @param {string} bech_str
* @returns {GenesisDelegateHash}
*/
static from_bech32(bech_str: string): GenesisDelegateHash;

/**
* @param {Uint8Array} bytes
* @returns {GenesisDelegateHash}
Expand All @@ -968,6 +1004,18 @@ declare export class GenesisHash {
*/
to_bytes(): Uint8Array;

/**
* @param {string} prefix
* @returns {string}
*/
to_bech32(prefix: string): string;

/**
* @param {string} bech_str
* @returns {GenesisHash}
*/
static from_bech32(bech_str: string): GenesisHash;

/**
* @param {Uint8Array} bytes
* @returns {GenesisHash}
Expand Down Expand Up @@ -1299,6 +1347,18 @@ declare export class KESVKey {
*/
to_bytes(): Uint8Array;

/**
* @param {string} prefix
* @returns {string}
*/
to_bech32(prefix: string): string;

/**
* @param {string} bech_str
* @returns {KESVKey}
*/
static from_bech32(bech_str: string): KESVKey;

/**
* @param {Uint8Array} bytes
* @returns {KESVKey}
Expand Down Expand Up @@ -1391,6 +1451,18 @@ declare export class MetadataHash {
*/
to_bytes(): Uint8Array;

/**
* @param {string} prefix
* @returns {string}
*/
to_bech32(prefix: string): string;

/**
* @param {string} bech_str
* @returns {MetadataHash}
*/
static from_bech32(bech_str: string): MetadataHash;

/**
* @param {Uint8Array} bytes
* @returns {MetadataHash}
Expand Down Expand Up @@ -2780,6 +2852,18 @@ declare export class ScriptHash {
*/
to_bytes(): Uint8Array;

/**
* @param {string} prefix
* @returns {string}
*/
to_bech32(prefix: string): string;

/**
* @param {string} bech_str
* @returns {ScriptHash}
*/
static from_bech32(bech_str: string): ScriptHash;

/**
* @param {Uint8Array} bytes
* @returns {ScriptHash}
Expand Down Expand Up @@ -3365,6 +3449,18 @@ declare export class TransactionHash {
*/
to_bytes(): Uint8Array;

/**
* @param {string} prefix
* @returns {string}
*/
to_bech32(prefix: string): string;

/**
* @param {string} bech_str
* @returns {TransactionHash}
*/
static from_bech32(bech_str: string): TransactionHash;

/**
* @param {Uint8Array} bytes
* @returns {TransactionHash}
Expand Down Expand Up @@ -3868,6 +3964,18 @@ declare export class VRFKeyHash {
*/
to_bytes(): Uint8Array;

/**
* @param {string} prefix
* @returns {string}
*/
to_bech32(prefix: string): string;

/**
* @param {string} bech_str
* @returns {VRFKeyHash}
*/
static from_bech32(bech_str: string): VRFKeyHash;

/**
* @param {Uint8Array} bytes
* @returns {VRFKeyHash}
Expand All @@ -3884,6 +3992,18 @@ declare export class VRFVKey {
*/
to_bytes(): Uint8Array;

/**
* @param {string} prefix
* @returns {string}
*/
to_bech32(prefix: string): string;

/**
* @param {string} bech_str
* @returns {VRFVKey}
*/
static from_bech32(bech_str: string): VRFVKey;

/**
* @param {Uint8Array} bytes
* @returns {VRFVKey}
Expand Down
12 changes: 12 additions & 0 deletions rust/src/crypto.rs
Expand Up @@ -3,6 +3,7 @@ use crate::impl_mockchain as chain;
use crate::chain_crypto as crypto;
use chain::{key};
use crypto::bech32::Bech32 as _;
use bech32::ToBase32;
use rand_os::OsRng;
use std::io::{BufRead, Seek, Write};
use std::str::FromStr;
Expand Down Expand Up @@ -679,6 +680,17 @@ macro_rules! impl_hash_type {
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_vec()
}

pub fn to_bech32(&self, prefix: &str) -> Result<String, JsError> {
bech32::encode(&prefix, self.to_bytes().to_base32())
.map_err(|e| JsError::from_str(&format! {"{:?}", e}))
}

pub fn from_bech32(bech_str: &str) -> Result<$name, JsError> {
let (_hrp, u5data) = bech32::decode(bech_str).map_err(|e| JsError::from_str(&e.to_string()))?;
let data: Vec<u8> = bech32::FromBase32::from_base32(&u5data).unwrap();
Ok(Self::from_bytes(data)?)
}
}

from_bytes!($name, bytes, {
Expand Down

0 comments on commit 1cebd05

Please sign in to comment.