Skip to content

Commit

Permalink
[rooch-networkgh-1552] refactor encoding and decoding, move to moveos…
Browse files Browse the repository at this point in the history
…_stdlib.
  • Loading branch information
Feliciss committed Apr 14, 2024
1 parent ef42210 commit 5cd97ab
Show file tree
Hide file tree
Showing 43 changed files with 821 additions and 864 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ enum_dispatch = "^0.3"
ethereum-types = "0.14.1"
ethers = { version = "2.0.7", features = ["legacy"] }
eyre = "0.6.8"
fastcrypto = { git = "https://github.com/rooch-network/fastcrypto", rev = "643831ec3b67bdd2b5f998c0bec1b7c91823351f" }
fastcrypto = { git = "https://github.com/rooch-network/fastcrypto", rev = "aa5f9f308b6598779820db8b673050c10cfcc3c1" }
futures = "0.3.28"
hex = "0.4.3"
rustc-hex = "1.0"
Expand Down
2 changes: 2 additions & 0 deletions frameworks/moveos-stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ itertools = { workspace = true }
ciborium = { workspace = true }
rlp = { workspace = true }
primitive-types = { workspace = true }
bech32 = { workspace = true }
bs58 = { workspace = true, features = ["check"] }

move-binary-format = { workspace = true }
move-bytecode-utils = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions frameworks/moveos-stdlib/doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ This is the reference documentation of the MoveOS standard library.
- [`0x2::address`](address.md#0x2_address)
- [`0x2::any`](any.md#0x2_any)
- [`0x2::bag`](bag.md#0x2_bag)
- [`0x2::base58`](base58.md#0x2_base58)
- [`0x2::bcs`](bcs.md#0x2_bcs)
- [`0x2::bech32`](bech32.md#0x2_bech32)
- [`0x2::big_vector`](big_vector.md#0x2_big_vector)
- [`0x2::copyable_any`](copyable_any.md#0x2_copyable_any)
- [`0x2::core_addresses`](core_addresses.md#0x2_core_addresses)
- [`0x2::display`](display.md#0x2_display)
- [`0x2::event`](event.md#0x2_event)
- [`0x2::features`](features.md#0x2_features)
- [`0x2::genesis`](genesis.md#0x2_genesis)
- [`0x2::hash`](hash.md#0x2_hash)
- [`0x2::hex`](hex.md#0x2_hex)
- [`0x2::json`](json.md#0x2_json)
- [`0x2::move_module`](move_module.md#0x2_move_module)
Expand Down
69 changes: 69 additions & 0 deletions frameworks/moveos-stdlib/doc/base58.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

<a name="0x2_base58"></a>

# Module `0x2::base58`

Module which defines base58 functions.


- [Function `encoding`](#0x2_base58_encoding)
- [Function `checksum_encoding`](#0x2_base58_checksum_encoding)
- [Function `decoding`](#0x2_base58_decoding)
- [Function `checksum_decoding`](#0x2_base58_checksum_decoding)


<pre><code></code></pre>



<a name="0x2_base58_encoding"></a>

## Function `encoding`

@param address_bytes: address bytes for base58 format
Encode the address bytes with Base58 algorithm and returns an encoded base58 bytes


<pre><code><b>public</b> <b>fun</b> <a href="base58.md#0x2_base58_encoding">encoding</a>(address_bytes: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<a name="0x2_base58_checksum_encoding"></a>

## Function `checksum_encoding`

@param address_bytes: address bytes on the base58 checksum format
@param version_byte: version byte used for verification of different types of checksum addresses
Encode the address bytes with Base58Check algorithm and returns an encoded base58 bytes with checksum


<pre><code><b>public</b> <b>fun</b> <a href="base58.md#0x2_base58_checksum_encoding">checksum_encoding</a>(address_bytes: &<a href="">vector</a>&lt;u8&gt;, version_byte: u8): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<a name="0x2_base58_decoding"></a>

## Function `decoding`

@param encoded_address_bytes: encoded base58 address bytes
Decode the base58 address bytes with Base58 algorithm and returns a raw base58 address bytes


<pre><code><b>public</b> <b>fun</b> <a href="base58.md#0x2_base58_decoding">decoding</a>(encoded_address_bytes: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<a name="0x2_base58_checksum_decoding"></a>

## Function `checksum_decoding`

@param encoded_address_bytes: encoded base58 address bytes
@param version_byte: version byte used for verification of different types of base58 addresses
Decode the base58 address bytes with Base58Check algorithm and returns a raw base58 address bytes without checksum


<pre><code><b>public</b> <b>fun</b> <a href="base58.md#0x2_base58_checksum_decoding">checksum_decoding</a>(encoded_address_bytes: &<a href="">vector</a>&lt;u8&gt;, version_byte: u8): <a href="">vector</a>&lt;u8&gt;
</code></pre>
40 changes: 40 additions & 0 deletions frameworks/moveos-stdlib/doc/bech32.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

<a name="0x2_bech32"></a>

# Module `0x2::bech32`

Module which defines bech32 functions.


- [Function `encoding`](#0x2_bech32_encoding)
- [Function `decoding`](#0x2_bech32_decoding)


<pre><code></code></pre>



<a name="0x2_bech32_encoding"></a>

## Function `encoding`

@param public_key: 20 or 32 bytes public keys
@param witness_version: 0 for bech32 encoding and 1-16 for bech32m encoding.
Encode the public keys with Bech32 or Bech32m encoding algorithm and returns 42 or 62 length Bech32 or Bech32m addresses.


<pre><code><b>public</b> <b>fun</b> <a href="bech32.md#0x2_bech32_encoding">encoding</a>(public_key: &<a href="">vector</a>&lt;u8&gt;, witness_version: u8): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<a name="0x2_bech32_decoding"></a>

## Function `decoding`

@param data: 42 or 62 length Bech32 or Bech32m address bytes
Decode the encoded 42 or 62 length Bech32 or Bech32m address bytes with Bech32 or Bech32m decoding algorithm and returns 20 or 32 bytes of public keys.


<pre><code><b>public</b> <b>fun</b> <a href="bech32.md#0x2_bech32_decoding">decoding</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>
79 changes: 79 additions & 0 deletions frameworks/moveos-stdlib/doc/hash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

<a name="0x2_hash"></a>

# Module `0x2::hash`

Module which defines hash functions. Note that Sha-256 and Sha3-256 is available in the std::hash module in the
Move standard library and wrap the functions at here.


- [Function `sha2_256`](#0x2_hash_sha2_256)
- [Function `sha3_256`](#0x2_hash_sha3_256)
- [Function `blake2b256`](#0x2_hash_blake2b256)
- [Function `keccak256`](#0x2_hash_keccak256)
- [Function `ripemd160`](#0x2_hash_ripemd160)


<pre><code><b>use</b> <a href="">0x1::hash</a>;
</code></pre>



<a name="0x2_hash_sha2_256"></a>

## Function `sha2_256`



<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_sha2_256">sha2_256</a>(data: <a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<a name="0x2_hash_sha3_256"></a>

## Function `sha3_256`



<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_sha3_256">sha3_256</a>(data: <a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<a name="0x2_hash_blake2b256"></a>

## Function `blake2b256`

@param data: Arbitrary binary data to hash
Hash the input bytes using Blake2b-256 and returns 32 bytes.


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_blake2b256">blake2b256</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<a name="0x2_hash_keccak256"></a>

## Function `keccak256`

@param data: Arbitrary binary data to hash
Hash the input bytes using keccak256 and returns 32 bytes.


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_keccak256">keccak256</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<a name="0x2_hash_ripemd160"></a>

## Function `ripemd160`

@param data: Arbitrary binary data to hash
Hash the input bytes using ripemd160 and returns 20 bytes.


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_ripemd160">ripemd160</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>
74 changes: 74 additions & 0 deletions frameworks/moveos-stdlib/sources/base58.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

/// Module which defines base58 functions.
module moveos_std::base58 {

/// @param address_bytes: address bytes for base58 format
/// Encode the address bytes with Base58 algorithm and returns an encoded base58 bytes
native public fun encoding(address_bytes: &vector<u8>): vector<u8>;

/// @param address_bytes: address bytes on the base58 checksum format
/// @param version_byte: version byte used for verification of different types of checksum addresses
/// Encode the address bytes with Base58Check algorithm and returns an encoded base58 bytes with checksum
native public fun checksum_encoding(address_bytes: &vector<u8>, version_byte: u8): vector<u8>;

/// @param encoded_address_bytes: encoded base58 address bytes
/// Decode the base58 address bytes with Base58 algorithm and returns a raw base58 address bytes
native public fun decoding(encoded_address_bytes: &vector<u8>): vector<u8>;

/// @param encoded_address_bytes: encoded base58 address bytes
/// @param version_byte: version byte used for verification of different types of base58 addresses
/// Decode the base58 address bytes with Base58Check algorithm and returns a raw base58 address bytes without checksum
native public fun checksum_decoding(encoded_address_bytes: &vector<u8>, version_byte: u8): vector<u8>;

#[test]
/// This test can be verified at http://lenschulwitz.com/base58.
fun test_encoding() {
let address_bytes = x"0062e907b15cbf27d5425399ebf6f0fb50ebb88f18c29b7d93";
let encoded_address_bytes = base58(&address_bytes);
let expected_encoded_address_bytes = b"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"; // Satoshi Nakamoto address with Base58 (P2PKH)

assert!(encoded_address_bytes == expected_encoded_address_bytes, 1000);
}

#[test]
/// checksum encoding adds 2 digits (1 bytes) checksum to the beginning of the hex string and 12 digits (6 bytes) checksum to the end of the hex string.
fun test_checksum_encoding() {
use std::vector;
let address_bytes = x"0062e907b15cbf27d5425399ebf6f0fb50ebb88f18";
let encoded_address_bytes = base58check(&address_bytes, 0); // Use script version 0 for verifying Base58 (P2PKH) address

let truncated_encoded_address_bytes = vector::empty<u8>();
let i = 1;
while (i < vector::length(&encoded_address_bytes) - 6) {
let value = vector::borrow(&encoded_address_bytes, i);
vector::push_back(&mut truncated_encoded_address_bytes, *value);
i = i + 1;
};

let expected_truncated_encoded_address_bytes = b"1A1zP1eP5QGefi2DMPTfTL5SLmv7"; // last 6 bytes are replaced with "CF73PR" and stripped.

assert!(truncated_encoded_address_bytes == expected_truncated_encoded_address_bytes, 1001);
}

#[test]
/// This test can be verified at http://lenschulwitz.com/base58.
fun test_decoding() {
let address_bytes = b"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"; // Satoshi Nakamoto address with Base58 (P2PKH)
let decoded_address_bytes = base58(&address_bytes);
let expected_decoded_address_bytes = x"0062e907b15cbf27d5425399ebf6f0fb50ebb88f18c29b7d93";

assert!(decoded_address_bytes == expected_decoded_address_bytes, 1002);
}

#[test]
/// checksum decoding removes the last 8 digits (4 bytes) checksum.
fun test_checksum_decoding() {
let address_bytes = b"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"; // Satoshi Nakamoto address with Base58 (P2PKH)
let decoded_address_bytes = base58check(&address_bytes, 0); // Base58 (P2PKH) is verified for script version 0
let expected_decoded_address_bytes = x"0062e907b15cbf27d5425399ebf6f0fb50ebb88f18";

assert!(decoded_address_bytes == expected_decoded_address_bytes, 1003);
}
}
Loading

0 comments on commit 5cd97ab

Please sign in to comment.