-
Notifications
You must be signed in to change notification settings - Fork 443
/
index.ts
127 lines (108 loc) · 3.9 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import type { CID } from 'multiformats/cid'
import type { MultihashDigest } from 'multiformats/hashes/interface'
import type { Uint8ArrayList } from 'uint8arraylist'
export type KeyType = 'RSA' | 'Ed25519' | 'secp256k1'
interface PublicKeyBase<KeyType extends string, DigestCode extends number = number> {
/**
* The type of this key
*/
readonly type: KeyType
/**
* The raw public key bytes (for Ed25519 and secp256k1 keys) or PKIX in ASN1
* DER format (for RSA keys)
*/
readonly raw: Uint8Array
/**
* Returns `true` if the passed object matches this key
*/
equals(key?: any): boolean
/**
* Returns this public key as a Multihash digest.
*
* It contains either an identity hash containing the protobuf version of the
* public key (for Ed25519 and secp256k1 keys) or a sha256 hash of the
* protobuf version of the public key (RSA keys).
*/
toMultihash(): MultihashDigest<DigestCode>
/**
* Return this public key as a CID encoded with the `libp2p-key` codec
*
* The digest contains either an identity hash containing the protobuf version
* of the public key (for Ed25519 and secp256k1 keys) or a sha256 hash of the
* protobuf version of the public key (RSA keys).
*/
toCID(): CID<unknown, 0x72, DigestCode, 1>
/**
* Verify the passed data was signed by the private key corresponding to this
* public key
*/
verify(data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean | Promise<boolean>
/**
* Returns this key as a multihash with base58btc encoding
*/
toString(): string
}
export interface RSAPublicKey extends PublicKeyBase<'RSA', 0x12> {}
export interface Ed25519PublicKey extends PublicKeyBase<'Ed25519', 0x0> {}
export interface Secp256k1PublicKey extends PublicKeyBase<'secp256k1', 0x0> {}
export type PublicKey = RSAPublicKey | Ed25519PublicKey | Secp256k1PublicKey
/**
* Returns true if the passed argument has type overlap with the `PublicKey`
* interface. Can be used to disambiguate object types.
*/
export function isPublicKey (key?: any): key is PublicKey {
if (key == null) {
return false
}
return (key.type === 'RSA' || key.type === 'Ed25519' || key.type === 'secp256k1') &&
key.raw instanceof Uint8Array &&
typeof key.equals === 'function' &&
typeof key.toMultihash === 'function' &&
typeof key.toCID === 'function' &&
typeof key.verify === 'function'
}
/**
* Generic private key interface
*/
interface PrivateKeyBase<KeyType extends string, PublicKeyType extends PublicKeyBase<KeyType>> {
/**
* The type of this key
*/
readonly type: KeyType
/**
* The public key that corresponds to this private key
*/
readonly publicKey: PublicKeyType
/**
* The raw public key bytes (for Ed25519 and secp256k1 keys) or PKIX in ASN1
* DER format (for RSA keys)
*/
readonly raw: Uint8Array
/**
* Returns `true` if the passed object matches this key
*/
equals(key?: any): boolean
/**
* Sign the passed data with this private key and return the signature for
* later verification
*/
sign(data: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array>
}
export interface RSAPrivateKey extends PrivateKeyBase<'RSA', RSAPublicKey> {}
export interface Ed25519PrivateKey extends PrivateKeyBase<'Ed25519', Ed25519PublicKey> {}
export interface Secp256k1PrivateKey extends PrivateKeyBase<'secp256k1', Secp256k1PublicKey> {}
export type PrivateKey = RSAPrivateKey | Ed25519PrivateKey | Secp256k1PrivateKey
/**
* Returns true if the passed argument has type overlap with the `PrivateKey`
* interface. Can be used to disambiguate object types.
*/
export function isPrivateKey (key?: any): key is PrivateKey {
if (key == null) {
return false
}
return (key.type === 'RSA' || key.type === 'Ed25519' || key.type === 'secp256k1') &&
isPublicKey(key.publicKey) &&
key.raw instanceof Uint8Array &&
typeof key.equals === 'function' &&
typeof key.sign === 'function'
}