ambula-labs / ambula

A fork from the substrate node template, implementing the Proof of Interaction consensus algorithm.
The Unlicense
1 stars 0 forks source link

Make the keystore available in the signing RPC pallet #23

Closed LcsTen closed 1 year ago

LcsTen commented 1 year ago

The signing RPC pallet was created in https://github.com/ambula-labs/ambula/pull/22, however it doesn't handle signing right now, and one prerequisite for signing, is, obviously, the key to sign. This key is in the keystore, and we must check whether it is possible and how to access the keystore via the signing RPC pallet.

According to https://substrate.stackexchange.com/questions/3415/bring-and-use-sp-keystore-and-schnorrkel-inside-frame, the only way to use access the keystore is via an offchain worker, which should be fine since the signing pallet doesn't directly interact with the blockchain; it signs a block that will get added to the blockchain later.

It seems that the only thing an offchain worker can do in the end is to interact with the blockchain by submitting a transaction; it seems it can't send the keystore it could get to the remainder of the pallet, such as our RPC endpoint. However, while reading https://docs.substrate.io/tutorials/build-application-logic/add-offchain-workers/, I discovered a promising class frame_system::offchain::Signer that apparently can get specific keys for signing (via with_filter) and can sign an arbitrary message (via sign_message). However the "offchain" part of the namespace scares me: what if we can only use it in an offchain worker and not in a RPC? Anyway, I'll try it out.

Paco recommended that I use SyncCryptoStore::sr25519_public_keys/SyncCryptoStore::sign_with instead.

This isn't easy. As far as I can tell, there is no way to "send" the keystore to the pallet. An alternative is to send it to the RPC server on creation, unfortunately it doesn't work because te keystore isn't Sync. I'm trying to find a way to send only the key instead of the whole keystore.