Refactor remote-wallet path parsing (backport #16798) (#16894)

* SDK: More conversions for `Pubkey`

(cherry picked from commit 9b7120bf73)

* SDK: More conversion for `DerivationPath`

(cherry picked from commit 722de942ca)

* remote-wallet: Add helpers for locating remote wallets

(cherry picked from commit 64fcb792c2)

* remote-wallet: Plumb `Locator` into `RemoteWalletInfo`

(cherry picked from commit 3d12be29ec)

* remote-wallet: `derivation-path` crate doesn't like empty trailing child indexes

(cherry picked from commit 4ce4f04c58)

* remote-wallet: Move `Locator` to its own module

(cherry picked from commit cac666d035)

Co-authored-by: Trent Nelson <trent@solana.com>
This commit is contained in:
mergify[bot]
2021-04-28 01:20:41 +00:00
committed by GitHub
parent dbc58455df
commit 9797178ad1
10 changed files with 757 additions and 103 deletions

View File

@@ -1,7 +1,11 @@
use crate::{decode_error::DecodeError, hash::hashv};
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use num_derive::{FromPrimitive, ToPrimitive};
use std::{convert::TryFrom, fmt, mem, str::FromStr};
use std::{
convert::{Infallible, TryFrom},
fmt, mem,
str::FromStr,
};
use thiserror::Error;
/// Number of bytes in a pubkey
@@ -63,7 +67,16 @@ pub enum ParsePubkeyError {
WrongSize,
#[error("Invalid Base58 string")]
Invalid,
#[error("Infallible")]
Infallible,
}
impl From<Infallible> for ParsePubkeyError {
fn from(_: Infallible) -> Self {
Self::Infallible
}
}
impl<T> DecodeError<T> for ParsePubkeyError {
fn type_of() -> &'static str {
"ParsePubkeyError"
@@ -88,6 +101,13 @@ impl FromStr for Pubkey {
}
}
impl TryFrom<&str> for Pubkey {
type Error = ParsePubkeyError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Pubkey::from_str(s)
}
}
impl Pubkey {
pub fn new(pubkey_vec: &[u8]) -> Self {
Self(

View File

@@ -1,7 +1,11 @@
use {
core::{iter::IntoIterator, slice::Iter},
derivation_path::{ChildIndex, DerivationPath as DerivationPathInner},
std::{fmt, str::FromStr},
std::{
convert::{Infallible, TryFrom},
fmt,
str::FromStr,
},
thiserror::Error,
};
@@ -9,13 +13,21 @@ const ACCOUNT_INDEX: usize = 2;
const CHANGE_INDEX: usize = 3;
/// Derivation path error.
#[derive(Error, Debug, Clone)]
#[derive(Error, Debug, Clone, PartialEq)]
pub enum DerivationPathError {
#[error("invalid derivation path: {0}")]
InvalidDerivationPath(String),
#[error("infallible")]
Infallible,
}
#[derive(PartialEq)]
impl From<Infallible> for DerivationPathError {
fn from(_: Infallible) -> Self {
Self::Infallible
}
}
#[derive(Clone, PartialEq)]
pub struct DerivationPath(DerivationPathInner);
impl Default for DerivationPath {
@@ -24,6 +36,13 @@ impl Default for DerivationPath {
}
}
impl TryFrom<&str> for DerivationPath {
type Error = DerivationPathError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::from_key_str(s)
}
}
impl DerivationPath {
fn new<P: Into<Box<[ChildIndex]>>>(path: P) -> Self {
Self(DerivationPathInner::new(path))