Skip to content
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

added from_bytes for Wallet type #1983

Merged
merged 3 commits into from Dec 30, 2022
Merged

added from_bytes for Wallet type #1983

merged 3 commits into from Dec 30, 2022

Conversation

0xKitsune
Copy link
Collaborator

@0xKitsune 0xKitsune commented Dec 29, 2022

Motivation

There was no way to initialize a new Wallet from a byte slice.

Solution

I added a from_bytes associated function for the Wallet type. The function initializes a new SignerKey from a byte slice and uses the key to create a new wallet.

impl Wallet<SigningKey> {
    //--snip--
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, WalletError> {
        let signer = SigningKey::from_bytes(bytes)?;
        let address = secret_key_to_address(&signer);
        Ok(Self { signer, address, chain_id: 1 })
    }
 }

PR Checklist

  • Added Tests
  • Added Documentation
  • Updated the changelog
  • Breaking changes

@@ -90,6 +90,13 @@ impl Wallet<SigningKey> {
let address = secret_key_to_address(&signer);
Self { signer, address, chain_id: 1 }
}

/// Creates a new Wallet instance from a raw scalar value (big endian).
pub fn from_bytes(bytes: &[u8]) -> Result<Self, WalletError> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: impl AsRef<[u8]> is slightly better but no need

@gakonst gakonst merged commit f94e6f5 into gakonst:master Dec 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants