@dfns/lib-ton 0.5.8
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 +15 -0
- package/index.js +57 -0
- package/package.json +15 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { DfnsApiClient } from '@dfns/sdk';
|
|
3
|
+
import { Cell } from '@ton/ton';
|
|
4
|
+
export type DfnsWalletOptions = {
|
|
5
|
+
walletId: string;
|
|
6
|
+
dfnsClient: DfnsApiClient;
|
|
7
|
+
};
|
|
8
|
+
export declare class DfnsWallet {
|
|
9
|
+
private metadata;
|
|
10
|
+
private readonly dfnsClient;
|
|
11
|
+
private constructor();
|
|
12
|
+
static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
|
|
13
|
+
get publicKey(): Buffer;
|
|
14
|
+
sign(message: Cell): Promise<Buffer>;
|
|
15
|
+
}
|
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 hexToBuffer = (hex) => {
|
|
6
|
+
return Buffer.from(stripHexPrefix(hex), 'hex');
|
|
7
|
+
};
|
|
8
|
+
const stripHexPrefix = (hex) => {
|
|
9
|
+
return hex.replace(/^0x/, '');
|
|
10
|
+
};
|
|
11
|
+
const assertSigned = (res) => {
|
|
12
|
+
if (res.status === 'Failed') {
|
|
13
|
+
throw new sdk_1.DfnsError(-1, 'signing failed', res);
|
|
14
|
+
}
|
|
15
|
+
else if (res.status !== 'Signed') {
|
|
16
|
+
throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
class DfnsWallet {
|
|
20
|
+
constructor(metadata, options) {
|
|
21
|
+
this.metadata = metadata;
|
|
22
|
+
this.dfnsClient = options.dfnsClient;
|
|
23
|
+
this.sign = this.sign.bind(this);
|
|
24
|
+
}
|
|
25
|
+
static async init(options) {
|
|
26
|
+
const { walletId, dfnsClient } = options;
|
|
27
|
+
const res = await dfnsClient.wallets.getWallet({ walletId });
|
|
28
|
+
if (res.status !== 'Active') {
|
|
29
|
+
throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
|
|
30
|
+
}
|
|
31
|
+
if (res.network !== 'Ton' && res.network !== 'TonTestnet') {
|
|
32
|
+
throw new sdk_1.DfnsError(-1, 'wallet is not bound to Ton or TonTestnet', {
|
|
33
|
+
walletId,
|
|
34
|
+
network: res.network,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return new DfnsWallet(res, options);
|
|
38
|
+
}
|
|
39
|
+
get publicKey() {
|
|
40
|
+
return Buffer.from(this.metadata.signingKey.publicKey, 'hex');
|
|
41
|
+
}
|
|
42
|
+
async sign(message) {
|
|
43
|
+
const res = await this.dfnsClient.wallets.generateSignature({
|
|
44
|
+
walletId: this.metadata.id,
|
|
45
|
+
body: {
|
|
46
|
+
kind: 'Message',
|
|
47
|
+
message: `0x${message.toBoc().toString('hex')}`,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
assertSigned(res);
|
|
51
|
+
if (!res.signature?.encoded) {
|
|
52
|
+
throw new sdk_1.DfnsError(-1, 'signature missing', res);
|
|
53
|
+
}
|
|
54
|
+
return hexToBuffer(res.signature?.encoded);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.DfnsWallet = DfnsWallet;
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dfns/lib-ton",
|
|
3
|
+
"version": "0.5.8",
|
|
4
|
+
"peerDependencies": {
|
|
5
|
+
"@ton/ton": "^15.0.0",
|
|
6
|
+
"@dfns/sdk": "0.5.8"
|
|
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
|
+
}
|