Skip to content
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

feat(sdk-coin-xrp): add trustsetBuilder #5024

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
65 changes: 63 additions & 2 deletions modules/sdk-coin-xrp/src/lib/iface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
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, Amount, TrustSet } from 'xrpl';

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

export type XrpTransaction = Payment | AccountSet | SignerListSet | TrustSet;

export interface Address {
address: string;
Expand Down Expand Up @@ -35,7 +46,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 +79,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?: Amount;
// account set fields
messageKey?: string;
setFlag?: number;
// signer list set fields
signerQuorum?: number;
signerEntries?: SignerEntry[];
}

export interface SignerDetails {
address: string;
weight: number;
}
13 changes: 13 additions & 0 deletions modules/sdk-coin-xrp/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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 { TokenTransferBuilder } from './tokenTransferBuilder';
export { WalletInitializationBuilder } from './walletInitializationBuilder';
export { Utils };
99 changes: 99 additions & 0 deletions modules/sdk-coin-xrp/src/lib/tokenTransferBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Amount, IssuedCurrencyAmount, Payment } from 'xrpl';
import { TransactionBuilder } from './transactionBuilder';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core';
import { XrpTransactionType } from './iface';
import { Transaction } from './transaction';
import utils from './utils';
import _ from 'lodash';

export class TokenTransferBuilder extends TransactionBuilder {
private _amount: Amount;
private _destination: string;
private _destinationTag?: number;

constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
}

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

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

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

const { destination, amount, destinationTag } = tx.toJson();
if (!destination) {
throw new BuildTransactionError('Missing destination');
}
if (!amount) {
throw new BuildTransactionError('Missing amount');
}

const normalizeAddress = utils.normalizeAddress({ address: destination, destinationTag });
this.to(normalizeAddress);
this.amount(amount);
}

/**
* Set the receiver address
* @param {string} address - the address with optional destination tag
* @returns {TransactionBuilder} This transaction builder
*/
to(address: string): TransactionBuilder {
const { address: xrpAddress, destinationTag } = utils.getAddressDetails(address);
this._destination = xrpAddress;
this._destinationTag = destinationTag;
return this;
}

/**
* Set the amount to send
* @param {string} amount - the amount sent
* @returns {TransactionBuilder} This transaction builder
*/
amount(amount: Amount): TransactionBuilder {
function isIssuedCurrencyAmount(amount: Amount): amount is IssuedCurrencyAmount {
return (
!_.isString(amount) &&
_.isObjectLike(amount) &&
_.isString(amount.currency) &&
_.isString(amount.issuer) &&
_.isString(amount.value)
);
}

if (!isIssuedCurrencyAmount(amount)) {
throw new Error(`amount type ${typeof amount} must be a IssuedCurrencyAmount type`);
}
this._amount = amount;
return this;
}

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

const transferFields: Payment = {
TransactionType: this.xrpTransactionType,
Account: this._sender,
Destination: this._destination,
Amount: this._amount,
};

if (this._destinationTag) {
transferFields.DestinationTag = this._destinationTag;
}

this._specificFields = transferFields;

return await super.buildImplementation();
}
}
Loading
Loading