-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathkeys.ts
73 lines (64 loc) · 2.4 KB
/
keys.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 {
generate_spend_key,
get_address_by_index,
get_ephemeral_address,
get_full_viewing_key,
get_noble_forwarding_addr,
get_transparent_address,
get_transmission_key_by_address,
get_wallet_id,
} from '../wasm/index.js';
import {
Address,
FullViewingKey,
SpendKey,
WalletId,
} from '@penumbra-zone/protobuf/penumbra/core/keys/v1/keys_pb';
export const generateSpendKey = (seedPhrase: string) =>
SpendKey.fromBinary(generate_spend_key(seedPhrase));
export const getFullViewingKey = (spendKey: SpendKey) =>
FullViewingKey.fromBinary(get_full_viewing_key(spendKey.toBinary()));
export const getAddressByIndex = (fullViewingKey: FullViewingKey, index: number) => {
const bytes = get_address_by_index(fullViewingKey.toBinary(), index);
return Address.fromBinary(bytes);
};
export const getEphemeralByIndex = (fullViewingKey: FullViewingKey, index: number) => {
const bytes = get_ephemeral_address(fullViewingKey.toBinary(), index);
return Address.fromBinary(bytes);
};
export const getWalletId = (fullViewingKey: FullViewingKey) =>
WalletId.fromBinary(get_wallet_id(fullViewingKey.toBinary()));
export interface NobleAddrResponse {
// A noble address that will be used for registration on the noble network
nobleAddrBech32: string;
// Byte representation of the noble forwarding address. Used for broadcasting cosmos message.
nobleAddrBytes: Uint8Array;
// The penumbra address that a deposit to the noble address with forward to
penumbraAddr: Address;
}
// Generates an address that can be used as a forwarding address for Noble
export const getNobleForwardingAddr = (
sequence: number,
fvk: FullViewingKey,
channel: string,
account?: number,
): NobleAddrResponse => {
const res = get_noble_forwarding_addr(sequence, fvk.toBinary(), channel, account);
return {
nobleAddrBech32: res.noble_addr_bech32,
nobleAddrBytes: res.noble_addr_bytes,
penumbraAddr: Address.fromBinary(res.penumbra_addr_bytes),
};
};
// Generates a transparent address that ensures bech32m encoding compatibility.
export const getTransparentAddress = (fvk: FullViewingKey) => {
const res = get_transparent_address(fvk.toBinary());
return {
address: Address.fromBinary(res.address),
encoding: res.encoding,
};
};
export const getTransmissionKeyByAddress = (address: Address) => {
const transmission_key = get_transmission_key_by_address(address.toBinary());
return transmission_key;
};