@dfns/lib-sui 0.7.6

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 +19 -0
  2. package/index.js +78 -0
  3. package/package.json +15 -0
package/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ /// <reference types="node" />
2
+ import { DfnsApiClient } from '@dfns/sdk';
3
+ import { PublicKey, SignatureScheme, SignatureWithBytes, Signer } from '@mysten/sui/cryptography';
4
+ export type DfnsWalletOptions = {
5
+ walletId: string;
6
+ dfnsClient: DfnsApiClient;
7
+ };
8
+ export declare class DfnsWallet extends Signer {
9
+ private metadata;
10
+ sign(bytes: Uint8Array): Promise<Uint8Array>;
11
+ private readonly dfnsClient;
12
+ private constructor();
13
+ signTransaction(bytes: Uint8Array): Promise<SignatureWithBytes>;
14
+ getKeyScheme(): SignatureScheme;
15
+ getPublicKey(): PublicKey;
16
+ static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
17
+ get address(): string;
18
+ get publicKey(): Buffer;
19
+ }
package/index.js ADDED
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ /* eslint @typescript-eslint/no-unused-vars: 0 */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.DfnsWallet = void 0;
5
+ const sdk_1 = require("@dfns/sdk");
6
+ const cryptography_1 = require("@mysten/sui/cryptography");
7
+ const ed25519_1 = require("@mysten/sui/keypairs/ed25519");
8
+ const bcs_1 = require("@mysten/bcs");
9
+ const hexToBuffer = (hex) => {
10
+ return Buffer.from(stripHexPrefix(hex), 'hex');
11
+ };
12
+ const stripHexPrefix = (hex) => {
13
+ return hex.replace(/^0x/, '');
14
+ };
15
+ const assertSigned = (res) => {
16
+ if (res.status === 'Failed') {
17
+ throw new sdk_1.DfnsError(-1, 'signing failed', res);
18
+ }
19
+ else if (res.status !== 'Signed') {
20
+ throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
21
+ }
22
+ };
23
+ class DfnsWallet extends cryptography_1.Signer {
24
+ // no-lint
25
+ sign(bytes) {
26
+ throw new Error('Method not implemented.');
27
+ }
28
+ constructor(metadata, options) {
29
+ super();
30
+ this.metadata = metadata;
31
+ this.dfnsClient = options.dfnsClient;
32
+ this.sign = this.sign.bind(this);
33
+ }
34
+ async signTransaction(bytes) {
35
+ const res = await this.dfnsClient.wallets.generateSignature({
36
+ walletId: this.metadata.id,
37
+ body: {
38
+ kind: 'Transaction',
39
+ transaction: `0x${Buffer.from(bytes).toString('hex')}`,
40
+ },
41
+ });
42
+ assertSigned(res);
43
+ if (!res.signature?.encoded) {
44
+ throw new sdk_1.DfnsError(-1, 'signature missing', res);
45
+ }
46
+ return {
47
+ signature: hexToBuffer(res.signature.encoded).toString('base64'),
48
+ bytes: (0, bcs_1.toBase64)(bytes),
49
+ };
50
+ }
51
+ getKeyScheme() {
52
+ return 'ED25519';
53
+ }
54
+ getPublicKey() {
55
+ return new ed25519_1.Ed25519PublicKey(hexToBuffer(this.metadata.signingKey.publicKey));
56
+ }
57
+ static async init(options) {
58
+ const { walletId, dfnsClient } = options;
59
+ const res = await dfnsClient.wallets.getWallet({ walletId });
60
+ if (res.status !== 'Active') {
61
+ throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
62
+ }
63
+ if (res.network !== 'Sui' && res.network !== 'SuiTestnet') {
64
+ throw new sdk_1.DfnsError(-1, 'wallet is not bound to Sui or SuiTestnet', {
65
+ walletId,
66
+ network: res.network,
67
+ });
68
+ }
69
+ return new DfnsWallet(res, options);
70
+ }
71
+ get address() {
72
+ return this.metadata.address;
73
+ }
74
+ get publicKey() {
75
+ return Buffer.from(this.metadata.signingKey.publicKey, 'hex');
76
+ }
77
+ }
78
+ exports.DfnsWallet = DfnsWallet;
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@dfns/lib-sui",
3
+ "version": "0.7.6",
4
+ "peerDependencies": {
5
+ "@mysten/sui": "^1.0.0",
6
+ "@dfns/sdk": "0.7.6"
7
+ },
8
+ "dependencies": {
9
+ "@mysten/bcs": "1.7.0",
10
+ "buffer": "6.0.3",
11
+ "cross-fetch": "3.1.6"
12
+ },
13
+ "main": "./index.js",
14
+ "type": "commonjs"
15
+ }