hashgraph / hedera-sdk-js

Hedera™ Hashgraph SDK for JavaScript/TypeScript
https://docs.hedera.com/guides/docs/sdks
Apache License 2.0
256 stars 140 forks source link

Enable Client construction without a private key #2132

Open gregscullard opened 8 months ago

gregscullard commented 8 months ago

Problem

There are occasions when I want to create a Client with a known accountId which isn't mine so that I can construct a transaction with the transactionId and nodeIds set which is done with freezeWith(client) so that I can export the transaction bytes for signing.

However, the Client construction requires a private key, one solution is to provide a dummy key, but this results in additional (invalid) signatures being added to the transaction.

Solution

Enable client construction without a private key, only an accountId and skip adding signatures if the private key is null/undefined on .freezeWith (and maybe other operations).

Alternatives

Appreciate a signer is an alternative option, but in some use cases, sending the transaction bytes off to be signed doesn't result in an immediate (or even ever) signature response, I could for example "email (!)" the transaction bytes to someone who will take care of signing and executing.

gregscullard commented 8 months ago

Note: Raised here, but this equally applies to other SDKs.

seromenho commented 7 months ago

@gregscullard if you haven't solved it already, you can create a client without the private key. I've implemented what you just described. The trick is that you will need to set both the transaction id and the nonce manually.

Try this:

const client = Client.forTestnet()
const payerAccountId = AccountId.fromString(PAYER_ACCOUNT_STRING)
const transactionId = TransactionId.generate(payerAccountId)
const nodeAccountIds = [new AccountId(3)]
const tx = ....
...
.setTransactionId(transactionId)
.setNodeAccountIds(nodeAccountIds)
.freezeWith(client)