Skip to content

Commit

Permalink
Merge pull request #5016 from BitGo/WIN-3518
Browse files Browse the repository at this point in the history
feat(sdk-coin-xrp): add transaction builder
  • Loading branch information
nvrakesh06 authored Oct 21, 2024
2 parents db35f38 + 392222b commit 0b0ccf4
Show file tree
Hide file tree
Showing 18 changed files with 1,446 additions and 17 deletions.
7 changes: 3 additions & 4 deletions modules/sdk-coin-xrp/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './xrp';
export * from './txrp';
export * from './lib';
export * from './register';
export * from './lib/iface';
export * from './lib/utils';
export * from './txrp';
export * from './xrp';
export * from './xrpToken';
73 changes: 73 additions & 0 deletions modules/sdk-coin-xrp/src/lib/accountSetBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { AccountSet } from 'xrpl';
import { XrpTransactionType } from './iface';
import { Transaction } from './transaction';
import { TransactionBuilder } from './transactionBuilder';
import utils from './utils';

export class AccountSetBuilder extends TransactionBuilder {
protected _setFlag: number;
protected _messageKey: string;
constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
}

protected get transactionType(): TransactionType {
return TransactionType.AccountUpdate;
}

protected get xrpTransactionType(): XrpTransactionType.AccountSet {
return XrpTransactionType.AccountSet;
}

setFlag(setFlag: number): TransactionBuilder {
utils.validateAccountSetFlag(setFlag);
this._setFlag = setFlag;
return this;
}

messageKey(messageKey: string): TransactionBuilder {
if (typeof messageKey !== 'string') {
throw new BuildTransactionError('Invalid message key');
}
this._messageKey = messageKey;
return this;
}

initBuilder(tx: Transaction): void {
super.initBuilder(tx);

const { setFlag, messageKey } = tx.toJson();
if (setFlag) {
this.setFlag(setFlag);
}

if (messageKey) {
this.messageKey(messageKey);
}
}

/** @inheritdoc */
protected async buildImplementation(): Promise<Transaction> {
if (!this._sender) {
throw new BuildTransactionError('Sender must be set before building the transaction');
}

const accountSetFields: AccountSet = {
TransactionType: this.xrpTransactionType,
Account: this._sender,
};
if (this._setFlag) {
accountSetFields.SetFlag = this._setFlag;
}

if (this._messageKey) {
accountSetFields.MessageKey = this._messageKey;
}

this._specificFields = accountSetFields;

return await super.buildImplementation();
}
}
28 changes: 28 additions & 0 deletions modules/sdk-coin-xrp/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// https://xrpl.org/signerlistset.html
export const MAX_SIGNERS = 32;
export const MIN_SIGNERS = 1;
export const MIN_SIGNER_QUORUM = 1;

// https://xrpl.org/accountset.html#accountset-flags
export const VALID_ACCOUNT_SET_FLAGS = [
5, // asfAccountTxnID
16, // asfAllowTrustLineClawback
10, // asfAuthorizedNFTokenMinter
8, // asfDefaultRipple
9, // asfDepositAuth
4, // asfDisableMaster
13, // asfDisallowIncomingCheck
12, // asfDisallowIncomingNFTokenOffer
14, // asfDisallowIncomingPayChan
15, // asfDisallowIncomingTrustline
3, // asfDisallowXRP
7, // asfGlobalFreeze
6, // asfNoFreeze
2, // asfRequireAuth
1, // asfRequireDest
];

// Global flags for bitgo address
export const USER_KEY_SETTING_FLAG = 65536;
export const MASTER_KEY_DEACTIVATION_FLAG = 1048576;
export const REQUIRE_DESTINATION_TAG_FLAG = 131072;
64 changes: 62 additions & 2 deletions modules/sdk-coin-xrp/src/lib/iface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import {
InitiateRecoveryOptions as BaseInitiateRecoveryOptions,
SignTransactionOptions as BaseSignTransactionOptions,
TransactionExplanation as BaseTransactionExplanation,
VerifyAddressOptions as BaseVerifyAddressOptions,
TransactionExplanation,
TransactionPrebuild,
} from '@bitgo/sdk-core';
import { AccountSet, Payment, Signer, SignerEntry, SignerListSet } from 'xrpl';

export enum XrpTransactionType {
AccountSet = 'AccountSet',
Payment = 'Payment',
SignerListSet = 'SignerListSet',
TrustSet = 'TrustSet',
}

export type XrpTransaction = Payment | AccountSet | SignerListSet;

export interface Address {
address: string;
Expand Down Expand Up @@ -35,7 +45,7 @@ export interface VerifyAddressOptions extends BaseVerifyAddressOptions {
rootAddress: string;
}

export interface RecoveryInfo extends TransactionExplanation {
export interface RecoveryInfo extends BaseTransactionExplanation {
txHex: string;
backupKey?: string;
coin?: string;
Expand Down Expand Up @@ -68,3 +78,53 @@ export interface HalfSignedTransaction {
export interface SupplementGenerateWalletOptions {
rootPrivateKey?: string;
}

export type TransactionExplanation =
| BaseTransactionExplanation
| AccountSetTransactionExplanation
| SignerListSetTransactionExplanation;

export interface AccountSetTransactionExplanation extends BaseTransactionExplanation {
accountSet: {
messageKey?: string;
setFlag: number;
};
}

export interface SignerListSetTransactionExplanation extends BaseTransactionExplanation {
signerListSet: {
signerQuorum: number;
signerEntries: SignerEntry[];
};
}

export interface TxData {
// mandatory fields
from: string;
transactionType: XrpTransactionType;
isMultiSig: boolean;
// optional fields
id?: string;
fee?: string;
flags: number;
sequence?: number;
lastLedgerSequence?: number;
signingPubKey?: string; // if '' then it is a multi sig
txnSignature?: string; // only for single sig
signers?: Signer[]; // only for multi sig
// transfer xrp fields
destination?: string;
destinationTag?: number;
amount?: string;
// account set fields
messageKey?: string;
setFlag?: number;
// signer list set fields
signerQuorum?: number;
signerEntries?: SignerEntry[];
}

export interface SignerDetails {
address: string;
weight: number;
}
12 changes: 12 additions & 0 deletions modules/sdk-coin-xrp/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Utils from './utils';

export * from './constants';
export * from './iface';
export { AccountSetBuilder } from './accountSetBuilder';
export { KeyPair } from './keyPair';
export { Transaction } from './transaction';
export { TransactionBuilder } from './transactionBuilder';
export { TransactionBuilderFactory } from './transactionBuilderFactory';
export { TransferBuilder } from './transferBuilder';
export { WalletInitializationBuilder } from './walletInitializationBuilder';
export { Utils };
Loading

0 comments on commit 0b0ccf4

Please sign in to comment.