forked from rooch-network/rooch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rooch-networkgh-1552] refactor encoding and decoding, move to moveos…
…_stdlib.
- Loading branch information
Feliciss
committed
Apr 14, 2024
1 parent
ef42210
commit 5cd97ab
Showing
43 changed files
with
821 additions
and
864 deletions.
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 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 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 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 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,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><u8>): <a href="">vector</a><u8> | ||
</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><u8>, version_byte: u8): <a href="">vector</a><u8> | ||
</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><u8>): <a href="">vector</a><u8> | ||
</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><u8>, version_byte: u8): <a href="">vector</a><u8> | ||
</code></pre> |
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 |
---|---|---|
@@ -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><u8>, witness_version: u8): <a href="">vector</a><u8> | ||
</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><u8>): <a href="">vector</a><u8> | ||
</code></pre> |
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 |
---|---|---|
@@ -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><u8>): <a href="">vector</a><u8> | ||
</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><u8>): <a href="">vector</a><u8> | ||
</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><u8>): <a href="">vector</a><u8> | ||
</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><u8>): <a href="">vector</a><u8> | ||
</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><u8>): <a href="">vector</a><u8> | ||
</code></pre> |
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.