-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
keypair.ts
72 lines (66 loc) · 2.39 KB
/
keypair.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
import { AuthorShortname } from "../util/doc-types.ts";
import { isErr, ValidationError } from "../util/errors.ts";
import { base32BytesToString, base32StringToBytes } from "./base32.ts";
import { AuthorKeypair, KeypairBytes, ShareKeypair } from "./crypto-types.ts";
import {
assembleAuthorAddress,
assembleShareAddress,
parseAuthorOrShareAddress,
} from "../core-validators/addresses.ts";
//================================================================================
/** Combine a shortname with a raw KeypairBytes to make an AuthorKeypair */
export function encodeAuthorKeypairToStrings(
shortname: AuthorShortname,
pair: KeypairBytes,
): AuthorKeypair {
return ({
address: assembleAuthorAddress(shortname, base32BytesToString(pair.pubkey)),
secret: base32BytesToString(pair.secret),
});
}
/** Combine a name with a raw KeypairBytes to make an ShareKeypair */
export function encodeShareKeypairToStrings(
name: string,
pair: KeypairBytes,
) {
return ({
address: assembleShareAddress(name, base32BytesToString(pair.pubkey)),
secret: base32BytesToString(pair.secret),
});
}
/** Convert a keypair (author / share) back into a raw KeypairBytes for use in crypto operations. */
export function decodeKeypairToBytes(
pair: AuthorKeypair | ShareKeypair,
): KeypairBytes | ValidationError {
try {
const address = isAuthorKeypair(pair) ? pair.address : pair.shareAddress;
const parsed = parseAuthorOrShareAddress(address);
if (isErr(parsed)) return parsed;
const bytes = {
pubkey: base32StringToBytes(parsed.pubkey),
secret: base32StringToBytes(pair.secret),
};
if (bytes.pubkey.length !== 32) {
// this is already checked by parseAuthorAddress so we can't test it here
// but we'll test it again just to make sure.
return new ValidationError(
`pubkey bytes should be 32 bytes long, not ${bytes.pubkey.length} after base32 decoding. ${address}`,
);
}
if (bytes.secret.length !== 32) {
return new ValidationError(
`secret bytes should be 32 bytes long, not ${bytes.secret.length} after base32 decoding. ${pair.secret}`,
);
}
return bytes;
} catch (err) {
return new ValidationError(
"crash while decoding author keypair: " + err.message,
);
}
}
export function isAuthorKeypair(
keypair: AuthorKeypair | ShareKeypair,
): keypair is AuthorKeypair {
return "address" in keypair;
}