-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[drft] drp xtndd #700
Closed
Closed
[drft] drp xtndd #700
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
use core::prelude::v1::Ok; | ||
use crate::*; | ||
use bech32::ToBase32; | ||
|
||
|
@@ -29,6 +30,93 @@ pub enum DRepKind { | |
AlwaysNoConfidence, | ||
} | ||
|
||
#[wasm_bindgen] | ||
#[derive(Clone, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)] | ||
pub enum DRepExtendedId { | ||
Key(Ed25519KeyHash), | ||
Script(ScriptHash), | ||
} | ||
|
||
const DREP_CIP129_PREFIX_KEY: u8 = 34; | ||
const DREP_CIP129_PREFIX_SCRIPT: u8 = 35; | ||
|
||
#[wasm_bindgen] | ||
impl DRepExtendedId { | ||
|
||
pub fn from_bytes(data: &Vec<u8>) -> Result<DRepExtendedId, JsError> { | ||
if data.len() != 29 { | ||
return Err(JsError::from_str("Malformed DRep Extended ID (incorrect len)")); | ||
} | ||
let prefix = data.get(0) | ||
.ok_or(JsError::from_str("Malformed DRep (unexpected failure to get bytes prefix)"))?; | ||
match prefix { | ||
&DREP_CIP129_PREFIX_KEY => return Ok( | ||
DRepExtendedId::Key( | ||
Ed25519KeyHash::from_bytes(data[1..].to_vec()) | ||
.map_err(|_| JsError::from_str("Malformed DRep KeyHash"))?, | ||
) | ||
), | ||
&DREP_CIP129_PREFIX_SCRIPT => return Ok( | ||
DRepExtendedId::Script( | ||
ScriptHash::from_bytes(data[1..].to_vec()) | ||
.map_err(|_| JsError::from_str("Malformed DRep ScriptHash"))?, | ||
) | ||
), | ||
_ => return Err(JsError::from_str("Malformed DRep Extended ID (incorrect prefix byte)")) | ||
} | ||
} | ||
|
||
pub fn from_hex(hex_str: &str) -> Result<DRepExtendedId, JsError> { | ||
DRepExtendedId::from_bytes( | ||
&hex::decode(hex_str) | ||
.map_err(|e| JsError::from_str(&e.to_string()))? | ||
) | ||
} | ||
|
||
pub fn from_bech32(bech32_str: &str) -> Result<DRepExtendedId, JsError> { | ||
let (hrp, u5data) = | ||
bech32::decode(bech32_str).map_err(|e| JsError::from_str(&e.to_string()))?; | ||
if hrp != "drep" { | ||
return Err(JsError::from_str("Malformed DRep Extended ID (incorrect prefix)")); | ||
} | ||
let data: Vec<u8> = bech32::FromBase32::from_base32(&u5data) | ||
.map_err(|e: bech32::Error| JsError::from_str(&format!("Malformed DRep base32: {}", &e.to_string())))?; | ||
DRepExtendedId::from_bytes(&data) | ||
} | ||
|
||
pub fn to_hex(&self) -> String { | ||
hex::encode(self.to_bytes()) | ||
} | ||
|
||
pub fn to_bytes(&self) -> Vec<u8> { | ||
let (prefix, data) = match self { | ||
DRepExtendedId::Key(keyhash) => (DREP_CIP129_PREFIX_KEY, keyhash.to_bytes()), | ||
DRepExtendedId::Script(scripthash) => (DREP_CIP129_PREFIX_SCRIPT, scripthash.to_bytes()), | ||
}; | ||
let mut res = vec![prefix]; | ||
res.extend(data); | ||
res | ||
} | ||
|
||
pub fn to_bech32(&self) -> Result<String, JsError> { | ||
bech32::encode("drep", self.to_bytes().to_base32()).map_err(|e| JsError::from_str(&format! {"{:?}", e})) | ||
} | ||
|
||
pub fn kind(&self) -> DRepKind { | ||
match self { | ||
DRepExtendedId::Key(_) => DRepKind::KeyHash, | ||
DRepExtendedId::Script(_) => DRepKind::ScriptHash, | ||
} | ||
} | ||
|
||
pub fn to_drep(&self) -> DRep { | ||
match self { | ||
DRepExtendedId::Key(key) => DRep(DRepEnum::KeyHash(key.clone())), | ||
DRepExtendedId::Script(script) => DRep(DRepEnum::ScriptHash(script.clone())), | ||
} | ||
} | ||
} | ||
|
||
#[derive( | ||
Clone, | ||
Debug, | ||
|
@@ -95,9 +183,17 @@ impl DRep { | |
} | ||
} | ||
|
||
pub fn to_bech32(&self) -> Result<String, JsError> { | ||
pub fn to_extended_id(&self) -> Option<DRepExtendedId> { | ||
match &self.0 { | ||
DRepEnum::KeyHash(x) => Some(DRepExtendedId::Key(x.clone())), | ||
DRepEnum::ScriptHash(x) => Some(DRepExtendedId::Script(x.clone())), | ||
_ => None | ||
} | ||
} | ||
|
||
fn internal_to_bech32(&self, vkh_prefix: String) -> Result<String, JsError> { | ||
let (hrp, data) = match &self.0 { | ||
DRepEnum::KeyHash(keyhash) => Ok(("drep", keyhash.to_bytes())), | ||
DRepEnum::KeyHash(keyhash) => Ok((vkh_prefix.as_str(), keyhash.to_bytes())), | ||
DRepEnum::ScriptHash(scripthash) => Ok(("drep_script", scripthash.to_bytes())), | ||
DRepEnum::AlwaysAbstain => { | ||
Err(JsError::from_str("Cannot convert AlwaysAbstain to bech32")) | ||
|
@@ -109,23 +205,36 @@ impl DRep { | |
bech32::encode(&hrp, data.to_base32()).map_err(|e| JsError::from_str(&format! {"{:?}", e})) | ||
} | ||
|
||
pub fn to_bech32(&self) -> Result<String, JsError> { | ||
self.internal_to_bech32("drep".to_string()) | ||
} | ||
|
||
pub fn to_bech32_cip129(&self) -> Result<String, JsError> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably we need to encode only in cip129 format and do not provide API to encode in a old format when cip129 will be merged |
||
self.internal_to_bech32("drep_vkh".to_string()) | ||
} | ||
|
||
pub fn from_bech32(bech32_str: &str) -> Result<DRep, JsError> { | ||
let (hrp, u5data) = | ||
bech32::decode(bech32_str).map_err(|e| JsError::from_str(&e.to_string()))?; | ||
let data: Vec<u8> = bech32::FromBase32::from_base32(&u5data) | ||
.map_err(|_| JsError::from_str("Malformed DRep"))?; | ||
.map_err(|e: bech32::Error| JsError::from_str(&format!("Malformed DRep base32: {}", &e.to_string())))?; | ||
let kind = match hrp.as_str() { | ||
"drep" => DRepKind::KeyHash, | ||
"drep" => match data.len() { | ||
28 => DRepKind::KeyHash, // pre cip129 compatibility | ||
29 => return DRepExtendedId::from_bech32(bech32_str).and_then(|id| Ok(id.to_drep())), | ||
_ => return Err(JsError::from_str("Malformed DRep (drep1 byte len)")) | ||
}, | ||
"drep_vkh" => DRepKind::KeyHash, | ||
"drep_script" => DRepKind::ScriptHash, | ||
_ => return Err(JsError::from_str("Malformed DRep")), | ||
_ => return Err(JsError::from_str("Malformed DRep (bech prefix)")), | ||
}; | ||
let drep = match kind { | ||
DRepKind::KeyHash => DRepEnum::KeyHash( | ||
Ed25519KeyHash::from_bytes(data) | ||
.map_err(|_| JsError::from_str("Malformed DRep"))?, | ||
.map_err(|_| JsError::from_str("Malformed DRep KeyHash"))?, | ||
), | ||
DRepKind::ScriptHash => DRepEnum::ScriptHash( | ||
ScriptHash::from_bytes(data).map_err(|_| JsError::from_str("Malformed DRep"))?, | ||
ScriptHash::from_bytes(data).map_err(|_| JsError::from_str("Malformed DRep ScriptHash"))?, | ||
), | ||
DRepKind::AlwaysAbstain => DRepEnum::AlwaysAbstain, | ||
DRepKind::AlwaysNoConfidence => DRepEnum::AlwaysNoConfidence, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My suggestion to fully implement CIP-129 GovernanceIdentifier to not have N implementations of different things that just are subset of GovernanceIdentifier