-
Notifications
You must be signed in to change notification settings - Fork 302
feat: added canton offer withdrawn builder #8050
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
Open
ravibitgo
wants to merge
1
commit into
master
Choose a base branch
from
COIN-7486
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+309
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
139 changes: 139 additions & 0 deletions
139
modules/sdk-coin-canton/src/lib/transferOfferWithdrawnBuilder.ts
This file contains hidden or 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,139 @@ | ||
| import { InvalidTransactionError, PublicKey, TransactionType } from '@bitgo/sdk-core'; | ||
| import { BaseCoin as CoinConfig } from '@bitgo/statics'; | ||
| import { CantonPrepareCommandResponse, CantonTransferOfferWithdrawnRequest } from './iface'; | ||
| import { TransactionBuilder } from './transactionBuilder'; | ||
| import { Transaction } from './transaction/transaction'; | ||
| import utils from './utils'; | ||
|
|
||
| export class TransferOfferWithdrawnBuilder extends TransactionBuilder { | ||
| private _commandId: string; | ||
| private _contractId: string; | ||
| private _actAsPartyId: string; | ||
| private _tokenName: string; | ||
| constructor(_coinConfig: Readonly<CoinConfig>) { | ||
| super(_coinConfig); | ||
| } | ||
|
|
||
| initBuilder(tx: Transaction): void { | ||
| super.initBuilder(tx); | ||
| this.setTransactionType(); | ||
| } | ||
|
|
||
| get transactionType(): TransactionType { | ||
| return TransactionType.TransferOfferWithdrawn; | ||
| } | ||
|
|
||
| setTransactionType(): void { | ||
| this.transaction.transactionType = TransactionType.TransferOfferWithdrawn; | ||
| } | ||
|
|
||
| setTransaction(transaction: CantonPrepareCommandResponse): void { | ||
| this.transaction.prepareCommand = transaction; | ||
| } | ||
|
|
||
| /** @inheritDoc */ | ||
| addSignature(publicKey: PublicKey, signature: Buffer): void { | ||
| if (!this.transaction) { | ||
| throw new InvalidTransactionError('transaction is empty!'); | ||
| } | ||
| this._signatures.push({ publicKey, signature }); | ||
| const pubKeyBase64 = utils.getBase64FromHex(publicKey.pub); | ||
| this.transaction.signerFingerprint = utils.getAddressFromPublicKey(pubKeyBase64); | ||
| this.transaction.signatures = signature.toString('base64'); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the unique id for the transfer offer withdrawn | ||
| * Also sets the _id of the transaction | ||
| * | ||
| * @param id - A uuid | ||
| * @returns The current builder instance for chaining. | ||
| * @throws Error if id is empty. | ||
| */ | ||
| commandId(id: string): this { | ||
| if (!id || !id.trim()) { | ||
| throw new Error('commandId must be a non-empty string'); | ||
| } | ||
| this._commandId = id.trim(); | ||
| // also set the transaction _id | ||
| this.transaction.id = id.trim(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the contract id the receiver needs to withdraw | ||
| * @param id - canton withdrawn contract id | ||
| * @returns The current builder instance for chaining. | ||
| * @throws Error if id is empty. | ||
| */ | ||
| contractId(id: string): this { | ||
| if (!id || !id.trim()) { | ||
| throw new Error('contractId must be a non-empty string'); | ||
| } | ||
| this._contractId = id.trim(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * The sender who wants to withdraw the offer | ||
| * | ||
| * @param id - the sender party id | ||
| * @returns The current builder instance for chaining. | ||
| * @throws Error if id is empty. | ||
| */ | ||
| actAs(id: string): this { | ||
| if (!id || !id.trim()) { | ||
| throw new Error('actAsPartyId must be a non-empty string'); | ||
| } | ||
| this._actAsPartyId = id.trim(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * The token name to withdraw the offer | ||
| * @param name - the bitgo name of the asset | ||
| * @returns The current builder instance for chaining. | ||
| * @throws Error if name is empty. | ||
| */ | ||
| tokenName(name: string): this { | ||
| if (!name || !name.trim()) { | ||
| throw new Error('tokenName must be a non-empty string'); | ||
| } | ||
| this._tokenName = name.trim(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Builds and returns the CantonTransferOfferWithdrawnRequest object from the builder's internal state. | ||
| * | ||
| * This method performs validation before constructing the object. If required fields are | ||
| * missing or invalid, it throws an error. | ||
| * | ||
| * @returns {CantonTransferOfferWithdrawnRequest} - A fully constructed and validated request object for transfer offer withdrawal. | ||
| * @throws {Error} If any required field is missing or fails validation. | ||
| */ | ||
| toRequestObject(): CantonTransferOfferWithdrawnRequest { | ||
| this.validate(); | ||
|
|
||
| return { | ||
| commandId: this._commandId, | ||
| contractId: this._contractId, | ||
| verboseHashing: false, | ||
| actAs: [this._actAsPartyId], | ||
| readAs: [], | ||
| tokenName: this._tokenName, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Validates the internal state of the builder before building the request object. | ||
| * | ||
| * @private | ||
| * @throws {Error} If any required field is missing or invalid. | ||
| */ | ||
| private validate(): void { | ||
| if (!this._commandId) throw new Error('commandId is missing'); | ||
| if (!this._contractId) throw new Error('contractId is missing'); | ||
| if (!this._actAsPartyId) throw new Error('receiver partyId is missing'); | ||
| } | ||
| } |
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
...sdk-coin-canton/test/unit/builder/transferOfferWithdrawn/transferOfferWithdrawnBuilder.ts
This file contains hidden or 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,70 @@ | ||
| import assert from 'assert'; | ||
| import should from 'should'; | ||
|
|
||
| import { coins } from '@bitgo/statics'; | ||
|
|
||
| import { TransferAcceptanceBuilder, Transaction, TransferOfferWithdrawnBuilder } from '../../../../src'; | ||
| import { CantonTransferAcceptRejectRequest } from '../../../../src/lib/iface'; | ||
|
|
||
| import { | ||
| CantonTokenTransferOfferWithdrawnPrepareResponse, | ||
| CantonTransferOfferWithdrawnPrepareResponse, | ||
| TransferAcceptance, | ||
| } from '../../../resources'; | ||
|
|
||
| describe('Transfer Offer Withdrawn Builder', () => { | ||
| it('should get the transfer offer withdrawn request object', function () { | ||
| const txBuilder = new TransferOfferWithdrawnBuilder(coins.get('tcanton')); | ||
| const transferOfferWithdrawnTx = new Transaction(coins.get('tcanton')); | ||
| txBuilder.initBuilder(transferOfferWithdrawnTx); | ||
| txBuilder.setTransaction(CantonTransferOfferWithdrawnPrepareResponse); | ||
| const { commandId, contractId, partyId } = TransferAcceptance; | ||
| txBuilder.commandId(commandId).contractId(contractId).actAs(partyId); | ||
| const requestObj: CantonTransferAcceptRejectRequest = txBuilder.toRequestObject(); | ||
| should.exist(requestObj); | ||
| assert.equal(requestObj.commandId, commandId); | ||
| assert.equal(requestObj.contractId, contractId); | ||
| assert.equal(requestObj.actAs.length, 1); | ||
| const actAs = requestObj.actAs[0]; | ||
| assert.equal(actAs, partyId); | ||
| }); | ||
|
|
||
| it('should validate raw canton transfer offer withdrawn transaction', function () { | ||
| const txBuilder = new TransferAcceptanceBuilder(coins.get('tcanton')); | ||
| const transferOfferWithdrawnTx = new Transaction(coins.get('tcanton')); | ||
| txBuilder.initBuilder(transferOfferWithdrawnTx); | ||
| txBuilder.setTransaction(CantonTransferOfferWithdrawnPrepareResponse); | ||
| txBuilder.validateRawTransaction(CantonTransferOfferWithdrawnPrepareResponse.preparedTransaction); | ||
| }); | ||
|
|
||
| it('should validate raw canton token transfer offer withdrawn transaction', function () { | ||
| const txBuilder = new TransferAcceptanceBuilder(coins.get('tcanton')); | ||
| const transferOfferWithdrawnTx = new Transaction(coins.get('tcanton')); | ||
| txBuilder.initBuilder(transferOfferWithdrawnTx); | ||
| txBuilder.setTransaction(CantonTokenTransferOfferWithdrawnPrepareResponse); | ||
| txBuilder.validateRawTransaction(CantonTokenTransferOfferWithdrawnPrepareResponse.preparedTransaction); | ||
| }); | ||
|
|
||
| it('should validate the transaction', function () { | ||
| const txBuilder = new TransferAcceptanceBuilder(coins.get('tcanton')); | ||
| const transferOfferWithdrawnTx = new Transaction(coins.get('tcanton')); | ||
| transferOfferWithdrawnTx.prepareCommand = CantonTransferOfferWithdrawnPrepareResponse; | ||
| txBuilder.initBuilder(transferOfferWithdrawnTx); | ||
| txBuilder.setTransaction(CantonTransferOfferWithdrawnPrepareResponse); | ||
| txBuilder.validateTransaction(transferOfferWithdrawnTx); | ||
| }); | ||
|
|
||
| it('should throw error in validating raw transaction', function () { | ||
| const txBuilder = new TransferAcceptanceBuilder(coins.get('tcanton')); | ||
| const transferOfferWithdrawnTx = new Transaction(coins.get('tcanton')); | ||
| txBuilder.initBuilder(transferOfferWithdrawnTx); | ||
| const invalidPrepareResponse = CantonTransferOfferWithdrawnPrepareResponse; | ||
| invalidPrepareResponse.preparedTransactionHash = '+vlIXv6Vgd2ypPXD0mrdn7RlcSH4c2hCRj2/tXqqUVs='; | ||
| txBuilder.setTransaction(invalidPrepareResponse); | ||
| try { | ||
| txBuilder.validateRawTransaction(invalidPrepareResponse.preparedTransaction); | ||
| } catch (e) { | ||
| assert.equal(e.message, 'invalid raw transaction, hash not matching'); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -95,6 +95,8 @@ export enum TransactionType { | |
| TransferAcknowledge, | ||
| // canton transfer reject, 2-step | ||
| TransferReject, | ||
| // canton transfer offer withdrawn, 2-step | ||
| TransferOfferWithdrawn, | ||
|
|
||
| // trx | ||
| FREEZE, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Useless conditional Warning
Copilot Autofix
AI 8 minutes ago
In general, a "useless conditional" where a condition is always
trueor alwaysfalseshould be removed or rewritten so that it expresses an actual decision. Keeping such a condition is misleading and can hide bugs or confuse future readers.Here, CodeQL indicates that
!senderis alwaystrueat the point ofif (!sender) { sender = receiver; }. That means that in all reachable executions of this branch,senderis falsy, so the body of theifalways runs. The current effective behavior is: whenever we have awithdrawnNode(ortokenWithdrawnNodein the similar block), we setreceiverto the owner party and then always setsenderto the same value. To keep behavior unchanged but make the code honest and clear, we should remove theif (!sender)wrapper and directly assignsender = receiver;when we are in these branches and have apartyowner.Concretely, in
modules/sdk-coin-canton/src/lib/utils.ts, in thewithdrawnNodeblock around line 266, change:to:
This preserves the runtime behavior implied by CodeQL’s analysis (since the inner
ifwas always true) while removing the useless condition. The similar pattern exists in thetokenWithdrawnNodeblock around lines 283–288; to maintain consistency and avoid a similar useless-conditional warning there (and to reflect the same semantic “sender defaults to owner”), apply the same simplification: always setsender = receiver;inside thatownerDatacheck. No new imports, methods, or definitions are required.