rsk private key encrypt - Error value parameter should be a number of string

198 views Asked by At

I m trying to save my private key in a keystore v3 json format but when I try to encrypt it using the following methods:

myNewAccount.encrypt('########')
rsk.accounts.encrypt('the-private-key', '########')

They both give the following error: Uncaught Error: value parameter should be a number or string.

Trying a slightly different approach,

rsk.accounts.encrypt('the-private-key', '########')

and I get a different error Uncaught Error: options.n should be number and has value of 2048, 4096, 8192 or 16384

I even tried passing options as third argument to the encrypt function but the same error:

rsk3.accounts.encrypt('the-private-key', '#######',  { kdf: 'pbkdf2', n: 4096, salt, iv, uuid })

error -> Uncaught Error: value parameter should be a number or string

What am I doing wrong? Here is how I create and use the account :

const account = rsk.accounts.create(rsk.utils.randomHex(32).toString('hex'))
rsk.accounts.wallet.add(account)
1

There are 1 answers

0
bguiz On

If you wish to use an existing private key:

(1) new ethers.Wallet(privateKey)

Reference: https://docs.ethers.io/v5/single-page/#/v5/api/signer/-%23-Wallet-constructor

(2) Wallet.encrypt(password)

Reference: https://docs.ethers.io/v5/single-page/#/v5/api/signer/-%23-Wallet-encrypt

// assuming you already have values set for `privateKey` and `password`

const wallet = new ethers.Wallet(privateKey); // (1)
const encryptedWalletJson = await wallet.encrypt(password); // (2)

If you wish to generate a new private key:

(1) ethers.Wallet.createRandom()

Reference: https://docs.ethers.io/v5/single-page/#/v5/api/signer/-%23-Wallet-createRandom

(2) Wallet.encrypt(password)

Reference: https://docs.ethers.io/v5/single-page/#/v5/api/signer/-%23-Wallet-encrypt

// assuming you already have a value for `password`

const wallet = ethers.Wallet.createRandom(); // (1)
const encryptedWalletJson = await wallet.encrypt(password); // (2)