-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
49 lines (41 loc) · 1.14 KB
/
script.js
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
const base32 = require('bs32');
const blake3 = require('blake3');
const Address = require('hsd/lib/primitives/address');
const rules = require('hsd/lib/covenants/rules');
const { Script, Opcode } = require('hsd/lib/script');
module.exports = {
createScript,
createAddress,
hashName
};
function createScript (nameHash, publicKey) {
if (typeof nameHash === 'string') {
nameHash = base32.decode(nameHash);
}
if (typeof publicKey === 'string') {
publicKey = base32.decode(publicKey);
}
const script = new Script([
Opcode.fromPush(publicKey),
Opcode.fromSymbol('checksig'),
Opcode.fromPush(nameHash),
Opcode.fromSymbol('drop')
]);
return script;
}
function createAddress (name, publicKey) {
const nameHash = hashName(name, publicKey);
const script = createScript(nameHash, publicKey);
const address = new Address().fromScript(script);
return address;
}
function hashName (name, publicKey) {
if (typeof name === 'string') {
name = name.split('.')[0];
name = Buffer.from(name);
}
if (typeof publicKey === 'string') {
publicKey = base32.decode(publicKey);
}
return blake3.hash(name, publicKey);
}