Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants