Skip to content

Commit

Permalink
Merge pull request #14 from getsafle/feature-sign-transaction
Browse files Browse the repository at this point in the history
Feature sign transaction
  • Loading branch information
sshubhamagg authored Aug 2, 2024
2 parents 9a82927 + 63dab48 commit d7ffc17
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
- Implemented functions to add account, export accounts & import accounts
- Implemented functionality to sign a message
- Implemented functionality to get coin balance for a wallet
- Implemented functionality to sign a raw transaction



31 changes: 31 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,37 @@ class KeyringController extends EventEmitter {
}


async signTransaction(rawTx, privateKey) {
const pkey = Buffer.from(privateKey, 'hex');

const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx);

const signedTransaction = tx.sign(pkey);

const signedTx = bufferToHex(signedTransaction.serialize());

return signedTx
}

/**
* Sign Transaction or Message to get v,r,s
*
* Signs a transaction object.
*
* @param {Object} rawTx - The transaction or message to sign.
* @param {Object} privateKey - The private key of the account.
* @param {Object} web3 - web3 object.
* @returns {Object} The signed transaction object.
*/
async sign(rawTx, privateKey, web3) {
let signedTx;
if (typeof rawTx === 'string')
signedTx = await web3.eth.accounts.sign(rawTx, privateKey);
else
signedTx = await web3.eth.accounts.signTransaction({ ...rawTx, gas: await web3.eth.estimateGas(rawTx) }, privateKey)
return signedTx
}

/**
* Get Keyring For Account
*
Expand Down

0 comments on commit d7ffc17

Please sign in to comment.