Comparing version 0.0.10 to 0.0.11
@@ -19,2 +19,4 @@ const crypto = require('crypto'); | ||
const MAX_TIME_MUTEX = 30000; // 30 sec | ||
class Channel { | ||
@@ -102,6 +104,12 @@ constructor(walletId, myDeviceAddress, peerDeviceAddress, peerAddress, myAmount, peerAmount, age = 150, id = null, objRecovery) { | ||
if (this.mutualClose) return; | ||
let authors = objJoint.unit.authors; | ||
if (this.step !== 'close' && authors.findIndex(el => el.address === this.addresses[0]) !== -1 && | ||
let outputs = objJoint.unit.messages[0].payload.outputs; | ||
if (outputs.findIndex(el => el.address === this.channelAddress) !== -1 && | ||
this.step !== 'close' && | ||
authors.findIndex(el => el.address === this.addresses[0]) !== -1 && | ||
authors.findIndex(el => el.address === this.peerAddress) !== -1) { | ||
if (this.unlockPick) { | ||
this.unlockPick(); | ||
this.unlockPick = null; | ||
} | ||
if (this.needConfirmation) { | ||
@@ -114,3 +122,2 @@ this.waitingUnit = objJoint.unit.unit; | ||
} | ||
this.channelAddress = Channel.findChannelAddress(objJoint.unit.messages[0].payload.outputs, this.addresses[0], this.peerAddress); | ||
this.updateInDb().catch(console.error); | ||
@@ -135,2 +142,4 @@ } | ||
this.removeListeners(); | ||
} else if (objMessage.step === 'channelAddress') { | ||
this.channelAddress = objMessage.channelAddress; | ||
} else if (objMessage.step === '1Contract') { | ||
@@ -166,2 +175,9 @@ this.peerAddress = objMessage.myAddress; | ||
await libAddress.addIfNotExistRemoteAddresses(objMessage.arrAddressesRows, this.peerDeviceAddress); | ||
let objSharedAddress = await this.createSharedAddress(); | ||
this.channelAddress = objSharedAddress.shared_address; | ||
core.sendTechMessageToDevice(this.peerDeviceAddress, { | ||
step: 'channelAddress', | ||
channelAddress: this.channelAddress, | ||
id: this.id | ||
}); | ||
let objJoint = await this.createChannel(objMessage); | ||
@@ -227,4 +243,13 @@ if (this.needConfirmation) { | ||
if (this.step === 'reject' || from_address !== this.peerDeviceAddress) return; | ||
let outputs = objUnit.messages[0].payload.outputs; | ||
let channelOutput = outputs.find(output => output.address === this.channelAddress); | ||
let peer_shared_address_output; | ||
if (this.objPeerContract) { | ||
peer_shared_address_output = outputs.find(output => output.address === this.objPeerContract.shared_address); | ||
} | ||
if (channelOutput === undefined && peer_shared_address_output === undefined) return; | ||
if (this.step === 'await_createChannel' && signing_path === 'r') { | ||
let check = ChannelUtils.checkPaymentToSharedAddress(this, objUnit.messages[0].payload.outputs); | ||
let check = ChannelUtils.checkPaymentToSharedAddress(this, outputs); | ||
if (!check.status) { | ||
@@ -243,3 +268,3 @@ console.error(new Error('await_createChannel incorrect. code:' + check.code)); | ||
this.prevSharedAddress = this.objMyContract.shared_address; | ||
let check = ChannelUtils.checkTransferPayment(this, objUnit.messages[0].payload.outputs); | ||
let check = ChannelUtils.checkTransferPayment(this, outputs); | ||
if (!check.status) { | ||
@@ -277,3 +302,3 @@ console.error(new Error('waiting_transfer incorrect. code:' + check.code)); | ||
// This is A | ||
let check = ChannelUtils.checkTransferPayment(this, objUnit.messages[0].payload.outputs); | ||
let check = ChannelUtils.checkTransferPayment(this, outputs); | ||
if (!check.status) { | ||
@@ -297,3 +322,3 @@ console.error(new Error('waiting_transfer incorrect. code:' + check.code)); | ||
} else if (this.step === 'await_closing' && (signing_path === 'r.0.0' || signing_path === 'r.0.1' || signing_path === 'r')) { | ||
let check = ChannelUtils.checkClosingPayment(this, objUnit.messages[0].payload.outputs); | ||
let check = ChannelUtils.checkClosingPayment(this, outputs); | ||
if (!check.status) { | ||
@@ -307,3 +332,3 @@ console.error(new Error('await_closing incorrect. code:' + check.code)); | ||
} else if (this.step === 'await_closing' && (signing_path === 'r.1.0' || signing_path === 'r.2.0')) { | ||
let check = ChannelUtils.checkClosingPayment(this, objUnit.messages[0].payload.outputs); | ||
let check = ChannelUtils.checkClosingPayment(this, outputs); | ||
if (!check.status) { | ||
@@ -321,3 +346,3 @@ console.error(new Error('await_closing incorrect. code:' + check.code)); | ||
} else if (this.step === 'waiting_transfer') { | ||
let check = ChannelUtils.checkTransferPayment(this, objUnit.messages[0].payload.outputs); | ||
let check = ChannelUtils.checkTransferPayment(this, outputs); | ||
if (!check.status) { | ||
@@ -408,4 +433,13 @@ console.error(new Error('waiting_transfer incorrect. code:' + check.code)); | ||
if (this.step === 'null') { | ||
let rows = await libToEs6.dbQuery("SELECT unit FROM unit_authors WHERE address = ? AND definition_chash IS NOT NULL LIMIT 0,1", [this.addresses[0]]); | ||
let needConfirmation = !rows.length; | ||
let MCIAndResponse = await Channel.getMCIAndResponse(); | ||
let rows = await libToEs6.dbQuery("SELECT 1 FROM units CROSS JOIN unit_authors USING(unit) \n\ | ||
WHERE (main_chain_index>? OR main_chain_index IS NULL) AND address IN(?) AND definition_chash IS NOT NULL \n\ | ||
UNION \n\ | ||
SELECT 1 FROM units JOIN address_definition_changes USING(unit) \n\ | ||
WHERE (main_chain_index>? OR main_chain_index IS NULL) AND address IN(?) \n\ | ||
UNION \n\ | ||
SELECT 1 FROM units CROSS JOIN unit_authors USING(unit) \n\ | ||
WHERE (main_chain_index>? OR main_chain_index IS NULL) AND address IN(?) AND sequence!='good'", | ||
[MCIAndResponse.mci, [this.addresses[0]], MCIAndResponse.mci, [this.addresses[0]], MCIAndResponse.mci, [this.addresses[0]]]); | ||
let needConfirmation = !!rows.length; | ||
this.needConfirmation = needConfirmation; | ||
@@ -510,3 +544,3 @@ core.sendTechMessageToDevice(this.peerDeviceAddress, { | ||
ifError: (err) => { | ||
return reject(err); | ||
return reject(new Error(err)); | ||
}, | ||
@@ -522,3 +556,3 @@ ifOk: async (shared_address) => { | ||
async sendInputsAndAddresses() { | ||
let objPick = await Channel.pickDivisibleCoinsForAmount(db, {asset: null}, this.addresses, this.myAmount + 450); | ||
let objPick = await Channel.pickDivisibleCoinsForAmount(db, {asset: null}, this.addresses, this.myAmount + 450, true); | ||
this.total_input = objPick.total_input; | ||
@@ -535,3 +569,7 @@ let myPayingAddresses = await libAddress.getAddressesOfUnits(objPick.arrInputs.map(input => input.unit)); | ||
}); | ||
this.unlockPick = objPick.unlock; | ||
this.step = 'await_createChannel'; | ||
setTimeout(() => { | ||
if (this.unlockPick) this.unlockPick(); | ||
}, MAX_TIME_MUTEX); | ||
return true; | ||
@@ -593,3 +631,3 @@ } | ||
ifError: (err) => { | ||
return reject(err); | ||
return reject(new Error(err)); | ||
}, | ||
@@ -611,7 +649,5 @@ ifOk: async (shared_address) => { | ||
async createChannel(objMessage) { | ||
let objSharedAddress = await this.createSharedAddress(); | ||
this.channelAddress = objSharedAddress.shared_address; | ||
let objPick = await Channel.pickDivisibleCoinsForAmount(db, {asset: null}, this.addresses, this.myAmount + 450); | ||
let objPick = await Channel.pickDivisibleCoinsForAmount(db, {asset: null}, this.addresses, this.myAmount + 450, true); | ||
if (!objPick) { | ||
return Promise.reject('Insufficient funds'); | ||
return Promise.reject(new Error('Insufficient funds')); | ||
} | ||
@@ -625,3 +661,3 @@ let myPayingAddresses = await libAddress.getAddressesOfUnits(objPick.arrInputs.map(input => input.unit)); | ||
opts.outputs = [{ | ||
address: objSharedAddress.shared_address, | ||
address: this.channelAddress, | ||
amount: this.myAmount + this.peerAmount | ||
@@ -639,9 +675,12 @@ }, { | ||
ifError: (err) => { | ||
return reject(err); | ||
setTimeout(objPick.unlock, 150); | ||
return reject(new Error(err)); | ||
}, | ||
ifNotEnoughFunds: (err) => { | ||
return reject(err); | ||
setTimeout(objPick.unlock, 150); | ||
return reject(new Error(err)); | ||
}, | ||
ifOk: (objJoint) => { | ||
network.broadcastJoint(objJoint); | ||
setTimeout(objPick.unlock, 150); | ||
return resolve(objJoint); | ||
@@ -699,6 +738,6 @@ } | ||
ifError: (err) => { | ||
return reject(err); | ||
return reject(new Error(err)); | ||
}, | ||
ifNotEnoughFunds: (err) => { | ||
return reject(err); | ||
return reject(new Error(err)); | ||
}, | ||
@@ -743,6 +782,6 @@ ifOk: (objJoint, assocPrivatePayloads, unlock_callback) => { | ||
ifError: (err) => { | ||
return reject(err); | ||
return reject(new Error(err)); | ||
}, | ||
ifNotEnoughFunds: (err) => { | ||
return reject(err); | ||
return reject(new Error(err)); | ||
}, | ||
@@ -764,3 +803,3 @@ ifOk: async (objJoint) => { | ||
if (!objInputs || !objInputs.arrInputs.length) { | ||
return reject('Insufficient funds'); | ||
return reject(new Error('Insufficient funds')); | ||
} | ||
@@ -780,3 +819,3 @@ core.sendTechMessageToDevice(this.peerDeviceAddress, { | ||
return resolve(objJoint); | ||
}).catch(reject); | ||
}).catch((e) => reject(new Error(e))); | ||
}); | ||
@@ -797,6 +836,6 @@ }); | ||
ifError: (err) => { | ||
return reject(err); | ||
return reject(new Error(err)); | ||
}, | ||
ifNotEnoughFunds: (err) => { | ||
return reject(err); | ||
return reject(new Error(err)); | ||
}, | ||
@@ -807,3 +846,3 @@ ifOk: async (objJoint) => { | ||
this.step = 'waiting_mci'; | ||
this.waitingMCI(this.waitingUnit).catch(reject); | ||
this.waitingMCI(this.waitingUnit).catch((e) => reject(new Error(e))); | ||
await this.updateInDb(); | ||
@@ -818,3 +857,3 @@ return resolve(objJoint); | ||
if (!objInputs || !objInputs.arrInputs.length) { | ||
return reject('Insufficient funds'); | ||
return reject(new Error('Insufficient funds')); | ||
} | ||
@@ -833,6 +872,6 @@ this.closingPayment(objInputs, [this.channelAddress], | ||
this.step = 'waiting_mci'; | ||
this.waitingMCI(this.waitingUnit).catch(reject); | ||
this.waitingMCI(this.waitingUnit).catch(e => reject(new Error(e))); | ||
await this.updateInDb(); | ||
return resolve(objJoint); | ||
}).catch(reject); | ||
}).catch(e => reject(new Error(e))); | ||
}); | ||
@@ -864,3 +903,3 @@ } | ||
if (!objInputs || !objInputs.arrInputs.length) { | ||
return reject('Insufficient funds'); | ||
return reject(new Error('Insufficient funds')); | ||
} | ||
@@ -884,3 +923,3 @@ this.closingPayment(objInputs, [this.objMyContract.shared_address], | ||
this.removeListeners(); | ||
}).catch(reject); | ||
}).catch(e => reject(new Error(e))); | ||
}); | ||
@@ -909,3 +948,3 @@ }); | ||
return true; | ||
}).catch(Promise.reject); | ||
}).catch(e => Promise.reject(new Error(e))); | ||
} | ||
@@ -939,10 +978,14 @@ | ||
static pickDivisibleCoinsForAmount(db, asset, addresses, amount) { | ||
static pickDivisibleCoinsForAmount(db, asset, addresses, amount, lock) { | ||
return new Promise((resolve => { | ||
libTransactions.pickDivisibleCoinsForAmount(asset.asset, addresses, amount).then(objPick => { | ||
return resolve({ | ||
arrInputs: objPick.arrInputs, | ||
total_input: objPick.total_input | ||
}); | ||
}).catch(() => {return resolve(null)}); | ||
mutex.lock(["biot_pickDivisibleCoinsForAmount"], (unlock) => { | ||
libTransactions.pickDivisibleCoinsForAmount(asset.asset, addresses, amount).then(objPick => { | ||
if (!lock) unlock(); | ||
return resolve({ | ||
arrInputs: objPick.arrInputs, | ||
total_input: objPick.total_input, | ||
unlock: lock ? unlock : undefined | ||
}); | ||
}).catch(() => {return resolve(null)}); | ||
}); | ||
})); | ||
@@ -979,12 +1022,2 @@ } | ||
static findChannelAddress(outputs, myAddress, peerAddress) { | ||
let _obj = outputs.find(obj => { | ||
return obj.address !== myAddress && obj.address !== peerAddress; | ||
}); | ||
if (!_obj) { | ||
throw 'Address not found'; | ||
} | ||
return _obj.address; | ||
} | ||
static getMCIAndResponse() { | ||
@@ -991,0 +1024,0 @@ return new Promise(resolve => { |
@@ -134,3 +134,3 @@ const objectHash = require('byteballcore/object_hash'); | ||
let channelOutput = outputs.find(output => { | ||
let peer_shared_address_output = outputs.find(output => { | ||
return output.address === channel.objPeerContract.shared_address; | ||
@@ -143,7 +143,7 @@ }); | ||
if (!channelOutput) { | ||
if (!peer_shared_address_output) { | ||
return {status: false, code: 1}; | ||
} | ||
if (channelOutput.amount !== (channel.myAmount + channel.peerAmount)) { | ||
if (peer_shared_address_output.amount !== (channel.myAmount + channel.peerAmount)) { | ||
return {status: false, code: 2}; | ||
@@ -150,0 +150,0 @@ } |
{ | ||
"name": "biot-core", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "core.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102813
2686