paritytech / subxt

Interact with Substrate based nodes in Rust or WebAssembly
Other
390 stars 236 forks source link

Support signing with json exported from polkadot.js #1631

Open ethernomad opened 3 weeks ago

ethernomad commented 3 weeks ago

Accounts managed by https://polkadot.js.org/apps/ or the browser extension can be downloaded as an encrypted json file, for example:

{
  "encoded": "",
  "encoding": {
    "content": [
      "pkcs8",
      "sr25519"
    ],
    "type": [
      "scrypt",
      "xsalsa20-poly1305"
    ],
    "version": "3"
  },
  "address": "5DV3QBwhs7djHop5WEcUQ1BMFSmsqvqnDhrYh343x495AL4v",
  "meta": {
    "genesisHash": "",
    "name": "",
    "whenCreated": 1639212245729
  }
}

I'm not sure what the name of this format is.

It would be useful for subxt_signer to be able to load a keypair from a json file and the correct password.

I can implement a pull request for this.

jsdw commented 3 weeks ago

I suspect it's a semi arbitrary format that the PJS extension invented and that one or two other wallets then decided to allow import of.

Is it worth being able to do this programmatically eg via some utility function in Subxt signer I wonder? Or would it make more sense to have a binary that can spit out the relevant details to be passed to subxt-signer? (Can subkey do this already I wonder? https://docs.substrate.io/reference/command-line-tools/subkey/)

ethernomad commented 3 weeks ago

I have a use case where a program needs to have an encrypted key on disk that can be decrypted interactively. This json file format seems ideal because an account can be created / exported with ease from the browser extension.

I've almost finished implementing a from_json() method in subxt_signer::sr25519::Keypair

jsdw commented 3 weeks ago

I've almost finished implementing a from_json() method in subxt_signer::sr25519::Keypair

Ok awesome, would be good to see! I imagine it needs additional dependencies so we may want to have this behind a feature flag. I'm also currently wondering whether it should be attached to a specific algorithm (eg sr25519) or be a standalone utility function which takes the JSon and gives back the private key or whatever, but let's see what you come up with!