@dfns/lib-polkadot 0.3.4
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.
- package/index.d.ts +18 -0
- package/index.js +57 -0
- package/package.json +17 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DfnsApiClient } from '@dfns/sdk';
|
|
2
|
+
import { Signer, SignerResult } from '@polkadot/api/types';
|
|
3
|
+
import { SignerPayloadRaw } from '@polkadot/types/types/extrinsic';
|
|
4
|
+
export type DfnsWalletOptions = {
|
|
5
|
+
walletId: string;
|
|
6
|
+
dfnsClient: DfnsApiClient;
|
|
7
|
+
};
|
|
8
|
+
export declare class DfnsWallet implements Signer {
|
|
9
|
+
address: string;
|
|
10
|
+
private id;
|
|
11
|
+
private readonly dfnsClient;
|
|
12
|
+
private readonly walletId;
|
|
13
|
+
static supportedNetworks: string[];
|
|
14
|
+
private constructor();
|
|
15
|
+
static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
|
|
16
|
+
signRaw(raw: SignerPayloadRaw): Promise<SignerResult>;
|
|
17
|
+
private generateSignature;
|
|
18
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DfnsWallet = void 0;
|
|
4
|
+
const sdk_1 = require("@dfns/sdk");
|
|
5
|
+
const assertSignResponseSuccessful = (response) => {
|
|
6
|
+
if (response.status === 'Failed') {
|
|
7
|
+
throw new sdk_1.DfnsError(-1, 'signing failed', response);
|
|
8
|
+
}
|
|
9
|
+
else if (response.status !== 'Signed') {
|
|
10
|
+
throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', response);
|
|
11
|
+
}
|
|
12
|
+
else if (!response.signature || !(response.signature.encoded)) {
|
|
13
|
+
throw new sdk_1.DfnsError(-1, 'signature missing', response);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
class DfnsWallet {
|
|
17
|
+
constructor(address, options) {
|
|
18
|
+
this.address = address;
|
|
19
|
+
this.dfnsClient = options.dfnsClient;
|
|
20
|
+
this.walletId = options.walletId;
|
|
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 (!this.supportedNetworks.includes(res.network)) {
|
|
29
|
+
throw new sdk_1.DfnsError(-1, 'wallet is not bound to a Polkadot compatible network', {
|
|
30
|
+
walletId,
|
|
31
|
+
network: res.network,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return new DfnsWallet(res.address, options);
|
|
35
|
+
}
|
|
36
|
+
// Implementation of Signer
|
|
37
|
+
async signRaw(raw) {
|
|
38
|
+
const signature = await this.generateSignature(raw.data, raw.address);
|
|
39
|
+
return { id: ++this.id, signature: signature };
|
|
40
|
+
}
|
|
41
|
+
async generateSignature(data, address) {
|
|
42
|
+
if (this.address !== address) {
|
|
43
|
+
throw new sdk_1.DfnsError(-1, 'address does not match the wallet used to initialize DfnsWallet', {
|
|
44
|
+
expectedAddress: this.address,
|
|
45
|
+
givenAddress: address
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const response = await this.dfnsClient.wallets.generateSignature({
|
|
49
|
+
walletId: this.walletId,
|
|
50
|
+
body: { kind: 'Message', message: data, },
|
|
51
|
+
});
|
|
52
|
+
assertSignResponseSuccessful(response);
|
|
53
|
+
return (response.signature.encoded);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.DfnsWallet = DfnsWallet;
|
|
57
|
+
DfnsWallet.supportedNetworks = ['Polkadot', 'Westend', 'Kusama'];
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dfns/lib-polkadot",
|
|
3
|
+
"version": "0.3.4",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@polkadot/api": "^10.11.3",
|
|
6
|
+
"@polkadot/types": "10.12.3",
|
|
7
|
+
"@polkadot/util": "12.6.2",
|
|
8
|
+
"buffer": "6.0.3",
|
|
9
|
+
"cross-fetch": "3.1.6",
|
|
10
|
+
"uuid": "9.0.0"
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"@dfns/sdk": "0.3.4"
|
|
14
|
+
},
|
|
15
|
+
"main": "./index.js",
|
|
16
|
+
"type": "commonjs"
|
|
17
|
+
}
|