From c511e059c46a26c9dec2e55b627324ddee2cd119 Mon Sep 17 00:00:00 2001 From: SDargarh Date: Mon, 22 Jul 2024 17:49:15 +0530 Subject: [PATCH] Added functionality to sign message --- CHANGELOG.md | 3 ++- package-lock.json | 3 ++- package.json | 3 ++- src/index.js | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1ea987..6d16828 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,4 +5,5 @@ - Implemented Keyring functionality to enable account generation and export keys - Added getAccounts() method to fetch list of generated accounts - Added importWallet() to import account using privateKey -- Added initial test \ No newline at end of file +- Added initial test +- Added functionality to sign message \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index e94cf59..634632b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,8 @@ "ed25519-hd-key": "^1.3.0", "mocha": "^10.7.0", "nyc": "^17.0.0", - "obs-store": "^4.0.3" + "obs-store": "^4.0.3", + "tweetnacl": "^1.0.3" } }, "node_modules/@ampproject/remapping": { diff --git a/package.json b/package.json index 83661ea..12dd9ee 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "ed25519-hd-key": "^1.3.0", "mocha": "^10.7.0", "nyc": "^17.0.0", - "obs-store": "^4.0.3" + "obs-store": "^4.0.3", + "tweetnacl": "^1.0.3" } } diff --git a/src/index.js b/src/index.js index e6ea0c5..6d39fb5 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ const ObservableStore = require("obs-store"); const bs58 = require("bs58"); const helper = require("./helper"); +const nacl = require('tweetnacl'); const { solana: { HD_PATH }, solana_connection: { MAINNET }} = require('./config') @@ -55,6 +56,19 @@ class KeyringController { } } + async signMessage(message, _address) { + const { mnemonic, address } = this.store.getState() + const idx = address.indexOf(_address); + + if (idx < 0) + throw "Invalid address, the address is not available in the wallet" + + const accountDetails = helper.setupAccount(mnemonic, helper.getHDPath(idx)) + const msg = Buffer.from(message) + + return { signedMessage: bs58.encode(nacl.sign.detached(msg, accountDetails.secretKey)) }; + } + persistAllAddress(_address) { const { address } = this.store.getState(); let newAdd = address;