-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9810560
commit 112d065
Showing
5 changed files
with
165 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import Xrp from '@ledgerhq/hw-app-xrp'; | ||
import { encode } from 'ripple-binary-codec'; | ||
import { CheckCreate, Client, Transaction, TrustSet } from 'xrpl'; | ||
|
||
import { | ||
checkRippleTransactionResult, | ||
connectRippleClient, | ||
createCheck, | ||
getDLCBTCBalance, | ||
getLockedBTCBalance, | ||
setTrustLine, | ||
} from '../functions/ripple/ripple.functions.js'; | ||
|
||
export class LedgerXRPHandler { | ||
private ledgerApp: Xrp.default; | ||
private derivationPath: string; | ||
private xrpClient: Client; | ||
private issuerAddress: string; | ||
|
||
constructor( | ||
ledgerApp: Xrp.default, | ||
derivationPath: string, | ||
xrpClient: Client, | ||
issuerAddress: string | ||
) { | ||
this.ledgerApp = ledgerApp; | ||
this.derivationPath = derivationPath; | ||
this.xrpClient = xrpClient; | ||
this.issuerAddress = issuerAddress; | ||
} | ||
|
||
public async getAddress(): Promise<string> { | ||
const address = await this.ledgerApp.getAddress(this.derivationPath); | ||
return address.address; | ||
} | ||
|
||
public async setTrustLine(): Promise<void> { | ||
try { | ||
const deviceData = await this.ledgerApp.getAddress(this.derivationPath); | ||
|
||
const trustLineRequest = await setTrustLine( | ||
this.xrpClient, | ||
deviceData.address, | ||
this.issuerAddress | ||
); | ||
|
||
if (!trustLineRequest) { | ||
console.error('TrustLine is already set'); | ||
return; | ||
} | ||
const updatedTrustLineRequest: TrustSet = { | ||
...trustLineRequest, | ||
Flags: 2147483648, | ||
SigningPubKey: deviceData.publicKey.toUpperCase(), | ||
}; | ||
|
||
const encodedTrustLineRequest = encode(updatedTrustLineRequest); | ||
|
||
const signature = await this.ledgerApp.signTransaction( | ||
this.derivationPath, | ||
encodedTrustLineRequest | ||
); | ||
console.log('Signature:', signature); | ||
|
||
const signedTrustLineRequest: TrustSet = { | ||
...updatedTrustLineRequest, | ||
TxnSignature: signature, | ||
}; | ||
|
||
await connectRippleClient(this.xrpClient); | ||
|
||
const submitTrustLineRequestResponse = | ||
await this.xrpClient.submitAndWait(signedTrustLineRequest); | ||
|
||
console.log(`Response for submitted Transaction Request:`, submitTrustLineRequestResponse); | ||
|
||
checkRippleTransactionResult(submitTrustLineRequestResponse); | ||
} catch (error) { | ||
throw new Error(`Error setting Trust Line: ${error}`); | ||
} | ||
} | ||
|
||
public async createCheck(dlcBTCAmount: string, vaultUUID: string): Promise<void> { | ||
try { | ||
const deviceData = await this.ledgerApp.getAddress(this.derivationPath); | ||
|
||
const checkCreateRequest: CheckCreate = await createCheck( | ||
this.xrpClient, | ||
deviceData.address, | ||
this.issuerAddress, | ||
undefined, | ||
dlcBTCAmount, | ||
vaultUUID | ||
); | ||
|
||
const updatedCheckCreateRequest: CheckCreate = { | ||
...checkCreateRequest, | ||
Flags: 2147483648, | ||
SigningPubKey: deviceData.publicKey.toUpperCase(), | ||
}; | ||
|
||
const encodedCheckCreateRequest = encode(updatedCheckCreateRequest); | ||
|
||
const signature = await this.ledgerApp.signTransaction( | ||
this.derivationPath, | ||
encodedCheckCreateRequest | ||
); | ||
|
||
const signedCheckCreateRequest: Transaction = { | ||
...updatedCheckCreateRequest, | ||
TxnSignature: signature, | ||
}; | ||
|
||
await connectRippleClient(this.xrpClient); | ||
|
||
const submitCheckCreateRequestResponse = | ||
await this.xrpClient.submitAndWait(signedCheckCreateRequest); | ||
|
||
console.log(`Response for submitted Transaction Request:`, submitCheckCreateRequestResponse); | ||
|
||
checkRippleTransactionResult(submitCheckCreateRequestResponse); | ||
} catch (error) { | ||
throw new Error(`Error creating Check: ${error}`); | ||
} | ||
} | ||
|
||
public async getDLCBTCBalance(): Promise<number> { | ||
try { | ||
const deviceData = await this.ledgerApp.getAddress(this.derivationPath); | ||
|
||
return await getDLCBTCBalance(this.xrpClient, deviceData.address, this.issuerAddress); | ||
} catch (error) { | ||
throw new Error(`Error getting BTC Balance: ${error}`); | ||
} | ||
} | ||
|
||
public async getLockedBTCBalance(): Promise<number> { | ||
try { | ||
const deviceData = await this.ledgerApp.getAddress(this.derivationPath); | ||
|
||
return await getLockedBTCBalance(this.xrpClient, deviceData.address, this.issuerAddress); | ||
} catch (error) { | ||
throw new Error(`Error getting BTC Balance: ${error}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1105,6 +1105,14 @@ | |
tiny-secp256k1 "1.1.6" | ||
varuint-bitcoin "1.1.2" | ||
|
||
"@ledgerhq/[email protected]": | ||
version "6.29.4" | ||
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-app-xrp/-/hw-app-xrp-6.29.4.tgz#b90d847f2cf8ddca9ca0068f5079f199c334314d" | ||
integrity sha512-fEnqkwmEmcThGVtxLUQX9x4KB1E659Ke1dYuCZSXX4346+h3PCa7cfeKN/VRZNH8HQJgiYi53LqqYzwWXB5zbg== | ||
dependencies: | ||
"@ledgerhq/hw-transport" "^6.31.4" | ||
bip32-path "0.4.2" | ||
|
||
"@ledgerhq/hw-transport@^6.20.0": | ||
version "6.30.6" | ||
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport/-/hw-transport-6.30.6.tgz#c6d84672ac4828f311831998f4101ea205215a6d" | ||
|
@@ -1115,7 +1123,7 @@ | |
"@ledgerhq/logs" "^6.12.0" | ||
events "^3.3.0" | ||
|
||
"@ledgerhq/hw-transport@^6.31.2": | ||
"@ledgerhq/hw-transport@^6.31.2", "@ledgerhq/hw-transport@^6.31.4": | ||
version "6.31.4" | ||
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport/-/hw-transport-6.31.4.tgz#9b23a6de4a4caaa5c24b149c2dea8adde46f0eb1" | ||
integrity sha512-6c1ir/cXWJm5dCWdq55NPgCJ3UuKuuxRvf//Xs36Bq9BwkV2YaRQhZITAkads83l07NAdR16hkTWqqpwFMaI6A== | ||
|
@@ -1792,7 +1800,7 @@ bip174@^2.0.1, bip174@^2.1.1: | |
resolved "https://registry.yarnpkg.com/bip174/-/bip174-2.1.1.tgz#ef3e968cf76de234a546962bcf572cc150982f9f" | ||
integrity sha512-mdFV5+/v0XyNYXjBS6CQPLo9ekCx4gtKZFnJm5PMto7Fs9hTTDpkkzOB7/FtluRI6JbUUAu+snTYfJRgHLZbZQ== | ||
|
||
bip32-path@^0.4.2: | ||
bip32-path@0.4.2, bip32-path@^0.4.2: | ||
version "0.4.2" | ||
resolved "https://registry.yarnpkg.com/bip32-path/-/bip32-path-0.4.2.tgz#5db0416ad6822712f077836e2557b8697c0c7c99" | ||
integrity sha512-ZBMCELjJfcNMkz5bDuJ1WrYvjlhEF5k6mQ8vUr4N7MbVRsXei7ZOg8VhhwMfNiW68NWmLkgkc6WvTickrLGprQ== | ||
|