Skip to content

Commit

Permalink
fix derive sub account (#118)
Browse files Browse the repository at this point in the history
* fix derive sub account

* fmt
  • Loading branch information
StewartYe authored Jun 6, 2023
1 parent cd4413f commit 1b74db0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
44 changes: 42 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions sidecar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "polka
sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", package = "sp-io" }
galois-engine = { path = "../engine" }
hex-literal = "0.4.1"
[dev-dependencies]
sp-keyring = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
4 changes: 2 additions & 2 deletions sidecar/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ pub fn export_rpc(context: Context) -> RpcModule<Context> {
let user_id = crate::try_into_account(user_id)?;
let bot_id = crate::try_into_account(bot_id)?;
let sub_id = crate::derive_sub_account(
AsRef::<[u8; 32]>::as_ref(&user_id).to_vec(),
AsRef::<[u8; 32]>::as_ref(&bot_id).to_vec(),
&user_id,
&bot_id,
token,
);
let bot_x25519_pub_vec = crate::hexstr_to_vec(&bot_x25519_pub)?;
Expand Down
18 changes: 16 additions & 2 deletions sidecar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ pub fn try_into_account(addr: String) -> anyhow::Result<AccountId32> {
}
}

pub fn derive_sub_account(user_id: Vec<u8>, bot_id: Vec<u8>, token: u32) -> AccountId32 {
let h = (b"-*-#fusotao-proxy#-*-", token, user_id, bot_id)
pub fn derive_sub_account(user_addr: &AccountId32, bot_addr: &AccountId32, token: u32) -> AccountId32 {
let h = (b"-*-#fusotao-proxy#-*-", token, user_addr, bot_addr)
.using_encoded(sp_core::hashing::blake2_256);
Decode::decode(&mut h.as_ref()).expect("32 bytes; qed")
}
Expand All @@ -109,3 +109,17 @@ pub fn to_mapping_address(address: Vec<u8>) -> AccountId32 {
.using_encoded(sp_core::hashing::blake2_256);
Decode::decode(&mut h.as_ref()).expect("32 bytes; qed")
}

#[test]
fn test_derive_sub_account() {
use sp_keyring::AccountKeyring;
use std::str::FromStr;
let alice = AccountKeyring::Alice.to_account_id();
let bot = AccountKeyring::Ferdie.to_account_id();
let r = derive_sub_account(&alice, &bot, 1u32);
assert_eq!(
r,
AccountId32::from_str("0x768cff70bf523090fa1d09494cda1d4686361d1bc99129db3d67fe8b57649b7f")
.unwrap()
);
}

0 comments on commit 1b74db0

Please sign in to comment.