Generate Keypairs for guardians during test.
Change-Id: Ide5f0347ee40189aecfe5dca4ba8f4ef3f0756d9
This commit is contained in:
parent
889895bc91
commit
2e4279c964
|
@ -330,6 +330,7 @@ dependencies = [
|
||||||
"hex-literal",
|
"hex-literal",
|
||||||
"libsecp256k1",
|
"libsecp256k1",
|
||||||
"primitive-types 0.9.0",
|
"primitive-types 0.9.0",
|
||||||
|
"rand 0.7.3",
|
||||||
"sha3",
|
"sha3",
|
||||||
"solana-client",
|
"solana-client",
|
||||||
"solana-program",
|
"solana-program",
|
||||||
|
|
|
@ -25,6 +25,7 @@ solitaire = { path = "../../solitaire/program"}
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
hex = "*"
|
hex = "*"
|
||||||
|
rand = "0.7.3"
|
||||||
hex-literal = "0.3.1"
|
hex-literal = "0.3.1"
|
||||||
libsecp256k1 = { version = "0.3.5", features = [] }
|
libsecp256k1 = { version = "0.3.5", features = [] }
|
||||||
solana-client = "1.7.0"
|
solana-client = "1.7.0"
|
||||||
|
|
|
@ -49,6 +49,10 @@ use std::time::{
|
||||||
|
|
||||||
use sha3::Digest;
|
use sha3::Digest;
|
||||||
use hex_literal::hex;
|
use hex_literal::hex;
|
||||||
|
use secp256k1::{
|
||||||
|
PublicKey,
|
||||||
|
SecretKey,
|
||||||
|
};
|
||||||
|
|
||||||
use bridge::{
|
use bridge::{
|
||||||
accounts::GuardianSetDerivationData,
|
accounts::GuardianSetDerivationData,
|
||||||
|
@ -74,20 +78,36 @@ mod common;
|
||||||
fn test_alien_chain_messages() {
|
fn test_alien_chain_messages() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ethereum Test Addresses (Keccak hashed Public Key)
|
fn generate_keys() -> (Vec<[u8; 20]>, Vec<SecretKey>) {
|
||||||
const INITIAL_PUBLIC: [[u8; 20]; 2] = [
|
use rand::Rng;
|
||||||
hex!("1d72877eb2d898738afe94c6101152ede0435de9"),
|
let mut rng = rand::thread_rng();
|
||||||
hex!("7C6824A51bD586ecdc866ECCc9cd04b317570dDB"),
|
|
||||||
];
|
|
||||||
|
|
||||||
/// Secp256k1 Secret Keys, used as the single initial guardian for testing.
|
// Generate Guardian Keys
|
||||||
const INITIAL_SECRET: [[u8; 32]; 2] = [
|
let secret_keys: Vec<SecretKey> = std::iter::repeat_with(|| SecretKey::random(&mut rng))
|
||||||
hex!("99701c805ef938e13f0e48f09e2c327891c1d84729d152f301e7e62cbf1f91c9"),
|
.take(10)
|
||||||
hex!("0e76b44615dcabbcd1e5060c9b18e879cd0d4a5a7323e15f48ef13169c179743"),
|
.collect();
|
||||||
];
|
|
||||||
|
(
|
||||||
|
secret_keys
|
||||||
|
.iter()
|
||||||
|
.map(|key| {
|
||||||
|
let public_key = PublicKey::from_secret_key(&key);
|
||||||
|
let mut h = sha3::Keccak256::default();
|
||||||
|
h.write(&public_key.serialize()[1..]).unwrap();
|
||||||
|
let key: [u8; 32] = h.finalize().into();
|
||||||
|
let mut address = [0u8; 20];
|
||||||
|
address.copy_from_slice(&key[12..]);
|
||||||
|
address
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
secret_keys,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bridge_messages() {
|
fn test_bridge_messages() {
|
||||||
|
let (public_keys, secret_keys) = generate_keys();
|
||||||
|
|
||||||
// Data we want to verify exists, wrapped in a message the guardians can process.
|
// Data we want to verify exists, wrapped in a message the guardians can process.
|
||||||
let nonce = 12397;
|
let nonce = 12397;
|
||||||
let data = b"Prove Me".to_vec();
|
let data = b"Prove Me".to_vec();
|
||||||
|
@ -99,19 +119,19 @@ fn test_bridge_messages() {
|
||||||
let emitter = Keypair::new();
|
let emitter = Keypair::new();
|
||||||
|
|
||||||
// Initialize the Bridge.
|
// Initialize the Bridge.
|
||||||
common::initialize(client, program, payer, &INITIAL_PUBLIC);
|
common::initialize(client, program, payer, &*public_keys);
|
||||||
|
|
||||||
// Post the message, publishing the data for guardian consumption.
|
// Post the message, publishing the data for guardian consumption.
|
||||||
common::post_message(client, program, payer, &emitter, nonce, data.clone());
|
common::post_message(client, program, payer, &emitter, nonce, data.clone());
|
||||||
|
|
||||||
// Emulate Guardian behaviour, verifying the data and publishing signatures/VAA.
|
// Emulate Guardian behaviour, verifying the data and publishing signatures/VAA.
|
||||||
let (vaa, body, body_hash, keys) = guardian_sign_round(&emitter, data.clone(), nonce);
|
let (vaa, body, body_hash) = guardian_sign_round(&emitter, data.clone(), nonce);
|
||||||
common::verify_signatures(client, program, payer, body, body_hash, &keys);
|
common::verify_signatures(client, program, payer, body, body_hash, &secret_keys);
|
||||||
common::post_vaa(client, program, payer, vaa);
|
common::post_vaa(client, program, payer, vaa);
|
||||||
|
|
||||||
// Upgrade the guardian set with a new set of guardians.
|
// Upgrade the guardian set with a new set of guardians.
|
||||||
let nonce = 12398;
|
let nonce = 12398;
|
||||||
let data = update_guardian_set(1, &INITIAL_PUBLIC);
|
let data = update_guardian_set(1, &public_keys);
|
||||||
let message_key = common::post_message(client, program, payer, &emitter, nonce, data.clone());
|
let message_key = common::post_message(client, program, payer, &emitter, nonce, data.clone());
|
||||||
|
|
||||||
common::upgrade_guardian_set(
|
common::upgrade_guardian_set(
|
||||||
|
@ -142,7 +162,7 @@ fn guardian_sign_round(
|
||||||
emitter: &Keypair,
|
emitter: &Keypair,
|
||||||
data: Vec<u8>,
|
data: Vec<u8>,
|
||||||
nonce: u32,
|
nonce: u32,
|
||||||
) -> (PostVAAData, Vec<u8>, [u8; 32], Vec<secp256k1::SecretKey>) {
|
) -> (PostVAAData, Vec<u8>, [u8; 32]) {
|
||||||
let mut vaa = PostVAAData {
|
let mut vaa = PostVAAData {
|
||||||
version: 0,
|
version: 0,
|
||||||
guardian_set_index: 0,
|
guardian_set_index: 0,
|
||||||
|
@ -179,12 +199,5 @@ fn guardian_sign_round(
|
||||||
h.finalize().into()
|
h.finalize().into()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Sign with all available secret keys.
|
(vaa, body, body_hash)
|
||||||
let mut keys = Vec::new();
|
|
||||||
for secret_key in INITIAL_SECRET.iter() {
|
|
||||||
let secret_key = secp256k1::SecretKey::parse(secret_key).unwrap();
|
|
||||||
keys.push(secret_key);
|
|
||||||
}
|
|
||||||
|
|
||||||
(vaa, body, body_hash, keys)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue