1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
pub mod xt_primitives;
use crate::{Api, Index, Metadata};
use codec::Encode;
use sp_core::crypto::Pair;
use sp_core::H256;
use sp_runtime::generic::Era;
use sp_runtime::MultiSignature;
use xt_primitives::*;
type Call<A> = (u8, u8, A);
pub fn compose_call<A: Encode>(metadata: &Metadata, module: &str, call: &str, args: A) -> Call<A> {
let module = metadata.module_with_calls(module).unwrap();
let call_index = module.calls.get(call).unwrap();
(module.index, *call_index as u8, args)
}
pub fn compose_extrinsic_offline<P, A>(
signer: &P,
call: Call<A>,
nonce: Index,
era: Era,
genesis_hash: H256,
genesis_or_current_hash: H256,
runtime_spec_version: u32,
transaction_version: u32,
) -> UncheckedExtrinsicV4<Call<A>>
where
P: Pair,
MultiSignature: From<P::Signature>,
A: Encode,
{
let extra = GenericExtra::new(era, nonce);
let raw_payload = SignedPayload::from_raw(
&call,
&extra,
(
runtime_spec_version,
transaction_version,
genesis_hash,
genesis_or_current_hash,
(),
(),
(),
),
);
let signature = raw_payload.using_encoded(|payload| signer.sign(payload));
let mut arr: [u8; 32] = Default::default();
arr.clone_from_slice(signer.public().as_ref());
UncheckedExtrinsicV4::new_signed(
call,
GenericAddress::from(AccountId::from(arr)),
signature.into(),
extra,
)
}
pub fn compose_extrinsic<P, A>(
api: &Api<P>,
module: &str,
call: &str,
args: A,
) -> UncheckedExtrinsicV4<Call<A>>
where
P: Pair,
MultiSignature: From<P::Signature>,
A: Encode,
{
let call = compose_call(&api.metadata, module, call, args);
if let Some(signer) = &api.signer {
compose_extrinsic_offline(
signer,
call,
api.get_nonce().unwrap(),
Era::Immortal,
api.genesis_hash,
api.genesis_hash,
api.runtime_version.spec_version,
api.runtime_version.transaction_version,
)
} else {
UncheckedExtrinsicV4 {
signature: None,
function: call,
}
}
}