-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaleo.js
67 lines (51 loc) · 1.64 KB
/
aleo.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// generate Aleo account(private key & address) from mnemonic
// compatible with Leo Wallet extension
import bip39 from 'bip39'
import { Account } from '@aleohq/sdk'
import createHmac from 'create-hmac'
const MNEMONIC = '' //write your mnemonic here
let hexSeed = bip39.mnemonicToSeedSync(MNEMONIC).toString('hex')
let index = 0
let path = `m/44'/0'/${index}'/0'`
let { seed, _chainCode } = derivePath(path, hexSeed)
let account = new Account({seed: seed})
let privateKey = account.privateKey()
console.log('Aleo private key: ', privateKey.to_string())
let address = account.address()
console.log('Aleo address: ', address.to_string())
function getMasterSeed(seed) {
const hmac = createHmac('sha512', 'bls12_377 seed')
const I = hmac.update(Buffer.from(seed, 'hex')).digest()
const IL = I.slice(0, 32)
const IR = I.slice(32)
return {
seed: IL,
chainCode: IR,
}
}
function CKDPriv({ seed, chainCode }, index) {
const indexBuffer = Buffer.allocUnsafe(4)
indexBuffer.writeUInt32BE(index, 0)
const data = Buffer.concat([Buffer.alloc(1, 0), seed, indexBuffer])
const I = createHmac('sha512', chainCode)
.update(data)
.digest()
const IL = I.slice(0, 32)
const IR = I.slice(32)
return {
seed: IL,
chainCode: IR,
}
}
function replaceDerive(val) {
return val.replace("'", '')
}
function derivePath(path, rootSeed) {
const { seed, chainCode } = getMasterSeed(rootSeed)
const segments = path
.split('/')
.slice(1)
.map(replaceDerive)
.map(el => parseInt(el, 10))
return segments.reduce((parentSeeds, segment) => CKDPriv(parentSeeds, segment + 0x80000000), { seed, chainCode })
}