-
Notifications
You must be signed in to change notification settings - Fork 110
/
in-memory-viewing-key.ts
73 lines (67 loc) · 2.3 KB
/
in-memory-viewing-key.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { b58cencode, Prefix, prefix } from '@taquito/utils';
import * as sapling from '@airgap/sapling-wasm';
import { InMemorySpendingKey } from './in-memory-spending-key';
/**
* @description Holds the viewing key
*/
export class InMemoryViewingKey {
#fullViewingKey: Buffer;
constructor(fullViewingKey: string) {
this.#fullViewingKey = Buffer.from(fullViewingKey, 'hex');
}
/**
* @description Allows to instantiate the InMemoryViewingKey from an encrypted/unencrypted spending key
*
* @param spendingKey Base58Check-encoded spending key
* @param password Optional password to decrypt the spending key
* @example
* ```
* await InMemoryViewingKey.fromSpendingKey('sask27SLmU9herddHz4qFJBLMjWYMbJF8RtS579w9ej9mfCYK7VUdyCJPHK8AzW9zMsopGZEkYeNjAY7Zz1bkM7CGu8eKLzrjBLTMC5wWJDhxiK91ahA29rhDRsHdJDV2u2jFwb2MNUix8JW7sAkAqYVaJpCehTBPgRQ1KqKwqqUaNmuD8kazd4Q8MCWmgbWs21Yuomdqyi9FLigjRp7oY4m5adaVU19Nj1AHvsMY2tePeU2L')
* ```
*
*/
static async fromSpendingKey(spendingKey: string, password?: string) {
const inMemorySpendingkey = new InMemorySpendingKey(spendingKey, password);
return inMemorySpendingkey.getSaplingViewingKeyProvider();
}
/**
* @description Retrieve the full viewing key
* @returns Buffer representing the full viewing key
*
*/
getFullViewingKey() {
return this.#fullViewingKey;
}
/**
* @description Retrieve the outgoing viewing key
* @returns Buffer representing the outgoing viewing key
*
*/
async getOutgoingViewingKey() {
return sapling.getOutgoingViewingKey(this.#fullViewingKey);
}
/**
* @description Retrieve the incoming viewing key
* @returns Buffer representing the incoming viewing key
*
*/
async getIncomingViewingKey() {
return sapling.getIncomingViewingKey(this.#fullViewingKey);
}
/**
* @description Retrieve a payment address
* @param addressIndex used to determine which diversifier should be used to derive the address, default is 0
* @returns Base58Check-encoded address and its index
*
*/
async getAddress(addressIndex?: number) {
const { index, raw } = await sapling.getPaymentAddressFromViewingKey(
this.#fullViewingKey,
addressIndex
);
return {
address: b58cencode(raw, prefix[Prefix.ZET1]),
addressIndex: index.readInt32LE(),
};
}
}