- Notifications
You must be signed in to change notification settings - Fork 1.8k
Migrate Tezos to Rust #4434
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
Open
gupnik wants to merge 9 commits into master Choose a base branch from gupnik/tezos-migration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
Migrate Tezos to Rust #4434
Changes from all commits
Commits
Show all changes
9 commits Select commit Hold shift + click to select a range
c37db47
[WIP]: Migrate Tezos to Rust
gupnik f220007
Adds operation list
gupnik c34267d
Adds signer and compiler
gupnik e83495a
Integrates Tezos in coin registry
gupnik d5a1ea1
Removes C++ files and updates tests
gupnik 5a4ea7f
Trigger Build
gupnik eef445c
Adds Message Signer back
gupnik 9140bd9
Minor
gupnik 7eac5e5
Addresses review comments
gupnik 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "tw_tezos" | ||
version = "0.1.0" | ||
edition = "2021" | ||
| ||
[dependencies] | ||
tw_base58_address = { path = "../../tw_base58_address" } | ||
tw_coin_entry = { path = "../../tw_coin_entry" } | ||
tw_encoding = { path = "../../tw_encoding" } | ||
tw_keypair = { path = "../../tw_keypair" } | ||
tw_hash = { path = "../../tw_hash" } | ||
tw_memory = { path = "../../tw_memory" } | ||
tw_misc = { path = "../../tw_misc", features = ["serde"] } | ||
tw_proto = { path = "../../tw_proto" } | ||
zeroize = "1.8.1" | ||
| ||
[dev-dependencies] | ||
tw_coin_entry = { path = "../../tw_coin_entry", features = ["test-utils"] } | ||
tw_number = { path = "../../tw_number", features = ["helpers"] } |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
| ||
use std::fmt; | ||
use std::str::FromStr; | ||
use tw_base58_address::Base58Address; | ||
use tw_coin_entry::coin_entry::CoinAddress; | ||
use tw_coin_entry::error::prelude::*; | ||
use tw_encoding::base58::Alphabet; | ||
use tw_hash::blake2::blake2_b; | ||
use tw_hash::sha2::Sha256d; | ||
use tw_hash::H160; | ||
use tw_keypair::{ | ||
ecdsa, ed25519, | ||
tw::{PublicKey, PublicKeyType}, | ||
}; | ||
use tw_memory::Data; | ||
| ||
use crate::forging::forge_public_key_hash; | ||
| ||
pub const TEZOS_ADDRESS_SIZE: usize = 23; | ||
pub const TEZOS_ADDRESS_PREFIX_SIZE: usize = 3; | ||
pub const TEZOS_ADDRESS_PUBLIC_KEY_HASH_SIZE: usize = 20; | ||
pub const TEZOS_ADDRESS_CHECKSUM_SIZE: usize = 4; | ||
| ||
pub const TEZOS_ADDRESS_SECP256K1_PREFIX: [u8; TEZOS_ADDRESS_PREFIX_SIZE] = [0x06, 0xa1, 0xa1]; | ||
pub const TEZOS_ADDRESS_ED25519_PREFIX: [u8; TEZOS_ADDRESS_PREFIX_SIZE] = [0x06, 0xa1, 0x9f]; | ||
pub const TEZOS_ADDRESS_OTHER_PREFIX: [u8; TEZOS_ADDRESS_PREFIX_SIZE] = [0x06, 0xa1, 0xa4]; | ||
pub const TEZOS_ADDRESS_KT1_PREFIX: [u8; TEZOS_ADDRESS_PREFIX_SIZE] = [0x02, 0x5a, 0x79]; | ||
| ||
pub const ACCOUNT_ZERO_BYTES: [u8; TEZOS_ADDRESS_SIZE] = [0; TEZOS_ADDRESS_SIZE]; | ||
| ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct TezosAddress(Base58Address<TEZOS_ADDRESS_SIZE, TEZOS_ADDRESS_CHECKSUM_SIZE, Sha256d>); | ||
| ||
impl TezosAddress { | ||
fn new(public_key_hash: &[u8], prefix: &[u8]) -> AddressResult<TezosAddress> { | ||
if prefix.len() != TEZOS_ADDRESS_PREFIX_SIZE { | ||
return Err(AddressError::InvalidInput); | ||
} | ||
if public_key_hash.len() != TEZOS_ADDRESS_PUBLIC_KEY_HASH_SIZE { | ||
return Err(AddressError::InvalidInput); | ||
} | ||
let mut bytes: Data = Vec::with_capacity(prefix.len() + public_key_hash.len()); | ||
bytes.extend_from_slice(prefix); | ||
bytes.extend_from_slice(public_key_hash); | ||
Base58Address::new(&bytes, Alphabet::Bitcoin).map(TezosAddress) | ||
} | ||
| ||
pub fn with_public_key(public_key: &PublicKey) -> AddressResult<TezosAddress> { | ||
if public_key.public_key_type() == PublicKeyType::Secp256k1 { | ||
TezosAddress::with_secp256k1_public_key( | ||
public_key | ||
.to_secp256k1() | ||
.ok_or(AddressError::PublicKeyTypeMismatch)?, | ||
) | ||
} else if public_key.public_key_type() == PublicKeyType::Ed25519 { | ||
TezosAddress::with_ed25519_public_key( | ||
public_key | ||
.to_ed25519() | ||
.ok_or(AddressError::PublicKeyTypeMismatch)?, | ||
) | ||
} else { | ||
Err(AddressError::InvalidInput) | ||
} | ||
} | ||
| ||
pub fn with_secp256k1_public_key( | ||
public_key: &ecdsa::secp256k1::PublicKey, | ||
) -> AddressResult<TezosAddress> { | ||
let bytes = blake2_b( | ||
public_key.compressed().as_slice(), | ||
TEZOS_ADDRESS_PUBLIC_KEY_HASH_SIZE, | ||
)?; | ||
TezosAddress::new(&bytes, &TEZOS_ADDRESS_SECP256K1_PREFIX) | ||
} | ||
| ||
pub fn with_ed25519_public_key( | ||
public_key: &ed25519::sha512::PublicKey, | ||
) -> AddressResult<TezosAddress> { | ||
let bytes = blake2_b(public_key.as_slice(), TEZOS_ADDRESS_PUBLIC_KEY_HASH_SIZE)?; | ||
TezosAddress::new(&bytes, &TEZOS_ADDRESS_ED25519_PREFIX) | ||
} | ||
| ||
/// Address bytes excluding the prefix (skip first 3 bytes). | ||
pub fn bytes(&self) -> &[u8] { | ||
&self.0.as_ref()[TEZOS_ADDRESS_PREFIX_SIZE..] | ||
} | ||
| ||
/// Returns public key hash associated with the address. | ||
pub fn public_key_hash(&self) -> H160 { | ||
H160::try_from(&self.0.as_ref()[TEZOS_ADDRESS_PREFIX_SIZE..]) | ||
.expect("Expected exactly 20 bytes public key hash") | ||
} | ||
| ||
pub fn forge(&self) -> AddressResult<Data> { | ||
// normal address | ||
// https://github.com/ecadlabs/taquito/blob/master/packages/taquito-local-forging/src/codec.ts#L183 | ||
let prefix = &self.0.bytes[..TEZOS_ADDRESS_PREFIX_SIZE]; | ||
if prefix == TEZOS_ADDRESS_SECP256K1_PREFIX | ||
|| prefix == TEZOS_ADDRESS_ED25519_PREFIX | ||
|| prefix == TEZOS_ADDRESS_OTHER_PREFIX | ||
{ | ||
let mut forged = vec![0x00]; | ||
let forged_public_key_hash = | ||
forge_public_key_hash(&self.to_string()).map_err(|_| AddressError::InvalidInput)?; | ||
forged.extend_from_slice(&forged_public_key_hash); | ||
Ok(forged) | ||
} | ||
// contract address | ||
// https://github.com/ecadlabs/taquito/blob/master/packages/taquito-local-forging/src/codec.ts#L183 | ||
else if prefix == TEZOS_ADDRESS_KT1_PREFIX { | ||
let mut forged = vec![0x01]; | ||
forged.extend_from_slice(self.bytes()); | ||
forged.push(0x00); | ||
Ok(forged) | ||
} else { | ||
Err(AddressError::InvalidInput) | ||
} | ||
} | ||
} | ||
| ||
impl Default for TezosAddress { | ||
fn default() -> Self { | ||
Base58Address::new(ACCOUNT_ZERO_BYTES.as_slice(), Alphabet::Bitcoin) | ||
.map(TezosAddress) | ||
.expect("'ACCOUNT_ZERO_BYTES' is expected to be valid address bytes") | ||
} | ||
} | ||
| ||
impl CoinAddress for TezosAddress { | ||
#[inline] | ||
fn data(&self) -> Data { | ||
self.bytes().to_vec() | ||
} | ||
} | ||
| ||
impl FromStr for TezosAddress { | ||
type Err = AddressError; | ||
| ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let base58_addr = Base58Address::<TEZOS_ADDRESS_SIZE, TEZOS_ADDRESS_CHECKSUM_SIZE, Sha256d>::from_str_with_alphabet(s, Alphabet::Bitcoin)?; | ||
let prefix = &base58_addr.bytes[..TEZOS_ADDRESS_PREFIX_SIZE]; | ||
if prefix != TEZOS_ADDRESS_SECP256K1_PREFIX | ||
&& prefix != TEZOS_ADDRESS_ED25519_PREFIX | ||
&& prefix != TEZOS_ADDRESS_OTHER_PREFIX | ||
&& prefix != TEZOS_ADDRESS_KT1_PREFIX | ||
{ | ||
return Err(AddressError::UnexpectedAddressPrefix); | ||
} | ||
Ok(TezosAddress(base58_addr)) | ||
} | ||
} | ||
| ||
impl fmt::Display for TezosAddress { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
| ||
use tw_encoding::{ | ||
base58::{self, Alphabet, CHECKSUM_LEN}, | ||
hex, EncodingError, | ||
}; | ||
use tw_hash::sha2::sha256_d; | ||
use tw_keypair::{ecdsa, ed25519, KeyPairError}; | ||
use tw_memory::Data; | ||
use zeroize::Zeroizing; | ||
| ||
use crate::address::TEZOS_ADDRESS_PREFIX_SIZE; | ||
| ||
const TEZOS_KEY_PREFIX_SIZE: usize = 4; | ||
| ||
pub fn decode_check(input: &str, alphabet: Alphabet) -> Result<Data, EncodingError> { | ||
let data = base58::decode(input, alphabet)?; | ||
if data.len() < CHECKSUM_LEN { | ||
return Err(EncodingError::InvalidInput); | ||
} | ||
| ||
let checksum = &data[data.len() - CHECKSUM_LEN..]; | ||
let hash = sha256_d(&data[..data.len() - CHECKSUM_LEN]); | ||
if checksum != &hash[..CHECKSUM_LEN] { | ||
return Err(EncodingError::InvalidInput); | ||
} | ||
Ok(Data::from(&data[..data.len() - CHECKSUM_LEN])) | ||
} | ||
| ||
pub fn encode_check(data: &[u8], alphabet: Alphabet) -> String { | ||
let hash = sha256_d(data); | ||
let mut to_be_encoded = Vec::from(data); | ||
to_be_encoded.extend_from_slice(&hash[..CHECKSUM_LEN]); | ||
base58::encode(&to_be_encoded, alphabet) | ||
} | ||
| ||
pub fn base58_to_hex(input: &str, prefix_length: usize) -> Result<String, EncodingError> { | ||
let decoded = match decode_check(input, Alphabet::Bitcoin) { | ||
Ok(d) => d, | ||
Err(_) => return Err(EncodingError::InvalidInput), | ||
}; | ||
| ||
if decoded.len() < prefix_length { | ||
return Err(EncodingError::InvalidInput); | ||
} | ||
| ||
Ok(hex::encode(&decoded[prefix_length..], false)) | ||
} | ||
| ||
pub fn parse_public_key(public_key: &str) -> Result<tw_keypair::tw::PublicKey, KeyPairError> { | ||
satoshiotomakan marked this conversation as resolved. Show resolved Hide resolved | ||
let decoded = | ||
decode_check(public_key, Alphabet::Bitcoin).map_err(|_| KeyPairError::InvalidPublicKey)?; | ||
| ||
let ed25519_prefix = [13, 15, 37, 217]; | ||
let secp256k1_prefix = [3, 254, 226, 86]; | ||
| ||
if decoded.starts_with(&ed25519_prefix) { | ||
if decoded.len() != 32 + TEZOS_KEY_PREFIX_SIZE { | ||
return Err(KeyPairError::InvalidPublicKey); | ||
} | ||
Ok(tw_keypair::tw::PublicKey::Ed25519( | ||
ed25519::sha512::PublicKey::try_from(&decoded[TEZOS_KEY_PREFIX_SIZE..])?, | ||
)) | ||
} else if decoded.starts_with(&secp256k1_prefix) { | ||
if decoded.len() != 33 + TEZOS_KEY_PREFIX_SIZE { | ||
return Err(KeyPairError::InvalidPublicKey); | ||
} | ||
Ok(tw_keypair::tw::PublicKey::Secp256k1( | ||
ecdsa::secp256k1::PublicKey::try_from(&decoded[TEZOS_KEY_PREFIX_SIZE..])?, | ||
)) | ||
} else { | ||
Err(KeyPairError::InvalidPublicKey) | ||
} | ||
} | ||
| ||
pub fn parse_private_key(private_key: &str) -> Result<tw_keypair::tw::PrivateKey, KeyPairError> { | ||
let decoded = Zeroizing::new( | ||
decode_check(private_key, Alphabet::Bitcoin).map_err(|_| KeyPairError::InvalidSecretKey)?, | ||
); | ||
| ||
if decoded.len() != 32 + TEZOS_KEY_PREFIX_SIZE { | ||
return Err(KeyPairError::InvalidSecretKey); | ||
} | ||
| ||
tw_keypair::tw::PrivateKey::new((decoded[TEZOS_KEY_PREFIX_SIZE..]).to_vec()) | ||
} | ||
| ||
pub fn encode_prefix(address: &str, forged: &mut Data) -> Result<(), EncodingError> { | ||
let decoded = decode_check(address, Alphabet::Bitcoin)?; | ||
forged.extend_from_slice(&decoded[TEZOS_ADDRESS_PREFIX_SIZE..]); | ||
Ok(()) | ||
} |
Oops, something went wrong.
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.