Skip to content

Commit

Permalink
feat: Adds xrp support (#563)
Browse files Browse the repository at this point in the history
Co-authored-by: TejasvOnly <[email protected]>
  • Loading branch information
muzaffarbhat07 and TejasvOnly authored Nov 7, 2024
1 parent fd26063 commit 8286aa7
Show file tree
Hide file tree
Showing 88 changed files with 2,443 additions and 117 deletions.
11 changes: 11 additions & 0 deletions .changeset/seven-planets-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@cypherock/coin-support-xrp': major
'@cypherock/cysync-core-constants': patch
'@cypherock/coin-support': patch
'@cypherock/cysync-core': patch
'@cypherock/coins': patch
'@cypherock/cysync-ui': patch
'@cypherock/cysync-cli': patch
---

Added XRP support
4 changes: 3 additions & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@cypherock/coin-support-interfaces": "workspace:^",
"@cypherock/coin-support-near": "workspace:^",
"@cypherock/coin-support-solana": "workspace:^",
"@cypherock/coin-support-xrp": "workspace:^",
"@cypherock/coin-support-utils": "workspace:^",
"@cypherock/coins": "workspace:^",
"@cypherock/cysync-core-services": "workspace:^",
Expand All @@ -59,7 +60,8 @@
"near-api-js": "^2.1.4",
"ora": "^6.3.1",
"rxjs": "^7.8.1",
"semver": "^7.5.3"
"semver": "^7.5.3",
"xrpl": "^4.0.0"
},
"devDependencies": {
"@cypherock/eslint-config": "workspace:^",
Expand Down
3 changes: 3 additions & 0 deletions apps/cli/src/utils/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BtcSupport } from '@cypherock/coin-support-btc';
import { EvmSupport } from '@cypherock/coin-support-evm';
import { NearSupport } from '@cypherock/coin-support-near';
import { SolanaSupport } from '@cypherock/coin-support-solana';
import { XrpSupport } from '@cypherock/coin-support-xrp';
import { IDatabase, IKeyValueStore } from '@cypherock/db-interfaces';
import { IDeviceConnection } from '@cypherock/sdk-interfaces';
import { Command, Flags, Interfaces } from '@oclif/core';
Expand All @@ -11,6 +12,7 @@ import * as bitcoin from 'bitcoinjs-lib';
import * as eip712 from 'eip-712';
import { ethers } from 'ethers';
import * as nearApiJs from 'near-api-js';
import * as xrpl from 'xrpl';

import { initializeAndGetDb } from './db';
import { cleanUpDeviceConnection, createConnection } from './device';
Expand Down Expand Up @@ -94,6 +96,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
EvmSupport.setEip712Library(eip712);
NearSupport.setNearApiJs(nearApiJs);
SolanaSupport.setWeb3Library(solanaWeb3);
XrpSupport.setXrpLib(xrpl);
}

protected async catch(err: Error & { exitCode?: number }): Promise<any> {
Expand Down
8 changes: 8 additions & 0 deletions packages/coin-support-xrp/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
root: true,
extends: ['@cypherock/eslint-config/browser'],
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.eslint.json'],
},
};
3 changes: 3 additions & 0 deletions packages/coin-support-xrp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.turbo
dist
coverage
1 change: 1 addition & 0 deletions packages/coin-support-xrp/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@cypherock/prettier-config"
3 changes: 3 additions & 0 deletions packages/coin-support-xrp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Coin Support XRP

- All coin operations related to XRP
5 changes: 5 additions & 0 deletions packages/coin-support-xrp/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const baseConfig = require('@cypherock/jest-config/node');

