-
Notifications
You must be signed in to change notification settings - Fork 1
/
Memo.ts
69 lines (60 loc) · 2.42 KB
/
Memo.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
import { Expose } from "class-transformer";
import * as _ from "lodash";
import * as Long from "long";
import { Address } from "../crypto/Address";
import { ECKeyPair } from "../crypto/ECKeyPair";
import { AddressToClass, AddressToPlain, LongToClass, LongToPlain } from "../net/adapter/TypeAdapters";
import { Utils } from "../utils/Utils";
export class Memo {
public static createPublic(message: string): Memo {
return new Memo(Buffer.concat([Buffer.alloc(4, 0), Buffer.from(message)]).toString("hex"));
}
public static createEncrypted(message: string, keyPair: ECKeyPair, recipient: Address, nonce: Long = Utils.generateNonce()): Memo {
const msgBytes = Buffer.from(message);
const withChecksum = Buffer.concat([Utils.hash256(msgBytes).slice(0, 4), msgBytes]);
const secret = keyPair.secret(recipient, nonce);
const cipherText = Utils.encrypt(secret, withChecksum);
return new Memo(cipherText.toString("hex"), keyPair.publicAddress, recipient, nonce);
}
@AddressToClass
@AddressToPlain
@Expose({ name: "from" })
public from?: Address;
@AddressToClass
@AddressToPlain
@Expose({ name: "to" })
public to?: Address;
@Expose({ name: "message" })
public message: string;
// UInt64
@LongToClass
@LongToPlain
@Expose({ name: "nonce" })
public nonce: Long;
private constructor(message: string, from?: Address, to?: Address, nonce: Long = Long.ZERO) {
this.from = from;
this.to = to;
this.message = message;
this.nonce = nonce;
}
public decrypt(keyPair: ECKeyPair): string {
if (_.isNil(this.from) || _.isNil(this.to)) {
return Buffer.from(this.message, "hex").slice(4).toString("utf8");
} else if (this.from.publicKey === keyPair.publicKey) {
return this.decryptWithChecksum(keyPair.secret(this.to, this.nonce));
} else if (this.to.publicKey === keyPair.publicKey) {
return this.decryptWithChecksum(keyPair.secret(this.from, this.nonce));
} else {
return "";
}
}
private decryptWithChecksum(secret: Buffer) {
const clearText = Utils.decrypt(secret, Buffer.from(this.message, "hex"));
const msg = clearText.slice(4);
if (Utils.hash256(msg).slice(0, 4).equals(clearText.slice(0, 4))) {
return msg.toString("utf8");
} else {
return "";
}
}
}