@dfns/lib-polymesh 0.7.2-beta.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 +38 -0
  2. package/index.js +112 -0
  3. package/package.json +17 -0
package/index.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ import { DfnsApiClient } from '@dfns/sdk';
2
+ import { PolkadotSigner, SignerPayloadRaw, SignerPayloadJSON, SignerResult, SigningManager } from '@polymeshassociation/signing-manager-types';
3
+ import { Registry } from '@polkadot/types/types';
4
+ export type DfnsWalletOptions = {
5
+ walletId: string;
6
+ dfnsClient: DfnsApiClient;
7
+ registry: Registry;
8
+ };
9
+ export declare class DfnsSigningManager implements SigningManager {
10
+ private externalSigner;
11
+ private _ss58Format;
12
+ constructor(dfnsSigner: DfnsWallet);
13
+ /**
14
+ * Set the SS58 format in which addresses will be encoded
15
+ */
16
+ setSs58Format(ss58Format: number): void;
17
+ get ss58Format(): number;
18
+ getAccounts(): Promise<string[]>;
19
+ /**
20
+ * Return a signer object that uses the underlying keyring pairs to sign
21
+ */
22
+ getExternalSigner(): PolkadotSigner;
23
+ }
24
+ export declare class DfnsWallet implements PolkadotSigner {
25
+ address: string;
26
+ network: string;
27
+ private id;
28
+ private readonly dfnsClient;
29
+ private readonly walletId;
30
+ private readonly registry;
31
+ private constructor();
32
+ static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
33
+ signRaw(raw: SignerPayloadRaw): Promise<SignerResult>;
34
+ signPayload(signerPayload: SignerPayloadJSON): Promise<SignerResult>;
35
+ private signMessage;
36
+ private formatSignature;
37
+ private validateAddress;
38
+ }
package/index.js ADDED
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DfnsWallet = exports.DfnsSigningManager = void 0;
4
+ const sdk_1 = require("@dfns/sdk");
5
+ const types_1 = require("@polkadot/types");
6
+ const Ss58PrefixMap = {
7
+ ['Polymesh']: 12,
8
+ ['PolymeshTestnet']: 42,
9
+ };
10
+ const assertSignResponseSuccessful = (response) => {
11
+ if (response.status === 'Failed') {
12
+ throw new sdk_1.DfnsError(-1, 'signing failed', response);
13
+ }
14
+ else if (response.status !== 'Signed') {
15
+ throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', response);
16
+ }
17
+ else if (!response.signature || !response.signature.encoded) {
18
+ throw new sdk_1.DfnsError(-1, 'signature missing', response);
19
+ }
20
+ };
21
+ class DfnsSigningManager {
22
+ constructor(dfnsSigner) {
23
+ this.externalSigner = dfnsSigner;
24
+ this._ss58Format = Ss58PrefixMap[dfnsSigner.network];
25
+ }
26
+ /**
27
+ * Set the SS58 format in which addresses will be encoded
28
+ */
29
+ setSs58Format(ss58Format) {
30
+ this._ss58Format = ss58Format;
31
+ }
32
+ get ss58Format() {
33
+ return this._ss58Format;
34
+ }
35
+ async getAccounts() {
36
+ return [this.externalSigner.address];
37
+ }
38
+ /**
39
+ * Return a signer object that uses the underlying keyring pairs to sign
40
+ */
41
+ getExternalSigner() {
42
+ return this.externalSigner;
43
+ }
44
+ }
45
+ exports.DfnsSigningManager = DfnsSigningManager;
46
+ class DfnsWallet {
47
+ constructor(address, network, options) {
48
+ this.address = address;
49
+ this.network = network;
50
+ this.dfnsClient = options.dfnsClient;
51
+ this.walletId = options.walletId;
52
+ this.registry = options.registry;
53
+ }
54
+ static async init(options) {
55
+ const { walletId, dfnsClient } = options;
56
+ const res = await dfnsClient.wallets.getWallet({ walletId });
57
+ if (res.status !== 'Active') {
58
+ throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
59
+ }
60
+ if (res.network !== 'Polymesh' && res.network !== 'PolymeshTestnet') {
61
+ throw new sdk_1.DfnsError(-1, 'wallet is not bound to a Polymesh network', {
62
+ walletId,
63
+ network: res.network,
64
+ });
65
+ }
66
+ return new DfnsWallet(res.address, res.network, options);
67
+ }
68
+ async signRaw(raw) {
69
+ const signature = await this.signMessage(raw.data, raw.address);
70
+ return { id: ++this.id, signature: signature };
71
+ }
72
+ async signPayload(signerPayload) {
73
+ this.validateAddress(signerPayload.address);
74
+ const { transactionVersion, specVersion, ...rest } = signerPayload;
75
+ const genericPayload = new types_1.GenericSignerPayload(this.registry, {
76
+ ...rest,
77
+ runtimeVersion: { specVersion, transactionVersion },
78
+ });
79
+ const response = await this.dfnsClient.wallets.generateSignature({
80
+ walletId: this.walletId,
81
+ body: {
82
+ kind: 'SignerPayload',
83
+ payload: genericPayload.toHex()
84
+ }
85
+ });
86
+ assertSignResponseSuccessful(response);
87
+ const signature = this.formatSignature(response.signature.encoded);
88
+ return { id: ++this.id, signature };
89
+ }
90
+ async signMessage(data, address) {
91
+ this.validateAddress(address);
92
+ const response = await this.dfnsClient.wallets.generateSignature({
93
+ walletId: this.walletId,
94
+ body: { kind: 'Message', message: data }
95
+ });
96
+ assertSignResponseSuccessful(response);
97
+ return this.formatSignature(response.signature.encoded);
98
+ }
99
+ formatSignature(signature) {
100
+ // Add hex prefix and append 0 byte to indicate an ed25519 signature
101
+ return signature.replace(/^0x/, "0x00");
102
+ }
103
+ validateAddress(givenAddress) {
104
+ if (this.address !== givenAddress) {
105
+ throw new sdk_1.DfnsError(-1, 'address does not match the wallet used to initialize DfnsWallet', {
106
+ expectedAddress: this.address,
107
+ givenAddress,
108
+ });
109
+ }
110
+ }
111
+ }
112
+ exports.DfnsWallet = DfnsWallet;
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@dfns/lib-polymesh",
3
+ "version": "0.7.2-beta.1",
4
+ "peerDependencies": {
5
+ "@polkadot/api": "11.2.1",
6
+ "@polymeshassociation/signing-manager-types": "3.4.0",
7
+ "@dfns/sdk": "0.7.2-beta.1"
8
+ },
9
+ "dependencies": {
10
+ "@polkadot/types": "11.2.1",
11
+ "@polkadot/util": "12.6.2",
12
+ "buffer": "6.0.3",
13
+ "cross-fetch": "3.1.6"
14
+ },
15
+ "main": "./index.js",
16
+ "type": "commonjs"
17
+ }