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

create raw payload in Client #83

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ use sp_core::{
Pair,
};
use sp_runtime::{
generic::UncheckedExtrinsic,
generic::{
SignedPayload,
UncheckedExtrinsic,
},
traits::{
IdentifyAccount,
Verify,
Expand Down Expand Up @@ -296,6 +299,30 @@ impl<T: System + Balances + Sync + Send + 'static, S: 'static> Client<T, S> {
Ok(headers)
}

/// Creates raw payload to be signed for the supplied `Call` without private key
pub async fn create_raw_payload<C>(
&self,
account_id: <T as System>::AccountId,
call: Call<C>,
) -> Result<
Vec<u8>,
Error,
>
where
C: codec::Encode,
{
let account_nonce = self.account(account_id).await?.nonce;
let version = self.runtime_version.spec_version;
let genesis_hash = self.genesis_hash;
let call = self
.metadata()
.module_with_calls(&call.module)
.and_then(|module| module.call(&call.function, call.args))?;
let extra: extrinsic::DefaultExtra<T> = extrinsic::DefaultExtra::new(version, account_nonce, genesis_hash);
let raw_payload = SignedPayload::new(call, extra.extra())?;
Ok(raw_payload.encode())
}

/// Create a transaction builder for a private key.
pub async fn xt<P>(
&self,
Expand Down Expand Up @@ -484,7 +511,7 @@ impl codec::Encode for Encoded {

#[cfg(test)]
mod tests {
use sp_keyring::AccountKeyring;
use sp_keyring::{ AccountKeyring, Ed25519Keyring };

use super::*;
use crate::{
Expand Down Expand Up @@ -585,4 +612,34 @@ mod tests {

assert!(result.is_ok())
}

#[test]
#[ignore] // requires locally running substrate node
fn test_create_raw_payload() {

let result: Result<_, Error> = async_std::task::block_on(async move {
let signer_pair = Ed25519Keyring::Alice.pair();
let signer_account_id = Ed25519Keyring::Alice.to_account_id();
let dest = AccountKeyring::Bob.to_account_id();

let client = test_client().await;

// create raw payload with AccoundId and sign it
let raw_payload = client.create_raw_payload(signer_account_id, balances::transfer::<Runtime>(dest.clone().into(), 10_000)).await?;
let raw_signature = signer_pair.sign(raw_payload.encode().split_off(2).as_slice());
let raw_multisig = MultiSignature::from(raw_signature);

// create signature with Xtbuilder
let xt = client.xt(signer_pair.clone(), None).await?;
let xt_multi_sig = xt.create_and_sign(balances::transfer::<Runtime>(dest.clone().into(), 10_000))?.signature.unwrap().1;

// compare signatures
assert_eq!(raw_multisig, xt_multi_sig);

Ok(())

});

assert!(result.is_ok())
}
}