-
Notifications
You must be signed in to change notification settings - Fork 417
/
Copy pathtests.rs
109 lines (94 loc) · 3.93 KB
/
tests.rs
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
// This file is part of Astar.
// Copyright (C) Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.
use hex_literal::hex;
use crate::mock::*;
use precompile_utils::testing::*;
use sp_core::{ecdsa, Pair};
fn precompiles() -> TestPrecompileSet<Runtime> {
PrecompilesValue::get()
}
#[test]
fn wrong_signature_length_returns_false() {
ExtBuilder.build().execute_with(|| {
let pair = ecdsa::Pair::from_seed(b"12345678901234567890123456789012");
let public = pair.public();
let signature = hex!["0042"];
let message = hex!["00"];
precompiles()
.prepare_test(
TestAccount::Alice,
PRECOMPILE_ADDRESS,
PrecompileCall::verify {
public_bytes: <ecdsa::Public as AsRef<[u8]>>::as_ref(&public).into(),
signature_bytes: signature.into(),
message: message.into(),
},
)
.expect_no_logs()
.execute_returns(false);
});
}
#[test]
fn bad_signature_returns_false() {
ExtBuilder.build().execute_with(|| {
let pair = ecdsa::Pair::from_seed(b"12345678901234567890123456789012");
let public = pair.public();
let message = hex!("2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee00000000000000000200d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a4500000000000000");
let signature = pair.sign(&message[..]);
assert!(ecdsa::Pair::verify(&signature, &message[..], &public));
let bad_message = hex!["00"];
precompiles()
.prepare_test(
TestAccount::Alice,
PRECOMPILE_ADDRESS,
PrecompileCall::verify {
public_bytes: <ecdsa::Public as AsRef<[u8]>>::as_ref(&public).into(),
signature_bytes: <ecdsa::Signature as AsRef<[u8]>>::as_ref(&signature).into(),
message: bad_message.into(),
},
)
.expect_no_logs()
.execute_returns(false);
});
}
#[test]
fn substrate_test_vector_works() {
ExtBuilder.build().execute_with(|| {
let pair = ecdsa::Pair::from_seed(&hex!(
"1d2187216832d1ee14be2e677f9e3ebceca715510ba1460a20d6fce07ba36b1e"
));
let public = pair.public();
assert_eq!(
public,
ecdsa::Public::from_raw(hex!(
"02071bca0b0da3cfa98d3089db224999a827fc1df1a3d6221194382872f0d1a82a"
))
);
let message = hex!("2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee00000000000000000200d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a4500000000000000");
let signature = pair.sign(&message[..]);
assert!(ecdsa::Pair::verify(&signature, &message[..], &public));
precompiles()
.prepare_test(
TestAccount::Alice,
PRECOMPILE_ADDRESS,
PrecompileCall::verify {
public_bytes: <ecdsa::Public as AsRef<[u8]>>::as_ref(&public).into(),
signature_bytes: <ecdsa::Signature as AsRef<[u8]>>::as_ref(&signature).into(),
message: message.into(),
},
)
.expect_no_logs()
.execute_returns(true);
});
}