Skip to content

Commit

Permalink
feat: support xrpl (#34)
Browse files Browse the repository at this point in the history
* feat: add xrpl functions

---------

Co-authored-by: sosaucily <[email protected]>
  • Loading branch information
Polybius93 and sosaucily authored Oct 28, 2024
1 parent 2807a76 commit ff059c0
Show file tree
Hide file tree
Showing 21 changed files with 1,699 additions and 93 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/code-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ jobs:
- name: Typecheck
run: yarn lint:typecheck

lint-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Commit Message
uses: wagoid/commitlint-github-action@v4

test-unit:
needs: build
runs-on: ubuntu-latest
Expand Down
37 changes: 21 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "dlc-btc-lib",
"version": "2.2.7",
"version": "2.4.9",
"description": "This library provides a comprehensive set of interfaces and functions for minting dlcBTC tokens on supported blockchains.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -15,7 +15,8 @@
"./models": "./dist/models/index.js",
"./bitcoin-functions": "./dist/functions/bitcoin/index.js",
"./attestor-request-functions": "./dist/functions/attestor/index.js",
"./ethereum-functions": "./dist/functions/ethereum/index.js"
"./ethereum-functions": "./dist/functions/ethereum/index.js",
"./ripple-functions": "./dist/functions/ripple/index.js"
},
"scripts": {
"clean": "rm -rf dist && rm -rf node_modules",
Expand Down Expand Up @@ -58,20 +59,24 @@
"typescript-eslint": "^7.7.0"
},
"dependencies": {
"@ledgerhq/hw-app-btc": "^10.2.4",
"@noble/hashes": "^1.4.0",
"@scure/base": "^1.1.6",
"@scure/btc-signer": "^1.3.1",
"@types/ramda": "^0.30.1",
"bip32": "^4.0.0",
"bitcoinjs-lib": "^6.1.5",
"chalk": "^5.3.0",
"decimal.js": "^10.4.3",
"@gemwallet/api": "3.8.0",
"@ledgerhq/hw-app-btc": "10.4.1",
"@ledgerhq/hw-app-xrp": "6.29.4",
"@noble/hashes": "1.4.0",
"@scure/base": "1.1.8",
"@scure/btc-signer": "1.3.2",
"@types/ramda": "0.30.1",
"bip32": "4.0.0",
"bitcoinjs-lib": "6.1.6",
"chalk": "5.3.0",
"decimal.js": "10.4.3",
"ethers": "5.7.2",
"ledger-bitcoin": "^0.2.3",
"prompts": "^2.4.2",
"ramda": "^0.30.1",
"scure": "^1.6.0",
"tiny-secp256k1": "^2.2.3"
"ledger-bitcoin": "0.2.3",
"prompts": "2.4.2",
"ramda": "0.30.1",
"ripple-binary-codec": "2.1.0",
"scure": "1.6.0",
"tiny-secp256k1": "2.2.3",
"xrpl": "4.0.0"
}
}
4 changes: 4 additions & 0 deletions src/constants/ripple.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { convertStringToHex } from 'xrpl';

export const TRANSACTION_SUCCESS_CODE = 'tesSUCCESS';
export const XRPL_DLCBTC_CURRENCY_HEX = convertStringToHex('dlcBTC').padEnd(40, '0');
33 changes: 4 additions & 29 deletions src/dlc-handlers/software-wallet-dlc-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Transaction, p2tr, p2wpkh } from '@scure/btc-signer';
import { P2Ret, P2TROut } from '@scure/btc-signer/payment';
import { Network } from 'bitcoinjs-lib';
import { bitcoin, regtest, testnet } from 'bitcoinjs-lib/src/networks.js';

