This repository has been archived by the owner on Dec 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (86 loc) · 2.16 KB
/
index.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
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
// const Buffer = require('safe-buffer').Buffer
const fctUtils = require('factomjs-util')
const bip44 = require('factombip44')
const MAX_ADDRESSES = 10
/**
* A wallet contains up to MAX addresses
*/
function Wallet () {
this.hdWallet = null
this.numberOfFctAddresses = 0
this.addresses = []
this.mnemonic = ''
}
/**
* Wallet with random Mnemonic
* @return {Wallet}
*/
function newRandomWallet () {
var w = new Wallet()
var mnemonic = bip44.randomMnemonic()
w.hdWallet = new bip44.FactomBIP44(mnemonic)
w.mnemonic = mnemonic
w.init()
return w
}
/**
* Wallet from mnemonic
* @param {string} mnemonic 12 words
* @return {Wallet}
*/
function newWalletFromMnemonic (mnemonic) {
var w = new Wallet()
w.hdWallet = new bip44.FactomBIP44(mnemonic)
w.mnemonic = mnemonic
w.init()
return w
}
/**
* Returns the seed
* @return {string} Seed
*/
Wallet.prototype.getSeed = function () {
return this.mnemonic
}
/**
* Builds the map of public addresses for signing
*/
Wallet.prototype.init = function () {
for (var i = 0; i < MAX_ADDRESSES; i++) {
this.addresses[this.getFactoidAddress(i).HumanReadable] = i
}
}
/**
* Returns the address at given index
* @param {int} index Choose the address to return
* @return {fctUtils.Address} Address
*/
Wallet.prototype.getFactoidAddress = function (index) {
if (index + 1 > this.numberOfFctAddresses) {
this.numberOfFctAddresses = index + 1
}
return getAddress(this.hdWallet.generateFactoidPrivateKey(0, 0, index))
}
/**
* Signs a fctUtils.Transaction if this wallet has the private keys
* @param {fctUtils.Transaction } transaction
*/
Wallet.prototype.sign = function (transaction) {
for (var i = 0; i < transaction.Inputs.length; i++) {
var index = -1
index = this.addresses[transaction.Inputs[i].HumanReadable]
if (index !== -1) {
transaction.sign(this.hdWallet.generateFactoidPrivateKey(0, 0, index))
}
}
}
function getAddress (priv) {
var pub = fctUtils.privateKeyToPublicKey(priv)
var add = fctUtils.publicFactoidKeyToHumanAddress(pub)
return new fctUtils.Address(add, 0, true)
}
module.exports = {
newRandomWallet,
newWalletFromMnemonic,
Wallet
}