module.exports = {
...baseConfig,
};
66 changes: 66 additions & 0 deletions packages/coin-support-xrp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "@cypherock/coin-support-xrp",
"version": "0.0.0",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"license": "AGPL-3.0",
"private": true,
"scripts": {
"lint": "eslint --ext .ts,tsx,js,jsx src/ --fix",
"lint:check": "eslint --ext .ts,tsx,js,jsx src/",
"pretty": "prettier --write \"src/**/*.ts?(x)\" \"tests/**/*.ts?(x)\"",
"pretty:check": "prettier --check \"src/**/*.ts?(x)\" \"tests/**/*.ts?(x)\"",
"build": "rimraf dist && pnpm build:esm && pnpm build:cjs",
"build:cjs": "tsc -p tsconfig_cjs.json",
"build:esm": "tsc -p tsconfig.json",
"build:dirty": "pnpm build:esm",
"test": "jest",
"pre-commit": "lint-staged"
},
"devDependencies": {
"@cypherock/eslint-config": "workspace:^",
"@cypherock/jest-config": "workspace:^",
"@cypherock/prettier-config": "workspace:^",
"@cypherock/tsconfig": "workspace:^",
"@jest/globals": "^29.5.0",
"@stryker-mutator/core": "^7.0.2",
"@stryker-mutator/jest-runner": "^7.0.2",
"@stryker-mutator/typescript-checker": "^7.0.2",
"@types/jest": "^29.5.2",
"@types/lodash": "^4.14.195",
"@types/multicoin-address-validator": "^0.5.0",
"@types/node": "18.15.11",
"eslint": "^8.43.0",
"jest": "^29.5.0",
"lint-staged": "^13.2.2",
"prettier": "^2.8.8",
"rimraf": "^5.0.1",
"ts-jest": "^29.1.0",
"typescript": "^4.9.5",
"xrpl": "^4.0.0"
},
"dependencies": {
"@cypherock/coin-support-interfaces": "workspace:^",
"@cypherock/coin-support-utils": "workspace:^",
"@cypherock/coins": "workspace:^",
"@cypherock/cysync-interfaces": "workspace:^",
"@cypherock/cysync-utils": "workspace:^",
"@cypherock/db-interfaces": "workspace:^",
"@cypherock/sdk-app-xrp": "^1.0.0",
"@cypherock/sdk-interfaces": "^0.0.15",
"@cypherock/sdk-utils": "^0.0.18",
"lodash": "^4.17.21",
"multicoin-address-validator": "^0.5.12",
"rxjs": "^7.8.1"
},
"lint-staged": {
"*.{ts,tsx}": [
"eslint --ext ts,tsx --quiet --fix --",
"prettier --write"
],
"*.{js,jsx,md,mdx,mjs,yml,yaml,css,json}": [
"prettier --write"
]
}
}
5 changes: 5 additions & 0 deletions packages/coin-support-xrp/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getEnvVariable } from '@cypherock/cysync-utils';

export const config = {
API_CYPHEROCK: getEnvVariable('API_CYPHEROCK', 'https://api.cypherock.com'),
};
122 changes: 122 additions & 0 deletions packages/coin-support-xrp/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* eslint-disable class-methods-use-this */
import {
CoinSupport,
ICreateAccountEvent,
ICreateAccountParams,
IFormatAddressParams,
IGetAccountHistoryParams,
IGetAccountHistoryResult,
IGetCoinAllocationsParams,
IGetCoinAllocationsResult,
IGetExplorerLink,
IInitializeTransactionParams,
IPreparedTransaction,
IReceiveEvent,
IReceiveParams,
ISignMessageEvent,
ISignMessageParams,
ISignTransactionParams,
ISyncAccountsParams,
ISyncPriceHistoriesParams,
ISyncPricesParams,
IValidateAddressParams,
} from '@cypherock/coin-support-interfaces';
import { ITransaction } from '@cypherock/db-interfaces';
import { setXrpLib } from '@cypherock/sdk-app-xrp';
import { Observable } from 'rxjs';

import * as operations from './operations';
import {
IBroadcastXrpTransactionParams,
IPrepareXrpTransactionParams,
ISignXrpTransactionEvent,
} from './operations/types';
import { setCoinSupportXrpLib, XrpLibType } from './utils';

export * from './operations/types';
export { updateLogger } from './utils/logger';

export class XrpSupport implements CoinSupport {
public static setXrpLib(xrplib: XrpLibType): void {
setXrpLib(xrplib);
setCoinSupportXrpLib(xrplib);
}

public receive(params: IReceiveParams): Observable<IReceiveEvent> {
return operations.receive(params);
}

public createAccounts(
params: ICreateAccountParams,
): Observable<ICreateAccountEvent> {
return operations.createAccounts(params);
}

public syncAccount(params: ISyncAccountsParams): Observable<void> {
return operations.syncAccount(params);
}

public async initializeTransaction(
params: IInitializeTransactionParams,
): Promise<IPreparedTransaction> {
return operations.initializeTransaction(params);
}

public async prepareTransaction(
params: IPrepareXrpTransactionParams,
): Promise<IPreparedTransaction> {
return operations.prepareTransaction(params);
}

public signTransaction(
params: ISignTransactionParams,
): Observable<ISignXrpTransactionEvent> {
return operations.signTransaction(params);
}

public broadcastTransaction(
params: IBroadcastXrpTransactionParams,
): Promise<ITransaction> {
return operations.broadcastTransaction(params);
}

public signMessage(
params: ISignMessageParams,
): Observable<ISignMessageEvent> {
throw new Error(`Method not implemented Params: ${params}`);
}

public getCoinAllocations(
params: IGetCoinAllocationsParams,
): Promise<IGetCoinAllocationsResult> {
return operations.getCoinAllocations(params);
}

public getAccountHistory(
params: IGetAccountHistoryParams,
): Promise<IGetAccountHistoryResult> {
return operations.getAccountHistory(params);
}

public validateAddress(params: IValidateAddressParams): boolean {
return operations.validateAddress(params);
}

public syncPrices(params: ISyncPricesParams): Observable<void> {
return operations.syncPrices(params);
}

public syncPriceHistories(
params: ISyncPriceHistoriesParams,
): Observable<void> {
return operations.syncPriceHistories(params);
}

public getExplorerLink(params: IGetExplorerLink): string {
return operations.getExplorerLink(params);
}

public formatAddress(params: IFormatAddressParams): string {
return params.address;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {
getAccountAndCoin,
insertOrUpdateTransactions,
} from '@cypherock/coin-support-utils';
import { xrpCoinList } from '@cypherock/coins';
import { BigNumber } from '@cypherock/cysync-utils';
import {
ITransaction,
TransactionStatusMap,
TransactionTypeMap,
} from '@cypherock/db-interfaces';

import { IBroadcastXrpTransactionParams } from './types';

import { broadcastTransactionToBlockchain } from '../../services';
import { deriveAddress } from '../../utils';

export const broadcastTransaction = async (
params: IBroadcastXrpTransactionParams,
): Promise<ITransaction> => {
const { db, signedTransaction, transaction } = params;
const { account, coin } = await getAccountAndCoin(
db,
xrpCoinList,
transaction.accountId,
);

const myAddress = deriveAddress(account.xpubOrAddress);
const isMine = params.transaction.computedData.output.address === myAddress;

const result = await broadcastTransactionToBlockchain(
signedTransaction,
coin.id,
);

const parsedTransaction: ITransaction = {
hash: result.tx_json.hash,
fees: transaction.computedData.fees,
amount: '0',
status: TransactionStatusMap.pending,
type: TransactionTypeMap.send,
timestamp: Date.now(),
blockHeight: -1,
inputs: [
{
address: myAddress,
amount: '0',
isMine: true,
},
],
outputs: [
{
...params.transaction.userInputs.outputs[0],
isMine,
},
],
confirmations: 0,
accountId: account.__id,
walletId: account.walletId,
assetId: account.assetId,
parentAssetId: account.parentAssetId,
familyId: account.familyId,
parentAccountId: account.parentAccountId,
remarks: [transaction.userInputs.outputs[0].remarks ?? ''],
extraData: {
destinationTag: result.tx_json.DestinationTag,
flags: result.tx_json.Flags,
sequence: result.tx_json.Sequence,
lastLedgerSequence: result.tx_json.LastLedgerSequence,
},
};

const amount = parsedTransaction.outputs.reduce(
(sum, output) => (output.isMine ? sum : sum.plus(output.amount)),
new BigNumber(0),
);
parsedTransaction.amount = amount.abs().toString();
parsedTransaction.inputs[0].amount = amount.abs().toString();

const [addedTxn] = await insertOrUpdateTransactions(db, [parsedTransaction]);

return addedTxn;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IBroadcastTransactionParams } from '@cypherock/coin-support-interfaces';

import { IPreparedXrpTransaction } from '../transaction';

export interface IBroadcastXrpTransactionParams
extends IBroadcastTransactionParams<string> {
transaction: IPreparedXrpTransaction;
}
Loading

0 comments on commit 8286aa7

Please sign in to comment.