-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cactus-plugin-ledger-connector-ethereum): add signing utils
- Add methods for signing transactions to ethereum connector. - Modify offline signing test to use this new method. - Modify electricity trade sample to use new signing method. - Add offline signature section to ethereum connector readme. Co-authored-by: Peter Somogyvari <peter.somogyvari@accenture.com> Signed-off-by: Michal Bajer <michal.bajer@fujitsu.com> Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
- Loading branch information
Showing
10 changed files
with
225 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/cactus-plugin-ledger-connector-ethereum/src/main/typescript/sign-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Chain, Common } from "@ethereumjs/common"; | ||
import { | ||
AccessListEIP2930Transaction, | ||
BlobEIP4844Transaction, | ||
FeeMarketEIP1559Transaction, | ||
LegacyTransaction, | ||
TransactionFactory, | ||
TypedTxData, | ||
} from "@ethereumjs/tx"; | ||
|
||
type CustomChainArg = Parameters<typeof Common.custom>[0]; | ||
|
||
/** | ||
* Sign ethereum transaction data offline. Can be used with both mainnet and custom chains. | ||
* | ||
* @param txData transaction data (format must be compatible with @ethereumjs/tx) | ||
* @param privateKey HEX private signing key | ||
* @param customChainInfo optional custom chain information (default: mainnet) | ||
* | ||
* @returns serialized transaction, txId (hash) and signedTransaction object | ||
*/ | ||
export function signTransaction( | ||
txData: TypedTxData, | ||
privateKey: string, | ||
customChainInfo?: CustomChainArg, | ||
): { | ||
serializedTransactionHex: string; | ||
txId: string; | ||
signedTransactionObject: | ||
| LegacyTransaction | ||
| AccessListEIP2930Transaction | ||
| FeeMarketEIP1559Transaction | ||
| BlobEIP4844Transaction; | ||
} { | ||
let chainConfiguration = new Common({ chain: Chain.Mainnet }); | ||
if (customChainInfo) { | ||
chainConfiguration = Common.custom(customChainInfo); | ||
} | ||
|
||
const transaction = TransactionFactory.fromTxData(txData, { | ||
common: chainConfiguration, | ||
}); | ||
if (privateKey.toLowerCase().startsWith("0x")) { | ||
privateKey = privateKey.slice(2); | ||
} | ||
const signedTransaction = transaction.sign(Buffer.from(privateKey, "hex")); | ||
|
||
const hash = signedTransaction.hash(); | ||
const txId = "0x" + Buffer.from(hash).toString("hex"); | ||
const serializedTransaction = signedTransaction.serialize(); | ||
const serializedTransactionHex = | ||
"0x" + Buffer.from(serializedTransaction).toString("hex"); | ||
|
||
return { | ||
serializedTransactionHex, | ||
txId, | ||
signedTransactionObject: signedTransaction, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters