From c0c8a3dddd112db825a336329ce2effa45f146b8 Mon Sep 17 00:00:00 2001 From: STerliakov Date: Tue, 19 Dec 2023 19:54:32 +0400 Subject: [PATCH 1/3] Add cargo-rdme --- .github/workflows/test.yml | 5 ++- Cargo.toml | 1 + README.md | 44 ++----------------------- src/lib.rs | 67 ++++++++++++++++++++++++++++++++------ src/transactions.rs | 23 +++++++------ tests/test_hdnode.rs | 5 ++- tests/test_transactions.rs | 2 +- 7 files changed, 80 insertions(+), 67 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dedd0f3..f8c9e1a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,7 +23,10 @@ jobs: - uses: taiki-e/install-action@v2 with: - tool: cargo-tarpaulin + tool: cargo-tarpaulin,cargo-rdme + + - name: Check README is up-to-date + run: cargo rdme --check - name: Run tests with coverage run: RUST_BACKTRACE=1 cargo tarpaulin --out Xml diff --git a/Cargo.toml b/Cargo.toml index 5c0652b..0b30e05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ rustc-hex = "2.1.0" [dev-dependencies] # version_sync: to ensure versions in `Cargo.toml` and `README.md` are in sync version-sync = "0.9.4" + # cargo-bump: to bump package version and tag a commit at the same time. # actually, the docs recommend installing this globally: # $ git clone https://github.com/rnag/cargo-bump && cd cargo-bump && cargo install --path . && cd .. && rm -rf cargo-bump diff --git a/README.md b/README.md index 828b06a..e48cb83 100644 --- a/README.md +++ b/README.md @@ -3,49 +3,9 @@ [github](https://github.com/sterliakov/thor-devkit) [crates.io](https://crates.io/crates/thor-devkit) [docs.rs](https://docs.rs/thor-devkit) -[build status](https://github.com/sterliakov/thor-devkit/actions?query=branch%3Amain) +[build status](https://github.com/sterliakov/thor-devkit/actions?query=branch%3Amain) -Rust library to aid coding with VeChain, eg. Wallets/Tx/Sign/Verify. - -## Getting started - -Add some usage to your application. - -Here's an example of using `thor-devkit` in code: - -```rust -use thor_devkit.rs::*; - -#[tokio::main] -async fn main() -> Result<(), Box> { - println!("Hello world!"); - - Ok(()) -} -``` - -## Examples - -You can check out sample usage of this crate in the [examples/](https://github.com/sterliakov/thor-devkit/tree/main/examples) -folder in the project repo on GitHub. - -## Contributing - -Contributions are welcome! Open a pull request to fix a bug, or [open an issue][] -to discuss a new feature or change. - -Check out the [Contributing][] section in the docs for more info. - -[Contributing]: CONTRIBUTING.md -[open an issue]: https://github.com/sterliakov/thor-devkit/issues - -## License - -This project is proudly licensed under the GNU General Public License v3 ([LICENSE](LICENSE) -or http://opensource.org/licenses/MIT). - -`thor-devkit` can be distributed according to the GNU General Public License v3. Contributions -will be accepted under the same license. + ## Authors diff --git a/src/lib.rs b/src/lib.rs index 3d2ee93..85f523a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,22 +2,52 @@ #![warn(rust_2018_idioms, missing_docs)] #![deny(dead_code, unused_imports, unused_mut)] -//! [![github]](https://github.com/sterliakov/thor-devkit) [![crates-io]](https://crates.io/crates/thor-devkit) [![docs-rs]](https://docs.rs/thor-devkit) +//! Rust library to aid coding with VeChain, eg. Wallets/Tx/Sign/Verify. //! -//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github -//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust -//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K +//! This library acts primary as a proxy to several underlying libraries, +//! with the addition of some VeChain-specific toolchain components. //! -//!
+//! ## Usage //! -//! Rust library to aid coding with VeChain, eg. Wallets/Tx/Sign/Verify. +//! One of possible use cases can be transaction creation and signing. //! -//!
+//! Here is how you may approach it. Let's transfer a few VET to another account. //! -//! ## Usage +//! To do so, we need to create a transaction and encode it into broadcastable bytes. //! -//! ``` -//! fn main() {} +//! ```rust +//! use thor_devkit::transactions::{Transaction, Clause}; +//! use thor_devkit::hdnode::{Mnemonic, Language, HDNode}; +//! +//! let transaction = Transaction { +//! chain_tag: 1, +//! block_ref: 0xaabbccdd, +//! expiration: 32, +//! clauses: vec![ +//! Clause { +//! to: Some( +//! "0x7567d83b7b8d80addcb281a71d54fc7b3364ffed" +//! .parse() +//! .unwrap(), +//! ), +//! value: 10000.into(), +//! data: b"\x00\x00\x00\x60\x60\x60".to_vec().into(), +//! }, +//! ], +//! gas_price_coef: 128, +//! gas: 21000, +//! depends_on: None, +//! nonce: 0xbc614e, +//! reserved: None, +//! signature: None, +//! }; +//! let mnemonic = Mnemonic::from_phrase( +//! "ignore empty bird silly journey junior ripple have guard waste between tenant", +//! Language::English +//! ).expect("Must be correct"); +//! let wallet = HDNode::build().mnemonic(mnemonic).build().expect("Builds"); +//! let signed = transaction.sign(&wallet.private_key().expect("Must be non-restricted").private_key()); +//! println!("{:02x?}", signed.to_broadcastable_bytes()); //! ``` //! //! ## Examples @@ -33,6 +63,23 @@ //! [crates.io]: https://crates.io/crates/thor-devkit //! [`README.md`]: https://github.com/sterliakov/thor-devkit //! +//! +//! ## Contributing +//! +//! Contributions are welcome! Open a pull request to fix a bug, or [open an issue][] +//! to discuss a new feature or change. +//! +//! Check out the [Contributing][] section in the docs for more info. +//! +//! [Contributing]: CONTRIBUTING.md +//! [open an issue]: https://github.com/sterliakov/thor-devkit/issues +//! +//! ## License +//! +//! This project is proudly licensed under the GNU General Public License v3 ([LICENSE](LICENSE). +//! +//! `thor-devkit` can be distributed according to the GNU General Public License v3. Contributions +//! will be accepted under the same license. pub mod address; pub mod hdnode; diff --git a/src/transactions.rs b/src/transactions.rs index df2cf09..4c15239 100644 --- a/src/transactions.rs +++ b/src/transactions.rs @@ -316,16 +316,6 @@ impl Transaction { } } - pub fn signature(&self) -> Option { - //! Signature. 65 bytes for regular transactions, 130 - for VIP-191. - //! - //! Ignored when making a signing hash. - //! - //! For VIP-191 transactions, this would be a simple concatenation - //! of two signatures. - self.signature.clone() - } - pub fn has_valid_signature(&self) -> bool { //! Check wheter the signature is valid. self._has_valid_signature().unwrap_or(false) @@ -353,6 +343,19 @@ impl Transaction { } } } + + pub fn to_broadcastable_bytes(&self) -> Result { + //! Create a binary representation. + //! + //! Returns `Err(secp256k1::Error::IncorrectSignature)` if signature is not set. + if self.signature.is_some() { + let mut buf = alloy_rlp::BytesMut::new(); + self.encode(&mut buf); + Ok(buf.into()) + } else { + Err(secp256k1::Error::IncorrectSignature) + } + } } /// Represents a single transaction clause (recipient, value and data). diff --git a/tests/test_hdnode.rs b/tests/test_hdnode.rs index 7a5321b..72bbfa4 100644 --- a/tests/test_hdnode.rs +++ b/tests/test_hdnode.rs @@ -1,5 +1,4 @@ use bip32::{ExtendedKey, ExtendedKeyAttrs, Prefix}; -use bip39::{Language::English, Mnemonic}; use thor_devkit::decode_hex; use thor_devkit::hdnode::*; @@ -36,7 +35,7 @@ fn test_from_seed() { fn test_from_mnemonic_vet() { let mnemonic = Mnemonic::from_phrase( "ignore empty bird silly journey junior ripple have guard waste between tenant", - English, + Language::English, ) .unwrap(); let private = "e4a2687ec443f4d23b6ba9e7d904a31acdda90032b34aa0e642e6dd3fd36f682"; @@ -235,7 +234,7 @@ fn test_from_public_key_can_derive() { fn test_build() { let mnemonic = Mnemonic::from_phrase( "ignore empty bird silly journey junior ripple have guard waste between tenant", - English, + Language::English, ) .unwrap(); diff --git a/tests/test_transactions.rs b/tests/test_transactions.rs index b2f18b5..4a7af80 100644 --- a/tests/test_transactions.rs +++ b/tests/test_transactions.rs @@ -254,7 +254,7 @@ fn test_sign_undelegated() { ); let signed = tx.sign(&pk); - assert_eq!(signed.signature().unwrap(), signature.to_vec()); + assert_eq!(signed.signature.clone().unwrap(), signature.to_vec()); let mut buf = vec![]; signed.encode(&mut buf); From a9def95a1cf487e282298f22331a5ad09b5c3acb Mon Sep 17 00:00:00 2001 From: STerliakov Date: Tue, 19 Dec 2023 19:54:52 +0400 Subject: [PATCH 2/3] Apply cargo-rdme --- README.md | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e48cb83..cf60799 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,88 @@ [docs.rs](https://docs.rs/thor-devkit) [build status](https://github.com/sterliakov/thor-devkit/actions?query=branch%3Amain) - + + +Rust library to aid coding with VeChain, eg. Wallets/Tx/Sign/Verify. + +This library acts primary as a proxy to several underlying libraries, +with the addition of some VeChain-specific toolchain components. + +### Usage + +One of possible use cases can be transaction creation and signing. + +Here is how you may approach it. Let's transfer a few VET to another account. + +To do so, we need to create a transaction and encode it into broadcastable bytes. + +```rust +use thor_devkit::transactions::{Transaction, Clause}; +use thor_devkit::hdnode::{Mnemonic, Language, HDNode}; + +let transaction = Transaction { + chain_tag: 1, + block_ref: 0xaabbccdd, + expiration: 32, + clauses: vec![ + Clause { + to: Some( + "0x7567d83b7b8d80addcb281a71d54fc7b3364ffed" + .parse() + .unwrap(), + ), + value: 10000.into(), + data: b"\x00\x00\x00\x60\x60\x60".to_vec().into(), + }, + ], + gas_price_coef: 128, + gas: 21000, + depends_on: None, + nonce: 0xbc614e, + reserved: None, + signature: None, +}; +let mnemonic = Mnemonic::from_phrase( + "ignore empty bird silly journey junior ripple have guard waste between tenant", + Language::English +).expect("Must be correct"); +let wallet = HDNode::build().mnemonic(mnemonic).build().expect("Builds"); +let signed = transaction.sign(&wallet.private_key().expect("Must be non-restricted").private_key()); +println!("{:02x?}", signed.to_broadcastable_bytes()); +``` + +### Examples + +You can check out sample usage of this crate in the [examples/](https://github.com/sterliakov/thor-devkit/tree/main/examples) +folder in the project repo on GitHub. + +### Readme Docs + +You can find the crate's readme documentation on the +[crates.io] page, or alternatively in the [`README.md`] file on the GitHub project repo. + +[crates.io]: https://crates.io/crates/thor-devkit +[`README.md`]: https://github.com/sterliakov/thor-devkit + + +### Contributing + +Contributions are welcome! Open a pull request to fix a bug, or [open an issue][] +to discuss a new feature or change. + +Check out the [Contributing][] section in the docs for more info. + +[Contributing]: CONTRIBUTING.md +[open an issue]: https://github.com/sterliakov/thor-devkit/issues + +### License + +This project is proudly licensed under the GNU General Public License v3 ([LICENSE](LICENSE). + +`thor-devkit` can be distributed according to the GNU General Public License v3. Contributions +will be accepted under the same license. + + ## Authors From feb95baa90bd5341a089fdf5ef8800f8f74e5cb9 Mon Sep 17 00:00:00 2001 From: STerliakov Date: Tue, 19 Dec 2023 20:02:10 +0400 Subject: [PATCH 3/3] Add test for new function --- tests/test_transactions.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_transactions.rs b/tests/test_transactions.rs index 4a7af80..28556f1 100644 --- a/tests/test_transactions.rs +++ b/tests/test_transactions.rs @@ -75,6 +75,10 @@ fn test_rlp_encode_basic() { tx ); assert!(!tx.has_valid_signature()); + assert_eq!( + tx.to_broadcastable_bytes().expect_err("Unsigned"), + secp256k1::Error::IncorrectSignature + ); } #[test] @@ -349,8 +353,7 @@ fn test_with_signature_validated() { fn test_decode_real() { let src = decode_hex("f8804a880106f4db1482fd5a81b4e1e09477845a52acad7fe6a346f5b09e5e89e7caec8e3b890391c64cd2bc206c008080828ca08088a63565b632b9b7c3c0b841d76de99625a1a8795e467d509818701ec5961a8a4cf7cc2d75cee95f9ad70891013aaa4088919cc46df4f1e3f87b4ea44d002033fa3f7bd69485cb807aa2985100").unwrap(); let tx = Transaction::decode(&mut &src[..]).unwrap(); - let mut buf = vec![]; - tx.encode(&mut buf); + let buf = tx.to_broadcastable_bytes().expect("Was signed"); assert_eq!(buf, src); } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy