-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e883b7
commit a090b18
Showing
8 changed files
with
968 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Order - 4 | ||
|
||
class AccountHelper { | ||
/* | ||
Variables: | ||
theWrapper | ||
*/ | ||
|
||
constructor(wrapper) { | ||
this.theWrapper = wrapper; | ||
} | ||
|
||
getFriendlyAddress(obj) { | ||
if (obj instanceof Nimiq.Wallet) { | ||
return obj.address.toUserFriendlyAddress(); | ||
} else if (obj instanceof Nimiq.Address) { | ||
return obj.toUserFriendlyAddress(); | ||
} else { | ||
this.theWrapper.callbacks.error("AccountHelper:getFriendlyAddress", "Parameter type incompatible with function."); | ||
} | ||
} | ||
|
||
getBalance(obj, callback) { | ||
let lookupAddr = null; | ||
|
||
if (obj instanceof Nimiq.Wallet) { | ||
lookupAddr = obj.address; | ||
} else if (obj instanceof Nimiq.Address) { | ||
lookupAddr = obj; | ||
} else if (typeof obj == "string") { | ||
lookupAddr = Nimiq.Address.fromUserFriendlyAddress(obj); | ||
} | ||
|
||
if (lookupAddr) { | ||
if (this.theWrapper.wrappedNode.accounts) { | ||
this.theWrapper.wrappedNode.accounts.get(lookupAddr).then((account) => { | ||
callback(account.balance); | ||
}); | ||
} else { | ||
this.theWrapper.wrappedNode.consensus.getAccount(lookupAddr).then((account) => { | ||
callback(account ? account.balance : 0); | ||
}); | ||
} | ||
} else { | ||
this.theWrapper.callbacks.error("AccountHelper:getBalance", "Parameter type incompatible with function."); | ||
} | ||
} | ||
|
||
createWallet() { | ||
return Nimiq.Wallet.generate(); | ||
} | ||
|
||
importWalletFromHexKey(key) { | ||
let privateKey = Nimiq.PrivateKey.unserialize(Nimiq.BufferUtils.fromHex(key.replace("0x", ""))); | ||
let keypair = Nimiq.KeyPair.derive(privateKey); | ||
return new Nimiq.Wallet(keypair); | ||
} | ||
|
||
importWalletFromMnemonic(words) { | ||
let mnemonic = null; | ||
|
||
if (typeof words == "string") { | ||
mnemonic = words.split(" "); | ||
} else { | ||
mnemonic = words; | ||
} | ||
|
||
if (mnemonic == null) { | ||
this.theWrapper.callbacks.error("AccountHelper:importWalletFromMnemonic", "Parameter type incompatible with function."); | ||
} else { | ||
let entropy = null; | ||
if (Nimiq.MnemonicUtils.getMnemonicType(mnemonic) == Nimiq.MnemonicUtils.MnemonicType.LEGACY) { | ||
entropy = Nimiq.MnemonicUtils.legacyMnemonicToEntropy(mnemonic); | ||
} else { | ||
entropy = Nimiq.MnemonicUtils.mnemonicToEntropy(mnemonic); | ||
} | ||
|
||
let keypair = Nimiq.KeyPair.derive(entropy); | ||
return new Nimiq.Wallet(keypair); | ||
} | ||
} | ||
|
||
importWalletFromBuffer(buffer) { | ||
return Nimiq.Wallet.loadPlain(buffer); | ||
} | ||
|
||
importWalletFromEncryptedBuffer(buffer, password, callback, errorCallback = null) { | ||
Nimiq.Wallet.loadEncrypted(buffer, password).then((result) => { | ||
callback(result); | ||
}).catch((err) => { | ||
if (errorCallback) { | ||
errorCallback(err); | ||
} else { | ||
this.theWrapper.callbacks.error("AccountHelper:importWalletFromEncryptedBuffer", err); | ||
} | ||
}); | ||
} | ||
|
||
exportWalletToHexKey(wallet) { | ||
return Nimiq.BufferUtils.toHex(wallet._keyPair.privateKey.serialize()); | ||
} | ||
|
||
// Will be changed to default to false once legacy wallets are actually legacy in the mainnet. | ||
// Currently they're only legacy on the testnet. | ||
exportWalletToMnemonic(wallet, legacy = true) { | ||
let privateKey = wallet._keyPair.privateKey; | ||
|
||
if (legacy) { | ||
return Nimiq.MnemonicUtils.entropyToLegacyMnemonic(privateKey.serialize()); | ||
} else { | ||
return Nimiq.MnemonicUtils.entropyToMnemonic(privateKey.serialize()); | ||
} | ||
} | ||
|
||
exportWalletToBuffer(wallet) { | ||
return wallet.exportPlain(); | ||
} | ||
|
||
exportWalletToEncryptedBuffer(wallet, password, callback) { | ||
wallet.exportEncrypted(password).then((result) => { | ||
callback(result); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Order - 2 | ||
|
||
class KeyguardHelper { | ||
/* | ||
Variables: | ||
theWrapper | ||
wrappedClient | ||
appName | ||
*/ | ||
|
||
constructor(wrapper) { | ||
this.theWrapper = wrapper; | ||
this.wrappedClient = null; | ||
this.appName = "Nimiq Application"; | ||
} | ||
|
||
initKeyguard(keyguardURL, appName) { | ||
this.wrappedClient = new AccountsClient(keyguardURL); | ||
this.appName = appName; | ||
} | ||
|
||
requestAddress(callback, options = { }) { | ||
if (WRAPPING_NODE) { | ||
this.theWrapper.callbacks.error("KeyguardHelper:requestAddress", "Keyguard cannot be used in NodeJS!"); | ||
return; | ||
} | ||
|
||
if (!this.wrappedClient) { | ||
this.theWrapper.callbacks.error("KeyguardHelper:requestAddress", "Keyguard not yet initialized!"); | ||
return; | ||
} | ||
|
||
let obj = { | ||
appName : this.appName | ||
}; | ||
|
||
if (options.appName) obj.appName = options.appName; | ||
|
||
this.wrappedClient.chooseAddress(obj).then((addr) => { | ||
callback(addr); | ||
}).catch((err) => { | ||
if (options.onError) { | ||
options.onError(err); | ||
} else { | ||
this.theWrapper.callbacks.error("KeyguardHelper:requestAddress", err); | ||
} | ||
}); | ||
} | ||
|
||
requestSignature(callback, options = { }) { | ||
if (WRAPPING_NODE) { | ||
this.theWrapper.callbacks.error("KeyguardHelper:requestSignature", "Keyguard cannot be used in NodeJS!"); | ||
return; | ||
} | ||
|
||
if (!this.wrappedClient) { | ||
this.theWrapper.callbacks.error("KeyguardHelper:requestSignature", "Keyguard not yet initialized!"); | ||
return; | ||
} | ||
|
||
let obj = { | ||
appName : this.appName, | ||
message : "Please sign this!" | ||
}; | ||
|
||
if (options.appName) obj.appName = options.appName; | ||
if (options.address) obj.signer = options.address; | ||
if (options.data) { | ||
if (typeof options.data == "string") { | ||
obj.message = options.data; | ||
} else if (options.data instanceof Uint8Array) { | ||
obj.message = options.data; | ||
} else { | ||
obj.message = JSON.stringify(options.data, null, 4); | ||
} | ||
} | ||
|
||
this.wrappedClient.signMessage(obj).then((signed) => { | ||
callback(signed); | ||
}).catch((err) => { | ||
if (options.onError) { | ||
options.onError(err); | ||
} else { | ||
this.theWrapper.callbacks.error("KeyguardHelper:requestSignature", err); | ||
} | ||
}); | ||
} | ||
|
||
requestTransaction(callback, options = { }) { | ||
if (WRAPPING_NODE) { | ||
this.theWrapper.callbacks.error("KeyguardHelper:requestTransaction", "Keyguard cannot be used in NodeJS!"); | ||
return; | ||
} | ||
|
||
if (!this.wrappedClient) { | ||
this.theWrapper.callbacks.error("KeyguardHelper:requestTransaction", "Keyguard not yet initialized!"); | ||
return; | ||
} | ||
|
||
let obj = { | ||
appName : this.appName, | ||
recipient : "NQ07 0000 0000 0000 0000 0000 0000 0000 0000", | ||
value : 0 | ||
}; | ||
|
||
if (options.appName) obj.appName = options.appName; | ||
if (options.logoURL) obj.shopLogoUrl = options.logoURL; | ||
if (options.address) obj.recipient = options.address; | ||
if (options.addrType) obj.recipientType = options.addrType; | ||
if (options.amount) obj.value = options.amount; | ||
if (options.fee) obj.fee = options.fee; | ||
if (options.flags) obj.flags = options.flags; | ||
if (options.expiration) obj.validityDuration = options.expiration; | ||
if (options.data) { | ||
if (typeof options.data == "string") { | ||
if (options.data.trim().length > 0) { | ||
obj.extraData = new Uint8Array(options.data.length); | ||
for (let i = 0; i < options.data.length; ++i) { | ||
obj.extraData[i] = options.data.charCodeAt(i); | ||
} | ||
} | ||
} else { | ||
obj.extraData = options.data; | ||
} | ||
} | ||
|
||
this.wrappedClient.checkout(obj).then((result) => { | ||
callback(result); | ||
}).catch((err) => { | ||
if (options.onError) { | ||
options.onError(err); | ||
} else { | ||
this.theWrapper.callbacks.error("KeyguardHelper:requestTransaction", err); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Order - 3 | ||
|
||
class MinerHelper { | ||
/* | ||
Variables: | ||
theWrapper | ||
wrappedMiner | ||
minerOptions | ||
*/ | ||
|
||
constructor(wrapper) { | ||
this.theWrapper = wrapper; | ||
this.wrappedMiner = null; | ||
} | ||
|
||
initMiner(options = { }) { | ||
this.minerOptions = { | ||
addr : Nimiq.Address.fromUserFriendlyAddress("NQ07 0000 0000 0000 0000 0000 0000 0000 0000"), | ||
host : "us.nimpool.io", | ||
port : 8444 | ||
}; | ||
|
||
if (options.poolHost) this.minerOptions.host = options.poolHost; | ||
if (options.poolPort) this.minerOptions.port = options.poolPort; | ||
if (options.address) { | ||
if (obj instanceof Nimiq.Wallet) { | ||
this.minerOptions.addr = obj.address; | ||
} else if (obj instanceof Nimiq.Address) { | ||
this.minerOptions.addr = obj; | ||
} else if (typeof obj == "string") { | ||
this.minerOptions.addr = Nimiq.Address.fromUserFriendlyAddress(obj); | ||
} | ||
} | ||
|
||
let miner = new Nimiq.SmartPoolMiner(this.theWrapper.wrappedNode.blockchain, this.theWrapper.wrappedNode.accounts, this.theWrapper.wrappedNode.mempool, this.theWrapper.wrappedNode.network.time, this.minerOptions.addr, Nimiq.BasePoolMiner.generateDeviceId(this.theWrapper.wrappedNode.network.config)); | ||
this.theWrapper.wrappedNode.miner = miner; | ||
this.wrappedMiner = miner; | ||
|
||
this.wrappedMiner.on('start', () => this.theWrapper.callbacks.minerChanged('started')); | ||
this.wrappedMiner.on('stop', () => this.theWrapper.callbacks.minerChanged('stopped')); | ||
this.wrappedMiner.on('connection-state', state => { | ||
if (state == Nimiq.BasePoolMiner.ConnectionState.CONNECTED) { | ||
this.theWrapper.callbacks.connectionState("connected"); | ||
} else if (state == Nimiq.BasePoolMiner.ConnectionState.CONNECTING) { | ||
this.theWrapper.callbacks.connectionState("connecting"); | ||
} else if (state == Nimiq.BasePoolMiner.ConnectionState.CLOSED) { | ||
this.theWrapper.callbacks.connectionState("disconnected"); | ||
} else { | ||
this.theWrapper.callbacks.error("MinerHelper:initMiner", "Unknown connection state occurred!"); | ||
} | ||
}); | ||
|
||
this.wrappedMiner.connect(this.minerOptions.host, this.minerOptions.port); | ||
} | ||
|
||
startMining() { | ||
if (!this.wrappedMiner) { | ||
this.theWrapper.callbacks.error("MinerHelper:startMining", "Miner not yet initialized!"); | ||
return; | ||
} | ||
|
||
this.wrappedMiner.startWork(); | ||
} | ||
|
||
stopMining() { | ||
if (!this.wrappedMiner) { | ||
this.theWrapper.callbacks.error("MinerHelper:stopMining", "Miner not yet initialized!"); | ||
return; | ||
} | ||
|
||
this.wrappedMiner.stopWork(); | ||
} | ||
|
||
estimateRewardPerHour() { | ||
if (!this.wrappedMiner) { | ||
this.theWrapper.callbacks.error("MinerHelper:estimateRewardPerHour", "Miner not yet initialized!"); | ||
return; | ||
} | ||
|
||
let myHash = this.wrappedMiner.hashrate; | ||
let goHash = this.theWrapper.globalHashrate; | ||
let reward = Nimiq.Policy.lunasToCoins(Nimiq.Policy.blockRewardAt(this.theWrapper.blockHeight)); | ||
let perBlock = (myHash / goHash) * reward; | ||
|
||
return perBlock * (60 / 1); //1 block per minute, 60 minutes in an hour. | ||
} | ||
|
||
get hashrate() { | ||
return this.wrappedMiner.hashrate; | ||
} | ||
|
||
get poolBalance() { | ||
return this.wrappedMiner.balance; | ||
} | ||
|
||
get payoutBalance() { | ||
return this.wrappedMiner.confirmedBalance; | ||
} | ||
|
||
get threads() { | ||
return this.wrappedMiner.threads; | ||
} | ||
|
||
set threads(t) { | ||
this.wrappedMiner.threads = t; | ||
} | ||
} |
Oops, something went wrong.