This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathed.ts
177 lines (152 loc) · 4.92 KB
/
ed.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright © 2022 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
import * as crypto from 'crypto';
import { Mnemonic } from '@liskhq/lisk-passphrase';
import { encode as encodeVarInt } from 'varuint-bitcoin';
import { parseKeyDerivationPath, hash, tagMessage } from './utils';
import { ED25519_CURVE, SIGNED_MESSAGE_PREFIX } from './constants';
import {
NACL_SIGN_PUBLICKEY_LENGTH,
NACL_SIGN_SIGNATURE_LENGTH,
getKeyPair,
getPublicKey,
signDetached,
verifyDetached,
} from './nacl';
const createHeader = (text: string): string => `-----${text}-----`;
const signedMessageHeader = createHeader('BEGIN LISK SIGNED MESSAGE');
const messageHeader = createHeader('MESSAGE');
const publicKeyHeader = createHeader('PUBLIC KEY');
const signatureHeader = createHeader('SIGNATURE');
const signatureFooter = createHeader('END LISK SIGNED MESSAGE');
const SIGNED_MESSAGE_PREFIX_BYTES = Buffer.from(SIGNED_MESSAGE_PREFIX, 'utf8');
const SIGNED_MESSAGE_PREFIX_LENGTH = encodeVarInt(SIGNED_MESSAGE_PREFIX.length);
export const digestMessage = (message: string): Buffer => {
const msgBytes = Buffer.from(message, 'utf8');
const msgLenBytes = encodeVarInt(message.length);
const dataBytes = Buffer.concat([
SIGNED_MESSAGE_PREFIX_LENGTH,
SIGNED_MESSAGE_PREFIX_BYTES,
msgLenBytes,
msgBytes,
]);
return hash(hash(dataBytes));
};
export const getPublicKeyFromPrivateKey = (pk: Buffer): Buffer => getPublicKey(pk);
const getMasterKeyFromSeed = (seed: Buffer) => {
const hmac = crypto.createHmac('sha512', ED25519_CURVE);
const digest = hmac.update(seed).digest();
const leftBytes = digest.slice(0, 32);
const rightBytes = digest.slice(32);
return {
key: leftBytes,
chainCode: rightBytes,
};
};
const getChildKey = (node: { key: Buffer; chainCode: Buffer }, index: number) => {
const indexBuffer = Buffer.allocUnsafe(4);
indexBuffer.writeUInt32BE(index, 0);
const data = Buffer.concat([Buffer.alloc(1, 0), node.key, indexBuffer]);
const digest = crypto.createHmac('sha512', node.chainCode).update(data).digest();
const leftBytes = digest.slice(0, 32);
const rightBytes = digest.slice(32);
return {
key: leftBytes,
chainCode: rightBytes,
};
};
export const getPrivateKeyFromPhraseAndPath = async (
phrase: string,
path: string,
): Promise<Buffer> => {
const masterSeed = await Mnemonic.mnemonicToSeed(phrase);
let node = getMasterKeyFromSeed(masterSeed);
for (const segment of parseKeyDerivationPath(path)) {
node = getChildKey(node, segment);
}
return getKeyPair(node.key).privateKey;
};
export interface SignedMessageWithPrivateKey {
readonly message: string;
readonly publicKey: Buffer;
readonly signature: Buffer;
}
export const signMessageWithPrivateKey = (
message: string,
privateKey: Buffer,
): SignedMessageWithPrivateKey => {
const msgBytes = digestMessage(message);
const publicKey = getPublicKey(privateKey);
const signature = signDetached(msgBytes, privateKey);
return {
message,
publicKey,
signature,
};
};
export const verifyMessageWithPublicKey = ({
message,
publicKey,
signature,
}: SignedMessageWithPrivateKey): boolean => {
const msgBytes = digestMessage(message);
if (publicKey.length !== NACL_SIGN_PUBLICKEY_LENGTH) {
throw new Error(
`Invalid publicKey, expected ${NACL_SIGN_PUBLICKEY_LENGTH.toString()}-byte publicKey`,
);
}
if (signature.length !== NACL_SIGN_SIGNATURE_LENGTH) {
throw new Error(
`Invalid signature length, expected ${NACL_SIGN_SIGNATURE_LENGTH.toString()}-byte signature`,
);
}
return verifyDetached(msgBytes, signature, publicKey);
};
export interface SignedMessage {
readonly message: string;
readonly publicKey: Buffer;
readonly signature: Buffer;
}
export const printSignedMessage = ({ message, signature, publicKey }: SignedMessage): string =>
[
signedMessageHeader,
messageHeader,
message,
publicKeyHeader,
publicKey.toString('hex'),
signatureHeader,
signature.toString('hex'),
signatureFooter,
]
.filter(Boolean)
.join('\n');
export const signAndPrintMessage = (message: string, privateKey: Buffer): string => {
const signedMessage = signMessageWithPrivateKey(message, privateKey);
return printSignedMessage(signedMessage);
};
export const signDataWithPrivateKey = (
tag: string,
chainID: Buffer,
data: Buffer,
privateKey: Buffer,
): Buffer => signDetached(hash(tagMessage(tag, chainID, data)), privateKey);
export const signData = signDataWithPrivateKey;
export const verifyData = (
tag: string,
chainID: Buffer,
data: Buffer,
signature: Buffer,
publicKey: Buffer,
): boolean => verifyDetached(hash(tagMessage(tag, chainID, data)), signature, publicKey);