-
Notifications
You must be signed in to change notification settings - Fork 300
feat: added transfer reject builder #7358
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
+216
−24
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
123 changes: 123 additions & 0 deletions
123
modules/sdk-coin-canton/src/lib/transferRejectionBuilder.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,123 @@ | ||
| import { InvalidTransactionError, PublicKey, TransactionType } from '@bitgo/sdk-core'; | ||
| import { BaseCoin as CoinConfig } from '@bitgo/statics'; | ||
| import { CantonPrepareCommandResponse, CantonTransferAcceptRejectRequest } from './iface'; | ||
| import { TransactionBuilder } from './transactionBuilder'; | ||
| import { Transaction } from './transaction/transaction'; | ||
| import utils from './utils'; | ||
|
|
||
| export class TransferRejectionBuilder 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.TransferReject; | ||
| } | ||
|
|
||
| setTransactionType(): void { | ||
| this.transaction.transactionType = TransactionType.TransferReject; | ||
| } | ||
|
|
||
| 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 rejection | ||
| * 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 rejection contract id the receiver needs to accept | ||
| * @param id - canton rejection 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; | ||
| } | ||
|
|
||
| /** | ||
| * 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 || !id.trim()) { | ||
| throw new Error('actAsPartyId must be a non-empty string'); | ||
| } | ||
| this._actAsPartyId = id.trim(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Builds and returns the CantonTransferAcceptRejectRequest 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 {CantonTransferAcceptRejectRequest} - A fully constructed and validated request object for transfer acceptance. | ||
| * @throws {Error} If any required field is missing or fails validation. | ||
| */ | ||
| toRequestObject(): CantonTransferAcceptRejectRequest { | ||
| this.validate(); | ||
|
|
||
| return { | ||
| commandId: this._commandId, | ||
| contractId: this._contractId, | ||
| verboseHashing: false, | ||
| actAs: [this._actAsPartyId], | ||
| readAs: [], | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * 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
57 changes: 57 additions & 0 deletions
57
modules/sdk-coin-canton/test/unit/builder/transferReject/transferRejectBuilder.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,57 @@ | ||
| import assert from 'assert'; | ||
| import should from 'should'; | ||
|
|
||
| import { coins } from '@bitgo/statics'; | ||
|
|
||
| import { Transaction, TransferRejectionBuilder } from '../../../../src'; | ||
| import { CantonTransferAcceptRejectRequest } from '../../../../src/lib/iface'; | ||
|
|
||
| import { TransferRejection, TransferRejectionPrepareResponse } from '../../../resources'; | ||
|
|
||
| describe('Transfer Rejection Builder', () => { | ||
| it('should get the transfer rejection request object', function () { | ||
| const txBuilder = new TransferRejectionBuilder(coins.get('tcanton')); | ||
| const tx = new Transaction(coins.get('tcanton')); | ||
| txBuilder.initBuilder(tx); | ||
| const { commandId, contractId, partyId } = TransferRejection; | ||
| 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 transaction', function () { | ||
| const txBuilder = new TransferRejectionBuilder(coins.get('tcanton')); | ||
| const tx = new Transaction(coins.get('tcanton')); | ||
| txBuilder.initBuilder(tx); | ||
| txBuilder.setTransaction(TransferRejectionPrepareResponse); | ||
| txBuilder.validateRawTransaction(TransferRejectionPrepareResponse.preparedTransaction); | ||
| }); | ||
|
|
||
| it('should validate the transaction', function () { | ||
| const txBuilder = new TransferRejectionBuilder(coins.get('tcanton')); | ||
| const tx = new Transaction(coins.get('tcanton')); | ||
| tx.prepareCommand = TransferRejectionPrepareResponse; | ||
| txBuilder.initBuilder(tx); | ||
| txBuilder.setTransaction(TransferRejectionPrepareResponse); | ||
| txBuilder.validateTransaction(tx); | ||
| }); | ||
|
|
||
| it('should throw error in validating raw transaction', function () { | ||
| const txBuilder = new TransferRejectionBuilder(coins.get('tcanton')); | ||
| const tx = new Transaction(coins.get('tcanton')); | ||
| txBuilder.initBuilder(tx); | ||
| const invalidPrepareResponse = TransferRejectionPrepareResponse; | ||
| invalidPrepareResponse.preparedTransactionHash = 'QFxX1WBdq7lZbSc45iKA3J/oOF9mrVLc3DeKphAjb15='; | ||
| 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
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.