Skip to content

Instantly share code, notes, and snippets.

@farwayer
Last active October 1, 2023 20:22
Show Gist options
  • Save farwayer/339a8770617b052fff3fca76f005a76b to your computer and use it in GitHub Desktop.
Save farwayer/339a8770617b052fff3fca76f005a76b to your computer and use it in GitHub Desktop.
Convert Nova Wallet (and Fearless) json export to eth private key (moonbeam, moonriver, etc.)
import {xsalsa20poly1305} from '@noble/ciphers/salsa'
import {scryptAsync} from '@noble/hashes/scrypt'
import {bytesToHex} from '@noble/hashes/utils'
import {base64} from '@scure/base'
let password = 'password'
let json = `
{
"address": "0xXXX",
"encoded": "XXX",
"encoding": {
"content": [
"pkcs8",
"ethereum"
],
"type": [
"scrypt",
"xsalsa20-poly1305"
],
"version": 3
},
"meta": {
"genesisHash": "xxx",
"name": "xxx",
"whenCreated": 123
}
}
`
let data = JSON.parse(json)
data = base64.decode(data.encoded)
let salt = data.slice(0, 32)
let params = Buffer.from(data)
let N = params.readUInt32LE(32)
let p = params.readUInt32LE(36)
let r = params.readUInt32LE(40)
let secret = await scryptAsync(password, salt, {N, r, p})
data = data.slice(44)
let nonce = data.slice(0, 24)
data = data.slice(24)
data = xsalsa20poly1305(secret, nonce).decrypt(data)
let pk = data.slice(16, 48)
console.log(bytesToHex(pk))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment