paritytech / schnorrkel-js

a Javascript wrapper for schnorrkel signatures on Ristretto using WebAssembly.
Apache License 2.0
16 stars 12 forks source link

Expose key derivation #9

Open jacogr opened 5 years ago

jacogr commented 5 years ago

Specifically derived_key_simple enabling us to do this logic from the JS side -

pub fn derive<Iter: Iterator<Item=DeriveJunction>>(&self, path: Iter) -> Option<Public> {
    let mut acc = PublicKey::from_bytes(self.as_ref()).ok()?;
    for j in path {
        match j {
            DeriveJunction::Soft(cc) => acc = acc.derived_key_simple(ChainCode(cc), &[]).0,
            DeriveJunction::Hard(_cc) => return None,
        }
    }
    Some(Self(acc.to_bytes()))
}
kianenigma commented 5 years ago

I assume this is not the only function that you will need, right?

jacogr commented 5 years ago

Only one actually - all the Junction etc. stuff is implemented to do the derivation, however in the JS derive function, cannot call anything to adjust the key (as per the Rust sample).

Not sure how that will work, have not looked - so there are possibly additional functions required around it to make thing work.

kianenigma commented 5 years ago

Ok then it looks fairly minimal to do. Hopefully within the next day should be delivered for you and the js team to test.

jacogr commented 5 years ago

yes, it looks like a single function, just not 100% sure how to jump through all the hoops since it operates on an actual pair - but basically, in the ed25519 the derivation just is a hash function, in the sr25519 need to call into the lib.

burdges commented 5 years ago

Just fyi, I added a hard derivation path which previously I'd ignored.

jacogr commented 5 years ago

@burdges Gav did this - https://github.com/paritytech/substrate/blob/master/core/primitives/src/sr25519.rs#L368 which is used further on by https://github.com/paritytech/substrate/blob/master/core/primitives/src/sr25519.rs#L450 (i.e. here the soft derivation goes via w3f/schnorrkel and hard derivation via the first linked function)

So for the meantime the polkadot-js keyring stuff will follow suit until that is adjusted in Substrate.