Skip to content

Commit

Permalink
feat(xrp): add memos and destinationTag to xrp crafts
Browse files Browse the repository at this point in the history
  • Loading branch information
jprudent committed Jan 31, 2025
1 parent b7e70fd commit 685f1d1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
9 changes: 2 additions & 7 deletions libs/coin-framework/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,9 @@ export type Transaction = {
recipient: string;
amount: bigint;
fee: bigint;
supplement?: unknown;
};
} & Record<string, unknown>; // Field containing dedicated value for each blockchain

// TODO rename start to minHeight
// and add a `token: string` field to the pagination if we really need to support pagination
// (which is not the case for now)
// for now start is used as a minHeight from which we want to fetch ALL operations
// limit is unused for now
// TODO add a `token: string` field to the pagination if we really need to support pagination (which is not the case for now)
// see design document at https://ledgerhq.atlassian.net/wiki/spaces/BE/pages/5446205788/coin-modules+lama-adapter+APIs+refinements
export type Pagination = { minHeight: number };
export type Api = {
Expand Down
2 changes: 1 addition & 1 deletion libs/coin-modules/coin-xrp/src/bridge/signOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const buildSignOperation =
recipient: transaction.recipient,
amount: BigInt(transaction.amount.toString()),
fee: BigInt(fee.toString()),
tag: transaction.tag,
destinationTag: transaction.tag,
},
publicKey,
);
Expand Down
44 changes: 38 additions & 6 deletions libs/coin-modules/coin-xrp/src/logic/craftTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,34 @@ import { UINT32_MAX, validateTag } from "./utils";
const LEDGER_OFFSET = 20;

const { TRANSACTION_TYPES } = XrplDefinitions;
type Memo = {
MemoData?: string;
MemoFormat?: string;
MemoType?: string;
};
type MemoWrapper = {
Memo: Memo;
};
type XrplTransaction = {
TransactionType: keyof typeof TRANSACTION_TYPES;
Flags: number;
Account: string;
Amount: string;
Destination: string;
DestinationTag: number | undefined;
DestinationTag?: number;
Fee: string;
Sequence: number;
LastLedgerSequence: number;
SigningPubKey?: string;
TxnSignature?: string;
Memos?: MemoWrapper[];
};

type MemoInput = {
data?: string;
format?: string;
type?: string;
};
export async function craftTransaction(
account: {
address: string;
Expand All @@ -31,31 +45,49 @@ export async function craftTransaction(
recipient: string;
amount: bigint;
fee: bigint;
tag?: number | null | undefined;
destinationTag?: number | null | undefined;
memos?: MemoInput[];
},
publicKey?: string,
): Promise<{
xrplTransaction: XrplTransaction;
serializedTransaction: string;
}> {
const tag = transaction.tag ? transaction.tag : undefined;
const xrplTransaction: XrplTransaction = {
TransactionType: "Payment",
Account: account.address,
Amount: transaction.amount.toString(),
Destination: transaction.recipient,
DestinationTag: tag,
Fee: transaction.fee.toString(),
Flags: 2147483648,
Sequence: account.nextSequenceNumber,
LastLedgerSequence: (await getLedgerIndex()) + LEDGER_OFFSET,
};

if (tag) {
function memoMapper(memoInput: MemoInput): MemoWrapper {
const memo: Memo = {};
if (memoInput.data) {
memo.MemoData = memoInput.data;
}
if (memoInput.format) {
memo.MemoFormat = memoInput.format;
}
if (memoInput.type) {
memo.MemoType = memoInput.type;
}
return { Memo: memo };
}

if (transaction.memos) {
xrplTransaction.Memos = transaction.memos.map(memoMapper);
}

if (transaction.destinationTag) {
invariant(
validateTag(new BigNumber(tag)),
validateTag(new BigNumber(transaction.destinationTag)),
`tag is set but is not in a valid format, should be between [0 - ${UINT32_MAX.toString()}]`,
);
xrplTransaction.DestinationTag = transaction.destinationTag;
}

const serializedTransaction = publicKey
Expand Down

0 comments on commit 685f1d1

Please sign in to comment.