Skip to content

Commit 392222b

Browse files
committed
feat(sdk-coin-xrp): add transaction builder
Ticket: WIN-3518
1 parent b99fe3f commit 392222b

18 files changed

+1446
-17
lines changed

modules/sdk-coin-xrp/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
export * from './xrp';
2-
export * from './txrp';
1+
export * from './lib';
32
export * from './register';
4-
export * from './lib/iface';
5-
export * from './lib/utils';
3+
export * from './txrp';
4+
export * from './xrp';
65
export * from './xrpToken';
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core';
2+
import { BaseCoin as CoinConfig } from '@bitgo/statics';
3+
import { AccountSet } from 'xrpl';
4+
import { XrpTransactionType } from './iface';
5+
import { Transaction } from './transaction';
6+
import { TransactionBuilder } from './transactionBuilder';
7+
import utils from './utils';
8+
9+
export class AccountSetBuilder extends TransactionBuilder {
10+
protected _setFlag: number;
11+
protected _messageKey: string;
12+
constructor(_coinConfig: Readonly<CoinConfig>) {
13+
super(_coinConfig);
14+
}
15+
16+
protected get transactionType(): TransactionType {
17+
return TransactionType.AccountUpdate;
18+
}
19+
20+
protected get xrpTransactionType(): XrpTransactionType.AccountSet {
21+
return XrpTransactionType.AccountSet;
22+
}
23+
24+
setFlag(setFlag: number): TransactionBuilder {
25+
utils.validateAccountSetFlag(setFlag);
26+
this._setFlag = setFlag;
27+
return this;
28+
}
29+
30+
messageKey(messageKey: string): TransactionBuilder {
31+
if (typeof messageKey !== 'string') {
32+
throw new BuildTransactionError('Invalid message key');
33+
}
34+
this._messageKey = messageKey;
35+
return this;
36+
}
37+
38+
initBuilder(tx: Transaction): void {
39+
super.initBuilder(tx);
40+
41+
const { setFlag, messageKey } = tx.toJson();
42+
if (setFlag) {
43+
this.setFlag(setFlag);
44+
}
45+
46+
if (messageKey) {
47+
this.messageKey(messageKey);
48+
}
49+
}
50+
51+
/** @inheritdoc */
52+
protected async buildImplementation(): Promise<Transaction> {
53+
if (!this._sender) {
54+
throw new BuildTransactionError('Sender must be set before building the transaction');
55+
}
56+
57+
const accountSetFields: AccountSet = {
58+
TransactionType: this.xrpTransactionType,
59+
Account: this._sender,
60+
};
61+
if (this._setFlag) {
62+
accountSetFields.SetFlag = this._setFlag;
63+
}
64+
65+
if (this._messageKey) {
66+
accountSetFields.MessageKey = this._messageKey;
67+
}
68+
69+
this._specificFields = accountSetFields;
70+
71+
return await super.buildImplementation();
72+
}
73+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://xrpl.org/signerlistset.html
2+
export const MAX_SIGNERS = 32;
3+
export const MIN_SIGNERS = 1;
4+
export const MIN_SIGNER_QUORUM = 1;
5+
6+
// https://xrpl.org/accountset.html#accountset-flags
7+
export const VALID_ACCOUNT_SET_FLAGS = [
8+
5, // asfAccountTxnID
9+
16, // asfAllowTrustLineClawback
10+
10, // asfAuthorizedNFTokenMinter
11+
8, // asfDefaultRipple
12+
9, // asfDepositAuth
13+
4, // asfDisableMaster
14+
13, // asfDisallowIncomingCheck
15+
12, // asfDisallowIncomingNFTokenOffer
16+
14, // asfDisallowIncomingPayChan
17+
15, // asfDisallowIncomingTrustline
18+
3, // asfDisallowXRP
19+
7, // asfGlobalFreeze
20+
6, // asfNoFreeze
21+
2, // asfRequireAuth
22+
1, // asfRequireDest
23+
];
24+
25+
// Global flags for bitgo address
26+
export const USER_KEY_SETTING_FLAG = 65536;
27+
export const MASTER_KEY_DEACTIVATION_FLAG = 1048576;
28+
export const REQUIRE_DESTINATION_TAG_FLAG = 131072;

modules/sdk-coin-xrp/src/lib/iface.ts

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import {
22
InitiateRecoveryOptions as BaseInitiateRecoveryOptions,
33
SignTransactionOptions as BaseSignTransactionOptions,
4+
TransactionExplanation as BaseTransactionExplanation,
45
VerifyAddressOptions as BaseVerifyAddressOptions,
5-
TransactionExplanation,
66
TransactionPrebuild,
77
} from '@bitgo/sdk-core';
8+
import { AccountSet, Payment, Signer, SignerEntry, SignerListSet } from 'xrpl';
9+
10+
export enum XrpTransactionType {
11+
AccountSet = 'AccountSet',
12+
Payment = 'Payment',
13+
SignerListSet = 'SignerListSet',
14+
TrustSet = 'TrustSet',
15+
}
16+
17+
export type XrpTransaction = Payment | AccountSet | SignerListSet;
818

919
export interface Address {
1020
address: string;
@@ -35,7 +45,7 @@ export interface VerifyAddressOptions extends BaseVerifyAddressOptions {
3545
rootAddress: string;
3646
}
3747

38-
export interface RecoveryInfo extends TransactionExplanation {
48+
export interface RecoveryInfo extends BaseTransactionExplanation {
3949
txHex: string;
4050
backupKey?: string;
4151
coin?: string;
@@ -68,3 +78,53 @@ export interface HalfSignedTransaction {
6878
export interface SupplementGenerateWalletOptions {
6979
rootPrivateKey?: string;
7080
}
81+
82+
export type TransactionExplanation =
83+
| BaseTransactionExplanation
84+
| AccountSetTransactionExplanation
85+
| SignerListSetTransactionExplanation;
86+
87+
export interface AccountSetTransactionExplanation extends BaseTransactionExplanation {
88+
accountSet: {
89+
messageKey?: string;
90+
setFlag: number;
91+
};
92+
}
93+
94+
export interface SignerListSetTransactionExplanation extends BaseTransactionExplanation {
95+
signerListSet: {
96+
signerQuorum: number;
97+
signerEntries: SignerEntry[];
98+
};
99+
}
100+
101+
export interface TxData {
102+
// mandatory fields
103+
from: string;
104+
transactionType: XrpTransactionType;
105+
isMultiSig: boolean;
106+
// optional fields
107+
id?: string;
108+
fee?: string;
109+
flags: number;
110+
sequence?: number;
111+
lastLedgerSequence?: number;
112+
signingPubKey?: string; // if '' then it is a multi sig
113+
txnSignature?: string; // only for single sig
114+
signers?: Signer[]; // only for multi sig
115+
// transfer xrp fields
116+
destination?: string;
117+
destinationTag?: number;
118+
amount?: string;
119+
// account set fields
120+
messageKey?: string;
121+
setFlag?: number;
122+
// signer list set fields
123+
signerQuorum?: number;
124+
signerEntries?: SignerEntry[];
125+
}
126+
127+
export interface SignerDetails {
128+
address: string;
129+
weight: number;
130+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Utils from './utils';
2+
3+
export * from './constants';
4+
export * from './iface';
5+
export { AccountSetBuilder } from './accountSetBuilder';
6+
export { KeyPair } from './keyPair';
7+
export { Transaction } from './transaction';
8+
export { TransactionBuilder } from './transactionBuilder';
9+
export { TransactionBuilderFactory } from './transactionBuilderFactory';
10+
export { TransferBuilder } from './transferBuilder';
11+
export { WalletInitializationBuilder } from './walletInitializationBuilder';
12+
export { Utils };

0 commit comments

Comments
 (0)