@dfns/lib-stellar 0.4.3-alpha.2

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 +16 -0
  2. package/index.js +61 -0
  3. package/package.json +15 -0
package/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { DfnsApiClient } from '@dfns/sdk';
2
+ import { Transaction, FeeBumpTransaction } from '@stellar/stellar-sdk';
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 network;
11
+ private constructor();
12
+ static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
13
+ get address(): string;
14
+ sign(transaction: Transaction): Promise<Transaction>;
15
+ sign(transaction: FeeBumpTransaction): Promise<FeeBumpTransaction>;
16
+ }
package/index.js ADDED
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DfnsWallet = void 0;
4
+ const sdk_1 = require("@dfns/sdk");
5
+ const stellar_sdk_1 = require("@stellar/stellar-sdk");
6
+ const hexToBase64 = (hex) => {
7
+ return Buffer.from(stripHexPrefix(hex), 'hex').toString('base64');
8
+ };
9
+ const stripHexPrefix = (hex) => {
10
+ return hex.replace(/^0x/, '');
11
+ };
12
+ const assertSigned = (res) => {
13
+ if (res.status === 'Failed') {
14
+ throw new sdk_1.DfnsError(-1, 'signing failed', res);
15
+ }
16
+ else if (res.status !== 'Signed') {
17
+ throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
18
+ }
19
+ };
20
+ class DfnsWallet {
21
+ constructor(metadata, options) {
22
+ this.metadata = metadata;
23
+ this.dfnsClient = options.dfnsClient;
24
+ this.network = metadata.network === 'Stellar' ? stellar_sdk_1.Networks.PUBLIC : stellar_sdk_1.Networks.TESTNET;
25
+ }
26
+ static async init(options) {
27
+ const { walletId, dfnsClient } = options;
28
+ const res = await dfnsClient.wallets.getWallet({ walletId });
29
+ if (res.status !== 'Active') {
30
+ throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
31
+ }
32
+ if (res.network !== 'Stellar' && res.network !== 'StellarTestnet') {
33
+ throw new sdk_1.DfnsError(-1, 'wallet is not bound to Stellar or StellarTestnet', {
34
+ walletId,
35
+ network: res.network,
36
+ });
37
+ }
38
+ return new DfnsWallet(res, options);
39
+ }
40
+ get address() {
41
+ return this.metadata.address;
42
+ }
43
+ async sign(transaction) {
44
+ const isFeeBump = 'innerTransaction' in transaction;
45
+ const res = await this.dfnsClient.wallets.generateSignature({
46
+ walletId: this.metadata.id,
47
+ body: {
48
+ kind: 'Transaction',
49
+ transaction: `0x${transaction.toEnvelope().toXDR('hex')}`,
50
+ },
51
+ });
52
+ assertSigned(res);
53
+ if (!res.signedData) {
54
+ throw new sdk_1.DfnsError(-1, 'signature missing', res);
55
+ }
56
+ return isFeeBump
57
+ ? new stellar_sdk_1.FeeBumpTransaction(hexToBase64(res.signedData), this.network)
58
+ : new stellar_sdk_1.Transaction(hexToBase64(res.signedData), this.network);
59
+ }
60
+ }
61
+ exports.DfnsWallet = DfnsWallet;
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@dfns/lib-stellar",
3
+ "version": "0.4.3-alpha.2",
4
+ "peerDependencies": {
5
+ "@stellar/stellar-sdk": "^11.0.0",
6
+ "@dfns/sdk": "0.4.3-alpha.2"
7
+ },
8
+ "dependencies": {
9
+ "buffer": "6.0.3",
10
+ "cross-fetch": "3.1.6",
11
+ "uuid": "9.0.0"
12
+ },
13
+ "main": "./index.js",
14
+ "type": "commonjs"
15
+ }