mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 20:27:58 +00:00
port subxt-signer to no-std (such a pain)
This commit is contained in:
+37
-19
@@ -6,10 +6,22 @@
|
||||
use codec::Encode;
|
||||
|
||||
use crate::crypto::{seed_from_entropy, DeriveJunction, SecretUri};
|
||||
use core::str::FromStr;
|
||||
use derive_more::{Display, From};
|
||||
use hex::FromHex;
|
||||
use secp256k1::{ecdsa::RecoverableSignature, Message, SecretKey, SECP256K1};
|
||||
use secp256k1::{ecdsa::RecoverableSignature, All, Message, Secp256k1, SecretKey};
|
||||
use secrecy::ExposeSecret;
|
||||
|
||||
struct GlobalSecp256K1Context;
|
||||
impl core::ops::Deref for GlobalSecp256K1Context {
|
||||
type Target = Secp256k1<All>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
static ONCE: once_cell::sync::OnceCell<Secp256k1<All>> = once_cell::sync::OnceCell::new();
|
||||
ONCE.get_or_init(|| Secp256k1::new())
|
||||
}
|
||||
}
|
||||
|
||||
const SEED_LENGTH: usize = 32;
|
||||
|
||||
/// Seed bytes used to generate a key pair.
|
||||
@@ -68,7 +80,7 @@ impl Keypair {
|
||||
let seed = Seed::from_hex(hex_str)?;
|
||||
Self::from_seed(seed)?
|
||||
} else {
|
||||
let phrase = bip39::Mnemonic::parse(phrase.expose_secret().as_str())?;
|
||||
let phrase = bip39::Mnemonic::from_str(phrase.expose_secret().as_str())?;
|
||||
let pass_str = password.as_ref().map(|p| p.expose_secret().as_str());
|
||||
Self::from_phrase(&phrase, pass_str)?
|
||||
};
|
||||
@@ -91,8 +103,9 @@ impl Keypair {
|
||||
/// keypair.sign(b"Hello world!");
|
||||
/// ```
|
||||
pub fn from_phrase(mnemonic: &bip39::Mnemonic, password: Option<&str>) -> Result<Self, Error> {
|
||||
let big_seed = seed_from_entropy(&mnemonic.to_entropy(), password.unwrap_or(""))
|
||||
.ok_or(Error::InvalidSeed)?;
|
||||
let (arr, len) = mnemonic.to_entropy_array();
|
||||
let big_seed =
|
||||
seed_from_entropy(&arr[0..len], password.unwrap_or("")).ok_or(Error::InvalidSeed)?;
|
||||
|
||||
let seed: Seed = big_seed[..SEED_LENGTH]
|
||||
.try_into()
|
||||
@@ -109,7 +122,8 @@ impl Keypair {
|
||||
pub fn from_seed(seed: Seed) -> Result<Self, Error> {
|
||||
let secret = SecretKey::from_slice(&seed).map_err(|_| Error::InvalidSeed)?;
|
||||
Ok(Self(secp256k1::Keypair::from_secret_key(
|
||||
SECP256K1, &secret,
|
||||
&GlobalSecp256K1Context,
|
||||
&secret,
|
||||
)))
|
||||
}
|
||||
|
||||
@@ -161,9 +175,9 @@ impl Keypair {
|
||||
// From sp_core::ecdsa::sign_prehashed:
|
||||
let wrapped = Message::from_digest_slice(&message_hash).expect("Message is 32 bytes; qed");
|
||||
let recsig: RecoverableSignature =
|
||||
SECP256K1.sign_ecdsa_recoverable(&wrapped, &self.0.secret_key());
|
||||
GlobalSecp256K1Context.sign_ecdsa_recoverable(&wrapped, &self.0.secret_key());
|
||||
// From sp_core::ecdsa's `impl From<RecoverableSignature> for Signature`:
|
||||
let (recid, sig) = recsig.serialize_compact();
|
||||
let (recid, sig): (_, [u8; 64]) = recsig.serialize_compact();
|
||||
let mut signature_bytes: [u8; 65] = [0; 65];
|
||||
signature_bytes[..64].copy_from_slice(&sig);
|
||||
signature_bytes[64] = (recid.to_i32() & 0xFF) as u8;
|
||||
@@ -192,31 +206,35 @@ pub fn verify<M: AsRef<[u8]>>(sig: &Signature, message: M, pubkey: &PublicKey) -
|
||||
};
|
||||
let message_hash = sp_core_hashing::blake2_256(message.as_ref());
|
||||
let wrapped = Message::from_digest_slice(&message_hash).expect("Message is 32 bytes; qed");
|
||||
signature.verify(&wrapped, &public).is_ok()
|
||||
GlobalSecp256K1Context
|
||||
.verify_ecdsa(&wrapped, &signature, &public)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
/// An error handed back if creating a keypair fails.
|
||||
#[derive(Debug, PartialEq, thiserror::Error)]
|
||||
#[derive(Debug, PartialEq, Display, From)]
|
||||
pub enum Error {
|
||||
/// Invalid seed.
|
||||
#[error("Invalid seed (was it the wrong length?)")]
|
||||
#[display(fmt = "Invalid seed (was it the wrong length?)")]
|
||||
#[from(ignore)]
|
||||
InvalidSeed,
|
||||
/// Invalid seed.
|
||||
#[error("Invalid seed for ECDSA, contained soft junction")]
|
||||
#[display(fmt = "Invalid seed for ECDSA, contained soft junction")]
|
||||
#[from(ignore)]
|
||||
SoftJunction,
|
||||
/// Invalid phrase.
|
||||
#[error("Cannot parse phrase: {0}")]
|
||||
Phrase(#[from] bip39::Error),
|
||||
#[display(fmt = "Cannot parse phrase: {_0}")]
|
||||
Phrase(bip39::Error),
|
||||
/// Invalid hex.
|
||||
#[error("Cannot parse hex string: {0}")]
|
||||
Hex(#[from] hex::FromHexError),
|
||||
#[display(fmt = "Cannot parse hex string: {_0}")]
|
||||
Hex(hex::FromHexError),
|
||||
}
|
||||
|
||||
/// Dev accounts, helpful for testing but not to be used in production,
|
||||
/// since the secret keys are known.
|
||||
pub mod dev {
|
||||
use super::*;
|
||||
use std::str::FromStr;
|
||||
use core::str::FromStr;
|
||||
|
||||
once_static_cloned! {
|
||||
/// Equivalent to `{DEV_PHRASE}//Alice`.
|
||||
@@ -260,9 +278,9 @@ pub mod dev {
|
||||
mod subxt_compat {
|
||||
use super::*;
|
||||
|
||||
use subxt::config::Config;
|
||||
use subxt::tx::Signer as SignerT;
|
||||
use subxt::utils::{AccountId32, MultiAddress, MultiSignature};
|
||||
use subxt_core::config::Config;
|
||||
use subxt_core::utils::{AccountId32, MultiAddress, MultiSignature};
|
||||
use subxt_core::Signer as SignerT;
|
||||
|
||||
impl From<Signature> for MultiSignature {
|
||||
fn from(value: Signature) -> Self {
|
||||
|
||||
Reference in New Issue
Block a user