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