-
Notifications
You must be signed in to change notification settings - Fork 3
/
EncryptionPublicKey.ts
42 lines (34 loc) · 1.08 KB
/
EncryptionPublicKey.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
import { sha256 } from '../crypto/sha256';
import type { PublicKeyType } from './PublicKeyType';
import type { PublicKey } from './PublicKey';
import type { SmartBuffer } from '@ylide/smart-buffer';
export class EncryptionPublicKey {
private readonly _signature: number;
constructor(public readonly type: PublicKeyType, public readonly keyBytes: Uint8Array) {
this._signature = new Uint32Array(sha256(this.keyBytes).slice(0, 4).buffer)[0];
}
get signature(): number {
return this._signature;
}
static fromPackedBytesInBuffer(buf: SmartBuffer) {
const version = buf.readUint8();
const type = buf.readUint8();
const keyBytes = buf.readBytes16Length();
return new EncryptionPublicKey(type, keyBytes);
}
getPackedSize() {
return 1 + 1 + 2 + this.keyBytes.length;
}
toPackedBytesInBuffer(buf: SmartBuffer) {
buf.writeUint8(1);
buf.writeUint8(this.type);
buf.writeBytes16Length(this.keyBytes);
}
equals(b: PublicKey) {
return (
b.type === this.type &&
b.keyBytes.length === this.keyBytes.length &&
b.keyBytes.every((v, i) => this.keyBytes[i] === v)
);
}
}