@dfns/lib-solana 0.1.2 → 0.1.3-rc.2
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 +2 -5
- package/index.js +88 -49
- package/package.json +3 -3
package/index.d.ts
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { DfnsApiClient } from '@dfns/sdk';
|
|
3
2
|
import { PublicKey, Transaction, VersionedTransaction } from '@solana/web3.js';
|
|
4
3
|
export type DfnsWalletOptions = {
|
|
5
4
|
walletId: string;
|
|
6
5
|
dfnsClient: DfnsApiClient;
|
|
7
|
-
maxRetries?: number;
|
|
8
|
-
retryInterval?: number;
|
|
9
6
|
};
|
|
10
7
|
export declare class DfnsWallet {
|
|
8
|
+
private metadata;
|
|
11
9
|
readonly publicKey: PublicKey;
|
|
12
|
-
private
|
|
10
|
+
private readonly dfnsClient;
|
|
13
11
|
private constructor();
|
|
14
12
|
static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
|
|
15
13
|
get address(): string;
|
|
16
|
-
waitForSignature(signatureId: string): Promise<Buffer>;
|
|
17
14
|
signTransaction(transaction: Transaction): Promise<Transaction>;
|
|
18
15
|
signVersionedTransaction(transaction: VersionedTransaction): Promise<VersionedTransaction>;
|
|
19
16
|
}
|
package/index.js
CHANGED
|
@@ -1,70 +1,109 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DfnsWallet = void 0;
|
|
4
|
-
const
|
|
4
|
+
const sdk_1 = require("@dfns/sdk");
|
|
5
5
|
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
const
|
|
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
|
+
const combineSignature = (res) => {
|
|
18
|
+
if (!res.signature) {
|
|
19
|
+
throw new sdk_1.DfnsError(-1, 'signature missing', res);
|
|
20
|
+
}
|
|
21
|
+
const { r, s } = res.signature;
|
|
22
|
+
return Buffer.concat([hexToBuffer(r), hexToBuffer(s)]);
|
|
23
|
+
};
|
|
7
24
|
class DfnsWallet {
|
|
8
|
-
constructor(
|
|
9
|
-
this.
|
|
10
|
-
this.
|
|
11
|
-
|
|
12
|
-
maxRetries: options.maxRetries ?? 3,
|
|
13
|
-
retryInterval: options.retryInterval ?? 1000,
|
|
14
|
-
};
|
|
25
|
+
constructor(metadata, options) {
|
|
26
|
+
this.metadata = metadata;
|
|
27
|
+
this.dfnsClient = options.dfnsClient;
|
|
28
|
+
this.publicKey = new web3_js_1.PublicKey(Buffer.from(metadata.signingKey.publicKey, 'hex'));
|
|
15
29
|
}
|
|
16
30
|
static async init(options) {
|
|
17
31
|
const { walletId, dfnsClient } = options;
|
|
18
32
|
const res = await dfnsClient.wallets.getWallet({ walletId });
|
|
19
|
-
if (
|
|
20
|
-
throw new
|
|
33
|
+
if (res.status !== 'Active') {
|
|
34
|
+
throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
|
|
21
35
|
}
|
|
22
|
-
const
|
|
23
|
-
|
|
36
|
+
const { scheme, curve } = res.signingKey;
|
|
37
|
+
if (scheme !== 'EdDSA') {
|
|
38
|
+
throw new sdk_1.DfnsError(-1, 'key scheme is not EdDSA', { walletId, scheme });
|
|
39
|
+
}
|
|
40
|
+
if (curve !== 'ed25519') {
|
|
41
|
+
throw new sdk_1.DfnsError(-1, 'key curve is not ed25519', { walletId, curve });
|
|
42
|
+
}
|
|
43
|
+
const metadata = {
|
|
44
|
+
boundToSolanaNetwork: res.network === 'Solana' || res.network === 'SolanaDevnet',
|
|
45
|
+
...res,
|
|
46
|
+
};
|
|
47
|
+
return new DfnsWallet(metadata, options);
|
|
24
48
|
}
|
|
25
49
|
get address() {
|
|
26
50
|
return this.publicKey.toBase58();
|
|
27
51
|
}
|
|
28
|
-
async
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
break;
|
|
52
|
+
async signTransaction(transaction) {
|
|
53
|
+
if (this.metadata.boundToSolanaNetwork) {
|
|
54
|
+
const res = await this.dfnsClient.wallets.generateSignature({
|
|
55
|
+
walletId: this.metadata.id,
|
|
56
|
+
body: {
|
|
57
|
+
kind: 'Transaction',
|
|
58
|
+
transaction: `0x${transaction.serialize({ verifySignatures: false }).toString('hex')}`,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
assertSigned(res);
|
|
62
|
+
if (!res.signedData) {
|
|
63
|
+
throw new sdk_1.DfnsError(-1, 'signedData missing', res);
|
|
41
64
|
}
|
|
42
|
-
|
|
65
|
+
return web3_js_1.Transaction.from(hexToBuffer(res.signedData));
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const res = await this.dfnsClient.wallets.generateSignature({
|
|
69
|
+
walletId: this.metadata.id,
|
|
70
|
+
body: {
|
|
71
|
+
kind: 'Message',
|
|
72
|
+
message: `0x${transaction.serializeMessage().toString('hex')}`,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
assertSigned(res);
|
|
76
|
+
transaction.addSignature(this.publicKey, combineSignature(res));
|
|
77
|
+
return transaction;
|
|
43
78
|
}
|
|
44
|
-
const waitedSeconds = Math.floor((this.options.maxRetries * retryInterval) / 1000);
|
|
45
|
-
throw new Error(`Signature request ${signatureId} took more than ${waitedSeconds}s to complete, stopping polling. Please update options "maxRetries" or "retryIntervals" to wait longer.`);
|
|
46
|
-
}
|
|
47
|
-
async signTransaction(transaction) {
|
|
48
|
-
const message = transaction.serializeMessage();
|
|
49
|
-
const { walletId, dfnsClient } = this.options;
|
|
50
|
-
const res = await dfnsClient.wallets.generateSignature({
|
|
51
|
-
walletId,
|
|
52
|
-
body: { kind: Wallets_1.SignatureKind.Message, message: `0x${message.toString('hex')}` },
|
|
53
|
-
});
|
|
54
|
-
const signature = await this.waitForSignature(res.id);
|
|
55
|
-
transaction.addSignature(this.publicKey, signature);
|
|
56
|
-
return transaction;
|
|
57
79
|
}
|
|
58
80
|
async signVersionedTransaction(transaction) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
81
|
+
if (this.metadata.boundToSolanaNetwork) {
|
|
82
|
+
const res = await this.dfnsClient.wallets.generateSignature({
|
|
83
|
+
walletId: this.metadata.id,
|
|
84
|
+
body: {
|
|
85
|
+
kind: 'Transaction',
|
|
86
|
+
transaction: `0x${Buffer.from(transaction.serialize()).toString('hex')}`,
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
assertSigned(res);
|
|
90
|
+
if (!res.signedData) {
|
|
91
|
+
throw new sdk_1.DfnsError(-1, 'signedData missing', res);
|
|
92
|
+
}
|
|
93
|
+
return web3_js_1.VersionedTransaction.deserialize(hexToBuffer(res.signedData));
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
const res = await this.dfnsClient.wallets.generateSignature({
|
|
97
|
+
walletId: this.metadata.id,
|
|
98
|
+
body: {
|
|
99
|
+
kind: 'Message',
|
|
100
|
+
message: `0x${Buffer.from(transaction.message.serialize()).toString('hex')}`,
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
assertSigned(res);
|
|
104
|
+
transaction.addSignature(this.publicKey, combineSignature(res));
|
|
105
|
+
return transaction;
|
|
106
|
+
}
|
|
68
107
|
}
|
|
69
108
|
}
|
|
70
109
|
exports.DfnsWallet = DfnsWallet;
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dfns/lib-solana",
|
|
3
|
-
"version": "0.1.2",
|
|
3
|
+
"version": "0.1.3-rc.2",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@solana/web3.js": "1.
|
|
5
|
+
"@solana/web3.js": "1.87.6",
|
|
6
6
|
"buffer": "6.0.3",
|
|
7
7
|
"cross-fetch": "3.1.6",
|
|
8
8
|
"uuid": "9.0.0"
|
|
9
9
|
},
|
|
10
10
|
"peerDependencies": {
|
|
11
|
-
"@dfns/sdk": "0.1.2"
|
|
11
|
+
"@dfns/sdk": "0.1.3-rc.2"
|
|
12
12
|
},
|
|
13
13
|
"main": "./index.js",
|
|
14
14
|
"type": "commonjs"
|