@dfns/lib-solana 0.1.0-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 +18 -0
  2. package/index.js +61 -0
  3. package/package.json +15 -0
package/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ /// <reference types="node" />
2
+ import { DfnsApiClient } from '@dfns/sdk';
3
+ import { PublicKey, Transaction } from '@solana/web3.js';
4
+ export type DfnsWalletOptions = {
5
+ walletId: string;
6
+ dfnsClient: DfnsApiClient;
7
+ maxRetries?: number;
8
+ retryInterval?: number;
9
+ };
10
+ export declare class DfnsWallet {
11
+ readonly publicKey: PublicKey;
12
+ private options;
13
+ private constructor();
14
+ static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
15
+ get address(): string;
16
+ waitForSignature(signatureId: string): Promise<Buffer>;
17
+ signTransaction(transaction: Transaction): Promise<Transaction>;
18
+ }
package/index.js ADDED
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DfnsWallet = void 0;
4
+ const Wallets_1 = require("@dfns/sdk/codegen/datamodel/Wallets");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ const sleep = (interval = 0) => new Promise((resolve) => setTimeout(resolve, interval));
7
+ class DfnsWallet {
8
+ constructor(publicKey, options) {
9
+ var _a, _b;
10
+ this.publicKey = publicKey;
11
+ this.options = {
12
+ ...options,
13
+ maxRetries: (_a = options.maxRetries) !== null && _a !== void 0 ? _a : 3,
14
+ retryInterval: (_b = options.retryInterval) !== null && _b !== void 0 ? _b : 1000,
15
+ };
16
+ }
17
+ static async init(options) {
18
+ var _a, _b;
19
+ const { walletId, dfnsClient } = options;
20
+ const res = await dfnsClient.wallets.getWallet({ walletId });
21
+ if (!res.signingKey || res.signingKey.scheme !== Wallets_1.KeyScheme.EdDSA || res.signingKey.curve !== Wallets_1.KeyCurve.ed25519) {
22
+ throw new Error(`wallet ${walletId} has incompatible scheme (${(_a = res.signingKey) === null || _a === void 0 ? void 0 : _a.scheme}) or curve (${(_b = res.signingKey) === null || _b === void 0 ? void 0 : _b.curve})`);
23
+ }
24
+ const publicKey = new web3_js_1.PublicKey(Buffer.from(res.signingKey.publicKey, 'hex'));
25
+ return new DfnsWallet(publicKey, options);
26
+ }
27
+ get address() {
28
+ return this.publicKey.toBase58();
29
+ }
30
+ async waitForSignature(signatureId) {
31
+ const { walletId, dfnsClient, retryInterval } = this.options;
32
+ let maxRetries = this.options.maxRetries;
33
+ while (maxRetries > 0) {
34
+ await sleep(retryInterval);
35
+ const res = await dfnsClient.wallets.getSignature({ walletId, signatureId });
36
+ if (res.status === Wallets_1.SignatureStatus.Signed) {
37
+ if (!res.signature)
38
+ break;
39
+ return Buffer.from(res.signature.r.substring(2).concat(res.signature.s.substring(2)), 'hex');
40
+ }
41
+ else if (res.status === Wallets_1.SignatureStatus.Failed) {
42
+ break;
43
+ }
44
+ maxRetries -= 1;
45
+ }
46
+ throw new Error(`signature ${signatureId} not available`);
47
+ }
48
+ async signTransaction(transaction) {
49
+ const message = transaction.serializeMessage();
50
+ console.log(message.length);
51
+ const { walletId, dfnsClient } = this.options;
52
+ const res = await dfnsClient.wallets.generateSignature({
53
+ walletId,
54
+ body: { kind: Wallets_1.SignatureKind.Message, message: `0x${message.toString('hex')}` },
55
+ });
56
+ const signature = await this.waitForSignature(res.id);
57
+ transaction.addSignature(this.publicKey, signature);
58
+ return transaction;
59
+ }
60
+ }
61
+ exports.DfnsWallet = DfnsWallet;
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@dfns/lib-solana",
3
+ "version": "0.1.0-beta.1",
4
+ "dependencies": {
5
+ "@solana/web3.js": "1.78.0",
6
+ "buffer": "6.0.3",
7
+ "cross-fetch": "3.1.6",
8
+ "uuid": "9.0.0"
9
+ },
10
+ "peerDependencies": {
11
+ "@dfns/sdk": "0.1.0-beta.1"
12
+ },
13
+ "main": "./index.js",
14
+ "types": "./index.d.ts"
15
+ }