0

This is the polkadotjs code:

const { Keyring } = require('@polkadot/keyring');

const seeds = "caution juice atom organ advance problem want pledge someone senior holiday very";
const keyring = new Keyring();
const pair = keyring.addFromUri(seeds);
const sig = pair.sign(pair.address);
const sigHex = '0x' + Buffer.from(sig).toString('hex');

I want to convert it into rust using subxt. Here is my code so far:

use sp_core::{
    sr25519,
    Pair,
};
use sp_keyring::AccountKeyring;

use subxt::{
    Client,
    DefaultConfig,
    PairSigner,
    SubstrateExtrinsicParams, extrinsic::Signer,
};

fn get_from_seed(seed: &str) -> sr25519::Pair {
    sr25519::Pair::from_string(&format!("//{}", seed), None)
        .expect("static values are valid; qed")
}

pub fn pair_signer(pair: sr25519::Pair) -> PairSigner<DefaultConfig, sr25519::Pair> {
    PairSigner::new(pair)
}


fn main() {
    let pair = get_from_seed("caution juice atom organ advance problem want pledge someone senior holiday very");
    let pairsigner = pair_signer(pair);
    let sig = pairsigner.sign(pairsigner.address());
    
}

But I am unable to convert it into hex. sign() takes &[u8], mismatched types expected reference &[u8] found enum MultiAddress<AccountId32, u32>

1 Answer 1

1

Seems solved it:

use sp_core::{
    sr25519,
    ed25519,
    Pair,
};
use sp_keyring::AccountKeyring;
use sp_runtime::MultiSignature;

use subxt::{
    Client,
    DefaultConfig,
    PairSigner,
    SubstrateExtrinsicParams, extrinsic::Signer,
};

fn get_from_seed(seed: &str) -> ed25519::Pair {
    ed25519::Pair::from_string(&format!("{}", seed), None)
        .expect("static values are valid; qed")
}

pub fn pair_signer(pair: ed25519::Pair) -> PairSigner<DefaultConfig, ed25519::Pair> {
    PairSigner::new(pair)
}


fn main() {
    let pair = get_from_seed("caution juice atom organ advance problem want pledge someone senior holiday very");
    let pairsigner = pair_signer(pair);
    println!("{}", pairsigner.account_id());
    let sig = pairsigner.sign(format!("{}", pairsigner.account_id()).as_bytes());
    if let MultiSignature::Ed25519(x) = sig.clone() {
        println!("0x{:?}", x);
    }
    
    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.