-
Notifications
You must be signed in to change notification settings - Fork 300
feat(sdk-coin-canton): added transfer acceptance builder #7321
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
Merged
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
111 changes: 111 additions & 0 deletions
111
modules/sdk-coin-canton/src/lib/transferAcceptanceBuilder.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,111 @@ | ||
| import { TransactionType } from '@bitgo/sdk-core'; | ||
| import { BaseCoin as CoinConfig } from '@bitgo/statics'; | ||
| import { CantonPrepareCommandResponse, CantonTransferAcceptRequest } from './iface'; | ||
| import { TransactionBuilder } from './transactionBuilder'; | ||
| import { Transaction } from './transaction/transaction'; | ||
|
|
||
| export class TransferAcceptanceBuilder extends TransactionBuilder { | ||
| private _commandId: string; | ||
| private _contractId: string; | ||
| private _actAsPartyId: string; | ||
| constructor(_coinConfig: Readonly<CoinConfig>) { | ||
| super(_coinConfig); | ||
| } | ||
|
|
||
| initBuilder(tx: Transaction): void { | ||
| super.initBuilder(tx); | ||
| this.setTransactionType(); | ||
| } | ||
|
|
||
| get transactionType(): TransactionType { | ||
| return TransactionType.TransferAccept; | ||
| } | ||
|
|
||
| setTransactionType(): void { | ||
| this.transaction.transactionType = TransactionType.TransferAccept; | ||
| } | ||
|
|
||
| setTransaction(transaction: CantonPrepareCommandResponse): void { | ||
| this.transaction.prepareCommand = transaction; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the unique id for the transfer acceptance | ||
| * 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.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 acceptance contract id the receiver needs to accept | ||
| * @param id - canton acceptance contract id | ||
| * @returns The current builder instance for chaining. | ||
| * @throws Error if id is empty. | ||
| */ | ||
| contractId(id: string): this { | ||
| if (!id.trim()) { | ||
| throw new Error('contractId must be a non-empty string'); | ||
| } | ||
| this._contractId = id.trim(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the receiver of the acceptance | ||
| * | ||
| * @param id - the receiver party id (address) | ||
| * @returns The current builder instance for chaining. | ||
| * @throws Error if id is empty. | ||
| */ | ||
| actAs(id: string): this { | ||
| if (!id.trim()) { | ||
| throw new Error('actAsPartyId must be a non-empty string'); | ||
| } | ||
| this._actAsPartyId = id.trim(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Builds and returns the CantonTransferAcceptRequest 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 {CantonTransferAcceptRequest} - A fully constructed and validated request object for transfer acceptance. | ||
| * @throws {Error} If any required field is missing or fails validation. | ||
| */ | ||
| toRequestObject(): CantonTransferAcceptRequest { | ||
| this.validate(); | ||
|
|
||
| return { | ||
| commandId: this._commandId, | ||
| contractId: this._contractId, | ||
| verboseHashing: false, | ||
| actAs: [this._actAsPartyId], | ||
| readAs: [], | ||
ravibitgo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * 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'); | ||
| } | ||
| } | ||
Large diffs are not rendered by default.
Oops, something went wrong.
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
71 changes: 71 additions & 0 deletions
71
modules/sdk-coin-canton/test/unit/builder/transferAccept/transferAcceptBuilder.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,71 @@ | ||
| import assert from 'assert'; | ||
| import should from 'should'; | ||
|
|
||
| import { coins } from '@bitgo/statics'; | ||
|
|
||
| import { TransferAcceptanceBuilder, Transaction } from '../../../../src'; | ||
| import { CantonTransferAcceptRequest } from '../../../../src/lib/iface'; | ||
|
|
||
| import { TransferAcceptance, TransferAcceptancePrepareResponse } from '../../../resources'; | ||
|
|
||
| describe('Transfer Acceptance Builder', () => { | ||
| it('should get the transfer acceptance request object', function () { | ||
| const txBuilder = new TransferAcceptanceBuilder(coins.get('tcanton')); | ||
| const transferAcceptanceTx = new Transaction(coins.get('tcanton')); | ||
| txBuilder.initBuilder(transferAcceptanceTx); | ||
| const { commandId, contractId, partyId } = TransferAcceptance; | ||
| txBuilder.commandId(commandId).contractId(contractId).actAs(partyId); | ||
| const requestObj: CantonTransferAcceptRequest = 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 transaction', function () { | ||
| const txBuilder = new TransferAcceptanceBuilder(coins.get('tcanton')); | ||
| const transferAcceptanceTx = new Transaction(coins.get('tcanton')); | ||
| txBuilder.initBuilder(transferAcceptanceTx); | ||
| txBuilder.setTransaction(TransferAcceptancePrepareResponse); | ||
| txBuilder.validateRawTransaction(TransferAcceptancePrepareResponse.preparedTransaction); | ||
| }); | ||
|
|
||
| it('should validate the transaction', function () { | ||
| const txBuilder = new TransferAcceptanceBuilder(coins.get('tcanton')); | ||
| const transferAcceptanceTx = new Transaction(coins.get('tcanton')); | ||
| transferAcceptanceTx.prepareCommand = TransferAcceptancePrepareResponse; | ||
| txBuilder.initBuilder(transferAcceptanceTx); | ||
| txBuilder.setTransaction(TransferAcceptancePrepareResponse); | ||
| txBuilder.validateTransaction(transferAcceptanceTx); | ||
| }); | ||
|
|
||
| it('should throw error in validating raw transaction', function () { | ||
| const txBuilder = new TransferAcceptanceBuilder(coins.get('tcanton')); | ||
| const transferAcceptanceTx = new Transaction(coins.get('tcanton')); | ||
| txBuilder.initBuilder(transferAcceptanceTx); | ||
| const invalidPrepareResponse = TransferAcceptancePrepareResponse; | ||
| invalidPrepareResponse.preparedTransactionHash = '+vlIXv6Vgd2ypPXD0mrdn7RlcSH4c2hCRj2/tXqqUVs='; | ||
| txBuilder.setTransaction(invalidPrepareResponse); | ||
| try { | ||
| txBuilder.validateRawTransaction(invalidPrepareResponse.preparedTransaction); | ||
| } catch (e) { | ||
| assert.equal(e.message, 'invalid raw transaction, hash not matching'); | ||
| } | ||
| }); | ||
|
|
||
| it('should throw error in validating raw transaction', function () { | ||
| const txBuilder = new TransferAcceptanceBuilder(coins.get('tcanton')); | ||
| const oneStepEnablementTx = new Transaction(coins.get('tcanton')); | ||
| txBuilder.initBuilder(oneStepEnablementTx); | ||
| const invalidPrepareResponse = TransferAcceptancePrepareResponse; | ||
| invalidPrepareResponse.preparedTransactionHash = '+vlIXv6Vgd2ypPXD0mrdn7RlcSH4c2hCRj2/tXqqUVs='; | ||
| oneStepEnablementTx.prepareCommand = invalidPrepareResponse; | ||
| try { | ||
| txBuilder.validateTransaction(oneStepEnablementTx); | ||
| } catch (e) { | ||
| assert.equal(e.message, 'invalid transaction'); | ||
| } | ||
| }); | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.