Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added decryptMessage function for implementation eth_decryptMessage #15

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ You must return a valid signed ethereumjs-tx (https://github.com/ethereumjs/ethe

### signMessage(address, data)

The `eth_sign` method will receive the incoming data, alread hashed, and must sign that hash, and then return the raw signed hash.
The `eth_sign` method will receive the incoming data, already hashed, and must sign that hash, and then return the raw signed hash.

### decryptMessage(address, data)

The `eth_decryptMessage` method will receive the incoming data in array format that returns `encrypt` function in `eth-sig-util` and must decrypt message, and then return the raw message.

### exportAccount(address)

Expand Down
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ class SimpleKeyring extends EventEmitter {
return Promise.resolve(sig)
}

// For eth_decryptMessage:
decryptMessage (withAccount, encryptedData) {
const wallet = this._getWalletForAccount(withAccount)
const privKey = ethUtil.stripHexPrefix(wallet.getPrivateKey())
const privKeyBuffer = new Buffer(privKey, 'hex')
const sig = sigUtil.decrypt(encryptedData, privKey)
return Promise.resolve(sig)
}

// personal_signTypedData, signs data along with the schema
signTypedData (withAccount, typedData, opts = {}) {
return this.signTypedData_v1(withAccount, typedData, opts);
Expand Down
25 changes: 17 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ describe('simple-keyring', () => {
assert.equal(restored, address, 'recovered address')
})
})

describe('#decryptMessage', () => {
it('returns the expected value', async () => {
const address = '0xbe93f9bacbcffc8ee6663f2647917ed7a20a57bb'
const privateKey = new Buffer('6969696969696969696969696969696969696969696969696969696969696969', 'hex')
const privKeyHex = ethUtil.bufferToHex(privateKey)
const message = 'Hello world!'
const encryptedMessage = sigUtil.encrypt(sigUtil.getEncryptionPublicKey(privateKey), {'data': message}, 'x25519-xsalsa20-poly1305')

await keyring.deserialize([privKeyHex])
const decryptedMessage = await keyring.decryptMessage(address, encryptedMessage)
assert.equal(message, decryptedMessage, 'signature matches')
})
})


describe('#signTypedData_v4 signature verification', () => {
Expand Down