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

Fix Sign Transaction Signature #125

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
11 changes: 4 additions & 7 deletions src/lib/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,14 @@ export function base64StringToTransaction<T extends Transaction>(transactionByte
* @param transaction - a base64 encoded string of proto.TransactionBody.encode().finish()
* @returns `string`
* */
export function transactionToTransactionBody<T extends Transaction>(
transaction: T,
nodeAccountId: AccountId,
) {
export function transactionToTransactionBody<T extends Transaction>(transaction: T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm uncomfortable changing the signature of a public method, this could result in a breaking change to existing implementations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this implementation should remain and Signer.ts changed from

    const transactionBody: proto.TransactionBody = transactionToTransactionBody(
      transaction,
      this._getRandomNodes(1)[0],
    )

to re-using the nodeAccountId from transaction (if that's possible) ?

This would no longer be a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! 🙂

From our testing and research, the method used in this PR is the only way to get the signed transaction validated by the Hedera network. The previous approach signed the transaction in a way that the Hedera Network couldn't verify.

That said, we can leave the nodeAccountId parameter for compatibility, although it's currently unused and likely not functional in its previous form. @gregscullard, should I include the nodeAccountId as you suggested?

If there is a better method to obtain the signed transaction bytes than the one used in this PR, please let me know. Based on our testing a few weeks ago, this was the only approach that worked.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave it to the community of builders/users to determine whether a breaking change in the signature of the transaction is acceptable or not. The change is minor and would likely be implemented as a result of changes to the application, so tested and working before deployed. Breaking but not critically so I guess.

// This is a private function, though provides the capabilities to construct a proto.TransactionBody
//@ts-ignore
return transaction._makeTransactionBody(nodeAccountId)
return transaction._signedTransactions.get(0)!.bodyBytes!
}

export function transactionBodyToBase64String(transactionBody: proto.TransactionBody) {
return Uint8ArrayToBase64String(proto.TransactionBody.encode(transactionBody).finish())
export function transactionBodyToBase64String(transactionBody: Uint8Array) {
return Uint8ArrayToBase64String(transactionBody)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/dapp/DAppConnector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
*/

import { AccountId, AccountInfoQuery, LedgerId, TopicCreateTransaction } from '@hashgraph/sdk'
import { AccountInfoQuery, LedgerId, TopicCreateTransaction } from '@hashgraph/sdk'
import {
DAppConnector,
ExecuteTransactionParams,
Expand Down Expand Up @@ -295,7 +295,7 @@ describe('DAppConnector', () => {
signerAccountId: testUserAccountId.toString(),
transactionBody: transactionBodyToBase64String(
// must specify a node account id for the transaction body
transactionToTransactionBody(transaction, AccountId.fromString('0.0.3')),
transactionToTransactionBody(transaction),
MiguelLZPF marked this conversation as resolved.
Show resolved Hide resolved
),
}

Expand Down
7 changes: 2 additions & 5 deletions test/wallet/methods/wallet-signTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
*/

import { TransferTransaction, AccountId, Hbar } from '@hashgraph/sdk'
import { TransferTransaction, Hbar } from '@hashgraph/sdk'
import {
HederaChainId,
SignTransactionResponse,
Expand Down Expand Up @@ -49,10 +49,7 @@ describe(Wallet.name, () => {
.setMaxTransactionFee(new Hbar(1))
.addHbarTransfer('0.0.123', new Hbar(10))
.addHbarTransfer('0.0.321', new Hbar(-10))
const transactionBody = transactionToTransactionBody(
transaction,
AccountId.fromString('0.0.3'),
)
const transactionBody = transactionToTransactionBody(transaction)
const respondSessionRequestSpy = jest.spyOn(wallet, 'respondSessionRequest')

const response = await wallet.hedera_signTransaction(
Expand Down
Loading