-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.ts
119 lines (106 loc) · 3.55 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
import { Aes, Crypt } from "eosjs-ecc";
import padStart from "lodash.padstart";
import Long from "long";
export let MEMO = "TO DECRYPT: eos-encrypt\n";
/**
* Encrypt Message
*
* @param {string} private_key EOSIO Private Key
* @param {string} public_key EOSIO Public Key
* @param {string} message Message to Encrypt
* @param {object} [options={}] Optional parameters
* @param {string} [options.memo="TO DECRYPT: eos-encrypt\n"] Serialized Memo
* @param {number} [options.maxsize=256] Maximum character message size
* @returns {string} Encrypted Message
* @example
*
* const encrypted = encrypt(private_key, public_key, message);
*/
export function encrypt(private_key: string, public_key: string, message: string, options: {
memo?: string,
maxsize?: number,
} = {}): string {
const memo = options.memo ? options.memo : MEMO;
const maxsize = options.maxsize ? options.maxsize : 256;
const buff = Aes.encrypt(private_key, public_key, message);
const str = serialize(buff, memo);
if (maxsize !== -1 && str.length > maxsize) { throw new Error(`message too long (max ${maxsize} chars)`); }
return str;
}
/**
* Decrypt Message
*
* @param {string} private_key EOSIO Private Key
* @param {string} public_key EOSIO Public Key
* @param {string} message Encrypted Message
* @param {object} [options={}] Optional parameters
* @param {string} [options.memo="TO DECRYPT: eos-encrypt\n"] Serialized Memo
* @returns {string} Decrypted Message
* @example
*
* const decrypted = decrypt(private_key, public_key, message);
*/
export function decrypt(private_key: string, public_key: string, message: string, options: {
memo?: string,
} = {}): string {
const memo = options.memo ? options.memo : MEMO;
const { nonce, content, checksum } = deserialize(message, memo);
const decrypted = Aes.decrypt(private_key, public_key, nonce, content, checksum);
return decrypted.toString("utf8");
}
/**
* Serialize
*
* @private
* @param {Crypt} buff Aes.encrypt => Object
* @param {string} [memo="TO DECRYPT: eos-encrypt\n"] Serialized Memo
* @returns {string} Serialized String
* @example
*
* const buff = Aes.encrypt(private_key, public_key, message);
* const str = serialize(buff);
*/
export function serialize(buff: Crypt, memo = MEMO) {
let str = memo;
str += padStart(buff.nonce.low.toString(), 11, ".");
str += padStart(buff.nonce.high.toString(), 11, ".");
str += padStart(buff.checksum.toString(), 11, ".");
str += buff.message.toString("base64");
return str;
}
/**
* Deserialize
*
* @private
* @param {string} message Message to deserialize
* @param {string} [memo="TO DECRYPT: eos-encrypt\n"] Serialized Memo
* @returns {Object} Deserialize Object
* @example
*
* const { nonce, content, checksum } = deserialize(message);
* const decrypted = Aes.decrypt(private_key, public_key, nonce, content, checksum);
*/
export function deserialize(message: string, memo = MEMO) {
message = message.replace(memo, "");
const low = parseInt(message.substring(0, 11).replace(/[.]/g, ""), 10);
const high = parseInt(message.substring(11, 22).replace(/[.]/g, ""), 10);
const checksum = parseInt(message.substring(22, 33).replace(/[.]/g, ""), 10);
message = message.substring(33, message.length);
return {
checksum,
content: Buffer.from(message, "base64"),
nonce: new Long(low, high, false),
};
}
/**
* Set Default Memo
*
* @param {string} memo Set Memo
* @returns {void}
* @example
*
* setMemo("TO DECRYPT: my-dapp\n");
*/
export function setMemo(memo: string) {
MEMO = memo;
}