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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use std::any::TypeId;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::sync::{Arc, RwLock};
use rhai::{Dynamic, Engine, EvalAltResult};
use sp_core::{ed25519, sr25519};
use sp_runtime::generic;
use ledger_apdu::{APDUAnswer, APDUCommand, APDUErrorCodes};
use sp_core::Encode;
use crate::client::{Client, Extra, ExtrinsicCallResult, ExtrinsicV4, SignedPayload};
use crate::metadata::EncodedCall;
use crate::types::TypeLookup;
use crate::users::AccountId;
pub const MAX_PACKET_LEN: u32 = 10_000_000;
pub const HIGH_BIT: u32 = 0x8000_0000;
pub const CHUNK_SIZE: usize = 250;
pub const INS_GET_ADDR: u8 = 0x01;
pub const INS_SIGN: u8 = 0x02;
pub const SIGN_INIT: u8 = 0x00;
pub const SIGN_ADD: u8 = 0x01;
pub const SIGN_LAST: u8 = 0x02;
pub const SCHEME_ED25519: u8 = 0x00;
pub const SCHEME_SR25519: u8 = 0x01;
pub const SLIP0044_POLYMESH: u32 = 595;
pub const APP_POLYMESH: u8 = 0x91;
pub trait LedgerSyncTransport: Send + Sync {
fn send_cmd(&self, command: APDUCommand) -> Result<APDUAnswer, Box<EvalAltResult>>;
}
impl LedgerSyncTransport for ledger::TransportNativeHID {
fn send_cmd(&self, command: APDUCommand) -> Result<APDUAnswer, Box<EvalAltResult>> {
Ok(self.exchange(&command).map_err(|e| e.to_string())?)
}
}
struct TransportTcp(RwLock<TcpStream>);
impl LedgerSyncTransport for TransportTcp {
fn send_cmd(&self, c: APDUCommand) -> Result<APDUAnswer, Box<EvalAltResult>> {
let mut sock = self.0.write().unwrap();
let packet_len = 5 + c.data.len();
let mut buf = Vec::with_capacity(packet_len + 4);
buf.extend(u32::to_be_bytes(packet_len as u32));
buf.extend(&[c.cla, c.ins, c.p1, c.p2, c.data.len() as u8]);
buf.extend(&c.data);
sock.write(&buf).map_err(|e| e.to_string())?;
let mut buf = [0; 4];
sock.read_exact(&mut buf).map_err(|e| e.to_string())?;
let packet_len = u32::from_be_bytes(buf) + 2;
if packet_len > MAX_PACKET_LEN {
return Err(
format!(
"APDU Packet is too large: {} > {}",
packet_len, MAX_PACKET_LEN
)
.into(),
);
}
let mut buf = vec![0u8; packet_len as usize];
sock.read_exact(&mut buf).map_err(|e| e.to_string())?;
log::debug!("Answer length: {}", buf.len());
Ok(APDUAnswer::from_answer(buf))
}
}
#[derive(Clone)]
pub struct Ledger {
transport: Arc<dyn LedgerSyncTransport>,
}
impl Ledger {
pub fn new_hid() -> Result<Self, Box<EvalAltResult>> {
let transport = ledger::TransportNativeHID::new().map_err(|e| e.to_string())?;
Ok(Self {
transport: Arc::new(transport),
})
}
pub fn new_tcp(addr: &str) -> Result<Self, Box<EvalAltResult>> {
let stream = TcpStream::connect(addr).map_err(|e| e.to_string())?;
Ok(Self {
transport: Arc::new(TransportTcp(RwLock::new(stream))),
})
}
pub fn send_cmd(&self, command: APDUCommand) -> Result<APDUAnswer, Box<EvalAltResult>> {
log::debug!("Ledger cmd: {:?}", command);
self.transport.send_cmd(command)
}
}
#[derive(Clone, Default, Debug)]
pub struct AddressBip44 {
slip0044: u32,
account: u32,
change: u32,
address_index: u32,
}
impl AddressBip44 {
pub fn new(slip0044: u32, account: u32, change: u32, address_index: u32) -> Self {
Self {
slip0044,
account,
change,
address_index,
}
}
pub fn encode(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(20);
buf.extend(u32::to_le_bytes(HIGH_BIT | 44));
buf.extend(u32::to_le_bytes(HIGH_BIT | self.slip0044));
buf.extend(u32::to_le_bytes(HIGH_BIT | self.account));
buf.extend(u32::to_le_bytes(HIGH_BIT | self.change));
buf.extend(u32::to_le_bytes(HIGH_BIT | self.address_index));
buf
}
}
#[derive(Clone)]
pub struct SubstrateApp {
ledger: Ledger,
client: Client,
cla: u8,
slip0044: u32,
account_id: AccountId,
address: AddressBip44,
scheme: u8,
nonce: u32,
}
impl SubstrateApp {
pub fn new_polymesh(ledger: Ledger, client: Client) -> Result<Self, Box<EvalAltResult>> {
let mut app = Self {
ledger,
client,
cla: APP_POLYMESH,
slip0044: SLIP0044_POLYMESH,
account_id: Default::default(),
address: AddressBip44::new(SLIP0044_POLYMESH, 0, 0, 0),
scheme: SCHEME_ED25519,
nonce: 0,
};
app.update_account()?;
Ok(app)
}
fn send_cmd(
&self,
ins: u8,
p1: u8,
p2: u8,
data: Vec<u8>,
) -> Result<Vec<u8>, Box<EvalAltResult>> {
Ok(Self::is_error(self.ledger.send_cmd(APDUCommand {
cla: self.cla,
ins,
p1,
p2,
data,
})?)?)
}
fn update_account(&mut self) -> Result<(), Box<EvalAltResult>> {
let res = self.send_cmd(INS_GET_ADDR, 0x00, self.scheme, self.address.encode())?;
let len = res.len();
log::debug!("-- GET_ADDR: len={}", len);
self.account_id = AccountId::try_from(&res[0..32]).unwrap();
let address = String::from_utf8_lossy(&res[32..len]);
log::debug!(" -- address: {:?}", address);
self.nonce = self.client.get_nonce(self.account_id.clone())?.unwrap_or(0);
log::debug!(
" Leaded nonce[{}] for account: {:?}",
self.nonce,
self.account_id
);
Ok(())
}
pub fn get_account_id(&self) -> AccountId {
self.account_id.clone()
}
pub fn set_address(
&mut self,
account: u32,
change: u32,
address_index: u32,
) -> Result<(), Box<EvalAltResult>> {
self.address = AddressBip44::new(self.slip0044, account, change, address_index);
self.update_account()
}
fn is_error(res: APDUAnswer) -> Result<Vec<u8>, Box<EvalAltResult>> {
if res.retcode == APDUErrorCodes::NoError as u16 {
Ok(res.data)
} else {
let msg = String::from_utf8_lossy(&res.data);
let err = ledger_apdu::map_apdu_error_description(res.retcode);
if msg.len() > 0 {
log::error!("Ledger: error: msg={}, error={}, code={:#X?}", msg, err, res.retcode);
Err(format!("Ledger: error: msg={}, error={}", msg, err).into())
} else {
log::error!("Ledger: error: error={}, code={:#X?}", err, res.retcode);
Err(format!("Ledger: error: error={}", err).into())
}
}
}
pub fn sign(&self, data: Vec<u8>) -> Result<Vec<u8>, Box<EvalAltResult>> {
let mut resp = self.send_cmd(INS_SIGN, SIGN_INIT, self.scheme, self.address.encode())?;
let mut chunks = data.chunks(CHUNK_SIZE).peekable();
while let Some(chunk) = chunks.next() {
let p1 = if chunks.peek().is_some() {
SIGN_ADD
} else {
SIGN_LAST
};
resp = self.send_cmd(INS_SIGN, p1, self.scheme, chunk.into())?;
}
Ok(resp)
}
pub fn submit_call(
&mut self,
call: EncodedCall,
) -> Result<ExtrinsicCallResult, Box<EvalAltResult>> {
let extra = Extra::new(generic::Era::Immortal, self.nonce);
let payload = SignedPayload::new(&call, &extra, self.client.get_signed_extra());
let signature = self.sign(payload.encode())?;
log::debug!(
"signature res: len={}, sig_type={}, sig={:?}",
signature.len(),
signature[0],
&signature[1..]
);
let sig = match self.scheme {
SCHEME_ED25519 => ed25519::Signature::from_slice(&signature[1..]).into(),
SCHEME_SR25519 => sr25519::Signature::from_slice(&signature[1..]).into(),
scheme => {
panic!("Unsupported signature scheme: {}", scheme);
}
};
let xt = ExtrinsicV4::signed(self.account_id.clone(), sig, extra, call);
let xthex = xt.to_hex();
let res = self.client.submit(xthex)?;
self.nonce += 1;
Ok(res)
}
}
#[derive(Clone)]
pub struct SharedApp(Arc<RwLock<SubstrateApp>>);
impl SharedApp {
pub fn acc(&mut self) -> AccountId {
self.0.read().unwrap().get_account_id()
}
pub fn submit_call(
&mut self,
call: EncodedCall,
) -> Result<ExtrinsicCallResult, Box<EvalAltResult>> {
self.0.write().unwrap().submit_call(call)
}
}
#[derive(Clone)]
pub struct LedgerApps {
ledgers: HashMap<String, Ledger>,
apps: HashMap<String, SharedApp>,
client: Client,
}
impl LedgerApps {
pub fn new(client: Client) -> Self {
Self {
ledgers: HashMap::new(),
apps: HashMap::new(),
client,
}
}
fn get_ledger(&mut self, ledger_type: &str) -> Result<Ledger, Box<EvalAltResult>> {
use std::collections::hash_map::Entry;
let (transport, param) = ledger_type
.split_once(':')
.map(|(transport, param)| (transport.trim(), param.trim()))
.unwrap_or((ledger_type, ""));
let parsed_name = format!("{}:{}", transport, param);
Ok(match self.ledgers.entry(parsed_name) {
Entry::Occupied(entry) => entry.get().clone(),
Entry::Vacant(entry) => {
log::info!("Create new ledger: {}", ledger_type);
let ledger = match transport {
"HID" => Ledger::new_hid()?,
"tcp" => Ledger::new_tcp(param)?,
_ => {
panic!("Unsupported ledger type: {}", ledger_type);
}
};
entry.insert(ledger.clone());
ledger
}
})
}
fn get_app(&mut self, ledger_app: &str) -> Result<Dynamic, Box<EvalAltResult>> {
use std::collections::hash_map::Entry;
let (app_name, ledger_type) = ledger_app
.split_once(':')
.map(|(app, ledger)| (app.trim(), ledger.trim()))
.ok_or_else(|| format!("Failed to parse ledger_app: {}", ledger_app))?;
let parsed_name = format!("{}:{}", app_name, ledger_type);
let ledger = self.get_ledger(ledger_type)?;
Ok(match self.apps.entry(parsed_name) {
Entry::Occupied(entry) => Dynamic::from(entry.get().clone()),
Entry::Vacant(entry) => {
log::info!("Create new ledger app: {}", app_name);
let app = match app_name {
"Polymesh" => SubstrateApp::new_polymesh(ledger, self.client.clone())?,
_ => {
panic!("Unsupported ledger app: {}", app_name);
}
};
let app = SharedApp(Arc::new(RwLock::new(app)));
entry.insert(app.clone());
Dynamic::from(app)
}
})
}
}
pub fn init_engine(
engine: &mut Engine,
globals: &mut HashMap<String, Dynamic>,
client: &Client,
lookup: &TypeLookup,
) -> Result<(), Box<EvalAltResult>> {
engine
.register_type_with_name::<SharedApp>("LedgerApp")
.register_get("acc", SharedApp::acc)
.register_result_fn("submit", SharedApp::submit_call)
.register_type_with_name::<LedgerApps>("LedgerApps")
.register_result_fn("get_app", LedgerApps::get_app);
globals.insert(
"LedgerApps".into(),
Dynamic::from(LedgerApps::new(client.clone())),
);
lookup.custom_encode("AccountId", TypeId::of::<SharedApp>(), |value, data| {
let mut app = value.cast::<SharedApp>();
data.encode(app.acc());
Ok(())
})?;
lookup.custom_encode("MultiAddress", TypeId::of::<SharedApp>(), |value, data| {
let mut app = value.cast::<SharedApp>();
data.encode(0u8);
data.encode(app.acc());
Ok(())
})?;
lookup.custom_encode("Signatory", TypeId::of::<SharedApp>(), |value, data| {
let mut app = value.cast::<SharedApp>();
data.encode(1u8);
data.encode(app.acc());
Ok(())
})?;
Ok(())
}