@dfns/lib-taquito 0.2.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.
- package/addHexPrefix.d.ts +1 -0
- package/addHexPrefix.js +7 -0
- package/constant.d.ts +1 -0
- package/constant.js +4 -0
- package/index.d.ts +25 -0
- package/index.js +89 -0
- package/package.json +6 -0
- package/uint8ArraysEqual.d.ts +1 -0
- package/uint8ArraysEqual.js +18 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function addHexPrefix(inputString: string): string;
|
package/addHexPrefix.js
ADDED
package/constant.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const TxWatermark: Uint8Array;
|
package/constant.js
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { DfnsApiClient } from '@dfns/sdk';
|
|
2
|
+
import { Signer } from '@taquito/taquito';
|
|
3
|
+
export type DfnsWalletOptions = {
|
|
4
|
+
walletId: string;
|
|
5
|
+
dfnsClient: DfnsApiClient;
|
|
6
|
+
};
|
|
7
|
+
export declare class DfnsWallet implements Signer {
|
|
8
|
+
private metadata;
|
|
9
|
+
private readonly dfnsClient;
|
|
10
|
+
private tezosPubKey;
|
|
11
|
+
private constructor();
|
|
12
|
+
static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
|
|
13
|
+
get address(): string;
|
|
14
|
+
publicKey(): Promise<string>;
|
|
15
|
+
publicKeyHash(): Promise<string>;
|
|
16
|
+
secretKey(): Promise<string | undefined>;
|
|
17
|
+
sign(op: string, magicByte?: Uint8Array | undefined): Promise<{
|
|
18
|
+
bytes: string;
|
|
19
|
+
sig: string;
|
|
20
|
+
prefixSig: string;
|
|
21
|
+
sbytes: string;
|
|
22
|
+
}>;
|
|
23
|
+
private generateSignatureForTransaction;
|
|
24
|
+
private generateSignatureForHash;
|
|
25
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DfnsWallet = void 0;
|
|
4
|
+
const sdk_1 = require("@dfns/sdk");
|
|
5
|
+
const utils_1 = require("@taquito/utils");
|
|
6
|
+
const constant_1 = require("./constant");
|
|
7
|
+
const blake2b_1 = require("@stablelib/blake2b");
|
|
8
|
+
const uint8ArraysEqual_1 = require("./uint8ArraysEqual");
|
|
9
|
+
const addHexPrefix_1 = require("./addHexPrefix");
|
|
10
|
+
const assertSigned = (res) => {
|
|
11
|
+
if (res.status === 'Failed') {
|
|
12
|
+
throw new sdk_1.DfnsError(-1, 'signing failed', res);
|
|
13
|
+
}
|
|
14
|
+
else if (res.status !== 'Signed') {
|
|
15
|
+
throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
class DfnsWallet {
|
|
19
|
+
constructor(metadata, options) {
|
|
20
|
+
this.metadata = metadata;
|
|
21
|
+
this.dfnsClient = options.dfnsClient;
|
|
22
|
+
// get the public key encoded with the right tezos prefix.
|
|
23
|
+
this.tezosPubKey = (0, utils_1.b58cencode)(metadata.signingKey.publicKey, utils_1.prefix[metadata.signingKey.scheme === 'EdDSA' ? utils_1.Prefix.EDPK : utils_1.Prefix.SPPK]);
|
|
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 !== 'Tezos' && res.network !== 'TezosGhostnet') {
|
|
32
|
+
throw new sdk_1.DfnsError(-1, 'wallet is not bound to Tezos or TezosGhostnet', { walletId, network: res.network });
|
|
33
|
+
}
|
|
34
|
+
return new DfnsWallet(res, options);
|
|
35
|
+
}
|
|
36
|
+
get address() {
|
|
37
|
+
// Tezos-bound wallets will have a tezos address
|
|
38
|
+
return this.metadata.address;
|
|
39
|
+
}
|
|
40
|
+
// publicKey implements the Taquito Signer interface
|
|
41
|
+
async publicKey() {
|
|
42
|
+
return this.tezosPubKey;
|
|
43
|
+
}
|
|
44
|
+
// publicKeyHash implements the Taquito Signer interface
|
|
45
|
+
async publicKeyHash() {
|
|
46
|
+
return this.metadata.address;
|
|
47
|
+
}
|
|
48
|
+
// secretKey implements the Taquito Signer interface
|
|
49
|
+
async secretKey() {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
// sign implements the Taquito Signer interface
|
|
53
|
+
async sign(op, magicByte) {
|
|
54
|
+
let res;
|
|
55
|
+
if ((0, uint8ArraysEqual_1.uint8ArraysEqual)(magicByte, constant_1.TxWatermark)) {
|
|
56
|
+
res = await this.generateSignatureForTransaction(op);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
res = await this.generateSignatureForHash(op, magicByte);
|
|
60
|
+
}
|
|
61
|
+
assertSigned(res);
|
|
62
|
+
const signature = res.signature.encoded.replace(/^0x/, '');
|
|
63
|
+
const sigPrefix = this.metadata.signingKey.scheme === 'ECDSA' ? utils_1.prefix.spsig : utils_1.prefix.edsig;
|
|
64
|
+
return {
|
|
65
|
+
bytes: op,
|
|
66
|
+
sig: (0, utils_1.b58cencode)(signature, utils_1.prefix.sig),
|
|
67
|
+
sbytes: res.signedData.replace(/^0x/, ''),
|
|
68
|
+
prefixSig: (0, utils_1.b58cencode)(signature, sigPrefix),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async generateSignatureForTransaction(transaction) {
|
|
72
|
+
return await this.dfnsClient.wallets.generateSignature({
|
|
73
|
+
walletId: this.metadata.id,
|
|
74
|
+
body: { kind: 'Transaction', transaction: (0, addHexPrefix_1.addHexPrefix)(transaction) },
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async generateSignatureForHash(op, magicByte) {
|
|
78
|
+
let data = (0, utils_1.hex2buf)(op);
|
|
79
|
+
if (magicByte) {
|
|
80
|
+
data = (0, utils_1.mergebuf)(magicByte, data);
|
|
81
|
+
}
|
|
82
|
+
const bytesHash = (0, blake2b_1.hash)(data, 32);
|
|
83
|
+
return await this.dfnsClient.wallets.generateSignature({
|
|
84
|
+
walletId: this.metadata.id,
|
|
85
|
+
body: { kind: 'Hash', hash: (0, addHexPrefix_1.addHexPrefix)((0, utils_1.buf2hex)(bytesHash)) },
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.DfnsWallet = DfnsWallet;
|
package/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uint8ArraysEqual(arr1: Uint8Array | undefined, arr2: Uint8Array | undefined): boolean;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uint8ArraysEqual = void 0;
|
|
4
|
+
function uint8ArraysEqual(arr1, arr2) {
|
|
5
|
+
if (!arr1 && !arr2) {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
if ((!arr1 && arr2) || (arr1 && !arr2) || arr1.length !== arr2.length) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
for (let i = 0; i < arr1.length; i++) {
|
|
12
|
+
if (arr1[i] !== arr2[i]) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
exports.uint8ArraysEqual = uint8ArraysEqual;
|