@dfns/lib-xrpl 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +18 -0
  2. package/index.js +53 -0
  3. package/package.json +9 -0
package/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { DfnsApiClient } from '@dfns/sdk';
2
+ import { Transaction } from 'xrpl';
3
+ export type DfnsWalletOptions = {
4
+ walletId: string;
5
+ dfnsClient: DfnsApiClient;
6
+ };
7
+ export declare class DfnsWallet {
8
+ private metadata;
9
+ private readonly dfnsClient;
10
+ private readonly signingPubKey;
11
+ private constructor();
12
+ static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
13
+ get address(): string;
14
+ signTransaction(transaction: Transaction): Promise<{
15
+ tx_blob: string;
16
+ hash: string;
17
+ }>;
18
+ }
package/index.js ADDED
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DfnsWallet = void 0;
4
+ const sdk_1 = require("@dfns/sdk");
5
+ const xrpl_1 = require("xrpl");
6
+ const assertSigned = (res) => {
7
+ if (res.status === 'Failed') {
8
+ throw new sdk_1.DfnsError(-1, 'signing failed', res);
9
+ }
10
+ else if (res.status !== 'Signed') {
11
+ throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
12
+ }
13
+ };
14
+ class DfnsWallet {
15
+ constructor(metadata, options) {
16
+ this.metadata = metadata;
17
+ // ripple adds the prefix "ed" to eddsa pubKey/private key to have
18
+ // the same bytes number as ecdsa pubKey.
19
+ const prefix = metadata.signingKey.scheme === 'EdDSA' ? 'ed' : '';
20
+ this.signingPubKey = prefix + metadata.signingKey.publicKey;
21
+ this.dfnsClient = options.dfnsClient;
22
+ }
23
+ static async init(options) {
24
+ const { walletId, dfnsClient } = options;
25
+ const res = await dfnsClient.wallets.getWallet({ walletId });
26
+ if (res.status !== 'Active') {
27
+ throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
28
+ }
29
+ if (res.network !== 'Ripple' && res.network !== 'RippleTestnet') {
30
+ throw new sdk_1.DfnsError(-1, 'wallet is not bound to Ripple or RippleTestnet', { walletId, network: res.network });
31
+ }
32
+ return new DfnsWallet(res, options);
33
+ }
34
+ get address() {
35
+ return this.metadata.address;
36
+ }
37
+ async signTransaction(transaction) {
38
+ const signatureResponse = await this.dfnsClient.wallets.generateSignature({
39
+ walletId: this.metadata.id,
40
+ body: { kind: 'Transaction', transaction: `0x${(0, xrpl_1.encode)(transaction).toLowerCase()}` },
41
+ });
42
+ assertSigned(signatureResponse);
43
+ if (!signatureResponse.signedData) {
44
+ throw new sdk_1.DfnsError(-1, 'signedData missing', signatureResponse);
45
+ }
46
+ const signedTxSerialized = signatureResponse.signedData.replace(/^0x/, '');
47
+ return {
48
+ tx_blob: signedTxSerialized,
49
+ hash: xrpl_1.hashes.hashSignedTx(signedTxSerialized),
50
+ };
51
+ }
52
+ }
53
+ exports.DfnsWallet = DfnsWallet;
package/package.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "@dfns/lib-xrpl",
3
+ "version": "0.2.1",
4
+ "dependencies": {
5
+ "xrpl": "2.14.0"
6
+ },
7
+ "main": "./index.js",
8
+ "type": "commonjs"
9
+ }