@dfns/lib-near 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.
- package/index.d.ts +22 -0
- package/index.js +94 -0
- package/package.json +19 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DfnsApiClient } from '@dfns/sdk';
|
|
2
|
+
import { PublicKey } from '@near-js/crypto';
|
|
3
|
+
import { SignedMessage, Signer } from '@near-js/signers';
|
|
4
|
+
import { Transaction, SignedTransaction, DelegateAction, SignedDelegate } from '@near-js/transactions';
|
|
5
|
+
export declare const hexToBuffer: (hex: string) => Buffer;
|
|
6
|
+
export type DfnsWalletOptions = {
|
|
7
|
+
walletId: string;
|
|
8
|
+
dfnsClient: DfnsApiClient;
|
|
9
|
+
};
|
|
10
|
+
export declare class DfnsWallet implements Signer {
|
|
11
|
+
private metadata;
|
|
12
|
+
private readonly dfnsClient;
|
|
13
|
+
static supportedNetworks: string[];
|
|
14
|
+
private constructor();
|
|
15
|
+
getPublicKey(): Promise<PublicKey>;
|
|
16
|
+
get address(): string;
|
|
17
|
+
get publicKeyHex(): string;
|
|
18
|
+
signNep413Message(_message: string, _accountId: string, _recipient: string, _nonce: Uint8Array, _callbackUrl?: string): Promise<SignedMessage>;
|
|
19
|
+
signTransaction(transaction: Transaction): Promise<[Uint8Array, SignedTransaction]>;
|
|
20
|
+
signDelegateAction(_delegateAction: DelegateAction): Promise<[Uint8Array, SignedDelegate]>;
|
|
21
|
+
static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
|
|
22
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DfnsWallet = exports.hexToBuffer = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
5
|
+
const sdk_1 = require("@dfns/sdk");
|
|
6
|
+
const crypto_1 = require("@near-js/crypto");
|
|
7
|
+
const transactions_1 = require("@near-js/transactions");
|
|
8
|
+
const utils_1 = require("@near-js/utils");
|
|
9
|
+
const sha2_1 = require("@noble/hashes/sha2");
|
|
10
|
+
const hexToBuffer = (hex) => {
|
|
11
|
+
return Buffer.from(stripHexPrefix(hex), 'hex');
|
|
12
|
+
};
|
|
13
|
+
exports.hexToBuffer = hexToBuffer;
|
|
14
|
+
const stripHexPrefix = (hex) => {
|
|
15
|
+
return hex.replace(/^0x/, '');
|
|
16
|
+
};
|
|
17
|
+
const assertSigned = (response) => {
|
|
18
|
+
if (response.status === 'Failed') {
|
|
19
|
+
throw new sdk_1.DfnsError(-1, 'signing failed', response);
|
|
20
|
+
}
|
|
21
|
+
else if (response.status !== 'Signed') {
|
|
22
|
+
throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', response);
|
|
23
|
+
}
|
|
24
|
+
else if (!response.signature || !response.signature.encoded) {
|
|
25
|
+
throw new sdk_1.DfnsError(-1, 'signature missing', response);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
class DfnsWallet {
|
|
29
|
+
constructor(metadata, options) {
|
|
30
|
+
this.metadata = metadata;
|
|
31
|
+
this.dfnsClient = options.dfnsClient;
|
|
32
|
+
}
|
|
33
|
+
async getPublicKey() {
|
|
34
|
+
const publicKeyBytes = Buffer.from(this.metadata.signingKey.publicKey, 'hex');
|
|
35
|
+
return new crypto_1.PublicKey({
|
|
36
|
+
keyType: crypto_1.KeyType.ED25519,
|
|
37
|
+
data: (0, exports.hexToBuffer)(this.metadata.address),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
get address() {
|
|
41
|
+
if (!this.metadata.address) {
|
|
42
|
+
throw new sdk_1.DfnsError(-1, 'wallet address missing from metadata');
|
|
43
|
+
}
|
|
44
|
+
return this.metadata.address;
|
|
45
|
+
}
|
|
46
|
+
get publicKeyHex() {
|
|
47
|
+
return this.metadata.signingKey.publicKey;
|
|
48
|
+
}
|
|
49
|
+
signNep413Message(_message, _accountId, _recipient, _nonce, _callbackUrl) {
|
|
50
|
+
throw new Error('Method not implemented.');
|
|
51
|
+
}
|
|
52
|
+
async signTransaction(transaction) {
|
|
53
|
+
const res = await this.dfnsClient.wallets.generateSignature({
|
|
54
|
+
walletId: this.metadata.id,
|
|
55
|
+
body: {
|
|
56
|
+
kind: 'Transaction',
|
|
57
|
+
transaction: '0x' + Buffer.from(transaction.encode()).toString('hex'),
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
assertSigned(res);
|
|
61
|
+
if (!res.signedData) {
|
|
62
|
+
throw new sdk_1.DfnsError(-1, 'signedData missing', res);
|
|
63
|
+
}
|
|
64
|
+
const signedTransaction = transactions_1.SignedTransaction.decode((0, exports.hexToBuffer)(res.signedData));
|
|
65
|
+
const txHash = (0, utils_1.baseEncode)((0, sha2_1.sha256)((0, transactions_1.encodeTransaction)(signedTransaction.transaction)));
|
|
66
|
+
return [
|
|
67
|
+
(0, exports.hexToBuffer)(txHash),
|
|
68
|
+
signedTransaction,
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
72
|
+
signDelegateAction(_delegateAction) {
|
|
73
|
+
throw new Error('Method not implemented.');
|
|
74
|
+
}
|
|
75
|
+
static async init(options) {
|
|
76
|
+
const { walletId, dfnsClient } = options;
|
|
77
|
+
const res = await dfnsClient.wallets.getWallet({ walletId });
|
|
78
|
+
if (res.status !== 'Active') {
|
|
79
|
+
throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
|
|
80
|
+
}
|
|
81
|
+
if (!DfnsWallet.supportedNetworks.includes(res.network)) {
|
|
82
|
+
throw new sdk_1.DfnsError(-1, 'wallet is not bound to a Near compatible network', {
|
|
83
|
+
walletId,
|
|
84
|
+
network: res.network,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (!res.address) {
|
|
88
|
+
throw new sdk_1.DfnsError(-1, 'wallet address missing', { walletId });
|
|
89
|
+
}
|
|
90
|
+
return new DfnsWallet(res, options);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.DfnsWallet = DfnsWallet;
|
|
94
|
+
DfnsWallet.supportedNetworks = ['Near', 'NearTestnet'];
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dfns/lib-near",
|
|
3
|
+
"version": "0.7.6",
|
|
4
|
+
"peerDependencies": {
|
|
5
|
+
"near-api-js": "^6.0.0",
|
|
6
|
+
"@dfns/sdk": "0.7.6"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@near-js/crypto": "2.2.4",
|
|
10
|
+
"@near-js/signers": "2.2.4",
|
|
11
|
+
"@near-js/transactions": "2.2.4",
|
|
12
|
+
"@near-js/utils": "2.2.4",
|
|
13
|
+
"@noble/hashes": "1.8.0",
|
|
14
|
+
"buffer": "6.0.3",
|
|
15
|
+
"cross-fetch": "3.1.6"
|
|
16
|
+
},
|
|
17
|
+
"main": "./index.js",
|
|
18
|
+
"type": "commonjs"
|
|
19
|
+
}
|