import {
createTaprootMultisigPayment,
Expand Down Expand Up @@ -33,35 +32,11 @@ export class SoftwareWalletDLCHandler {
taprootDerivedPublicKey: string,
fundingPaymentType: 'wpkh' | 'tr',
bitcoinNetwork: Network,
bitcoinBlockchainAPI?: string,
bitcoinBlockchainFeeRecommendationAPI?: string
bitcoinBlockchainAPI: string,
bitcoinBlockchainFeeRecommendationAPI: string
) {
switch (bitcoinNetwork) {
case bitcoin:
this.bitcoinBlockchainAPI = 'https://mempool.space/api';
this.bitcoinBlockchainFeeRecommendationAPI =
'https://mempool.space/api/v1/fees/recommended';
break;
case testnet:
this.bitcoinBlockchainAPI = 'https://mempool.space/testnet/api';
this.bitcoinBlockchainFeeRecommendationAPI =
'https://mempool.space/testnet/api/v1/fees/recommended';
break;
case regtest:
if (
bitcoinBlockchainAPI === undefined ||
bitcoinBlockchainFeeRecommendationAPI === undefined
) {
throw new Error(
'Regtest requires a Bitcoin Blockchain API and a Bitcoin Blockchain Fee Recommendation API'
);
}
this.bitcoinBlockchainAPI = bitcoinBlockchainAPI;
this.bitcoinBlockchainFeeRecommendationAPI = bitcoinBlockchainFeeRecommendationAPI;
break;
default:
throw new Error('Invalid Bitcoin Network');
}
this.bitcoinBlockchainAPI = bitcoinBlockchainAPI;
this.bitcoinBlockchainFeeRecommendationAPI = bitcoinBlockchainFeeRecommendationAPI;
this.fundingPaymentType = fundingPaymentType;
this.bitcoinNetwork = bitcoinNetwork;
this.fundingDerivedPublicKey = fundingDerivedPublicKey;
Expand Down
28 changes: 27 additions & 1 deletion src/functions/attestor/attestor-request.functions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
import { equals, filter, isEmpty, isNotNil, join, map, prop } from 'ramda';

import {
AttestorChainID,
FundingTXAttestorInfo,
WithdrawDepositTXAttestorInfo,
} from '../../models/attestor.models.js';
import { AttestorError } from '../../models/errors.js';
import { sendRequest } from '../request/request.functions.js';
import { sendGetRequest, sendRequest } from '../request/request.functions.js';

export async function submitSetupXRPLVaultRequest(
coordinatorURL: string,
userXRPLAddress: string,
attestorChainID: AttestorChainID
): Promise<void> {
const requestBody = JSON.stringify({
user_xrpl_address: userXRPLAddress,
chain: attestorChainID,
});
return sendRequest(`${coordinatorURL}/app/setup-xrpl-vault`, requestBody);
}

export async function submitXRPLCheckToCash(
coordinatorURL: string,
txHash: string,
attestorChainID: AttestorChainID
): Promise<void> {
const requestBody = JSON.stringify({ tx_hash: txHash, chain: attestorChainID });
return sendRequest(`${coordinatorURL}/app/cash-xrpl-check`, requestBody);
}

export async function getAttestorExtendedGroupPublicKey(coordinatorURL: string): Promise<string> {
return sendGetRequest(`${coordinatorURL}/tss/get-extended-group-publickey`);
}

export async function submitFundingPSBT(
attestorRootURLs: string[],
Expand Down
3 changes: 3 additions & 0 deletions src/functions/attestor/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export {
submitFundingPSBT,
submitWithdrawDepositPSBT,
getAttestorExtendedGroupPublicKey,
submitSetupXRPLVaultRequest,
submitXRPLCheckToCash,
} from '../attestor/attestor-request.functions.js';
4 changes: 3 additions & 1 deletion src/functions/bitcoin/bitcoin-request-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export async function fetchBitcoinTransaction(
const response = await fetch(bitcoinBlockchainAPITransactionEndpoint);

if (!response.ok)
throw new Error(`Bitcoin Network Transaction Response was not OK: ${response.statusText}`);
throw new Error(
`Bitcoin Network Transaction Response was not OK: ${response.statusText} - btc tx id: ${txID}`
);

return await response.json();
} catch (error) {
Expand Down
17 changes: 16 additions & 1 deletion src/functions/request/request.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ export async function sendRequest(url: string, body: string): Promise<void> {
});

if (!response.ok) {
throw new Error(`Response ${url} was not OK: ${response.statusText}`);
const errorMessage = await response.text();
throw new Error(`Request to ${url} failed: ${response.statusText} - ${errorMessage}`);
}
}

export async function sendGetRequest(url: string): Promise<string> {
const response = await fetch(url, {
method: 'GET',
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' },
});

if (!response.ok) {
const errorMessage = await response.text();
throw new Error(`Request to ${url} failed: ${response.statusText} - ${errorMessage}`);
}

return response.text();
}
1 change: 1 addition & 0 deletions src/functions/ripple/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ripple.functions.js';
Loading

0 comments on commit ff059c0

Please sign in to comment.