Skip to content

Commit

Permalink
Use Uint8Array in signTx() & broadcastSignedTx()
Browse files Browse the repository at this point in the history
  • Loading branch information
assafmo committed Jun 9, 2023
1 parent 74623c3 commit 7acc51f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- API breaking: `secretjs.tx.signTx()` now returns `txBytes` as a `Uint8Array` instead of a base64 `string`.
- API breaking: `secretjs.tx.broadcastSignedTx()` now receives `txBytes` as a `Uint8Array` instead of a base64 `string`.

## 1.9.3

Fix a bug where error messages would sometimes not decrypt on highly nested contract calls.
Expand Down
44 changes: 36 additions & 8 deletions src/secret_network_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,24 @@ export type TxSender = {
*/
broadcast: (messages: Msg[], txOptions?: TxOptions) => Promise<TxResponse>;

signTx: (messages: Msg[], txOptions?: TxOptions) => Promise<string>;
/**
* Prepare and sign an array of messages as a transaction
* @async
* @param {Msg[]} messages - Array of messages to prepare and sign
* @param {TxOptions} [txOptions] - An optional object of transaction options
* @returns {Promise<Uint8Array>} Returns a Promise that resolves txBytes, which can be passed into broadcastSignedTx().
*/
signTx: (messages: Msg[], txOptions?: TxOptions) => Promise<Uint8Array>;

/**
* Broadcast a signed transactions
* @async
* @param {Uint8Array} txBytes - Signed transaction bytes, can be the output of signTx()
* @param {TxOptions} [txOptions] - An optional object of transaction options
* @returns {Promise<TxResponse>}
*/
broadcastSignedTx: (
signedMessage: string,
txBytes: Uint8Array,
txOptions?: TxOptions,
) => Promise<TxResponse>;

Expand Down Expand Up @@ -1595,20 +1610,33 @@ export class SecretNetworkClient {
}
}

/**
* Prepare and sign an array of messages as a transaction
* @async
* @private
* @param {Msg[]} messages - Array of messages to prepare and sign
* @param {TxOptions} [txOptions] - An optional object of transaction options
* @returns {Promise<Uint8Array>} Returns a Promise that resolves txBytes, which can be passed into broadcastSignedTx().
*/
private async signTx(
messages: Msg[],
txOptions?: TxOptions,
): Promise<string> {
let signed = await this.prepareAndSign(messages, txOptions);

return toBase64(signed);
): Promise<Uint8Array> {
return this.prepareAndSign(messages, txOptions);
}

/**
* Broadcast a signed transactions
* @async
* @private
* @param {Uint8Array} txBytes - Signed transaction bytes, can be the output of signTx()
* @param {TxOptions} [txOptions] - An optional object of transaction options
* @returns {Promise<TxResponse>}
*/
private async broadcastSignedTx(
messages: string,
txBytes: Uint8Array,
txOptions?: TxOptions,
): Promise<TxResponse> {
let txBytes = fromBase64(messages);
return this.broadcastTx(
txBytes,
txOptions?.broadcastTimeoutMs ?? 60_000,
Expand Down

0 comments on commit 7acc51f

Please sign in to comment.