forked from byteball/headless-obyte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprosaic_contract_api.js
135 lines (121 loc) · 4.75 KB
/
prosaic_contract_api.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*jslint node: true */
"use strict";
var prosaic_contract = require('ocore/prosaic_contract.js');
var eventBus = require('ocore/event_bus.js');
var device = require('ocore/device.js');
var objectHash = require('ocore/object_hash.js');
var conf = require('ocore/conf.js');
var db = require('ocore/db.js');
var ecdsaSig = require('ocore/signature.js');
var walletDefinedByAddresses = require('ocore/wallet_defined_by_addresses.js');
var walletDefinedByKeys = require('ocore/wallet_defined_by_keys.js');
var headlessWallet = require('./start.js');
var contractsListened = [];
var wallet_id;
function offer(title, text, my_address, peer_address, peer_device_address, ttl, cosigners, signWithLocalPrivateKey, callbacks) {
var creation_date = new Date().toISOString().slice(0, 19).replace('T', ' ');
var hash = prosaic_contract.getHash({title:title, text:text, creation_date:creation_date});
prosaic_contract.createAndSend(hash, peer_address, peer_device_address, my_address, creation_date, ttl, title, text, cosigners, function(objContract){
listenForPendingContracts(signWithLocalPrivateKey, callbacks);
if (callbacks.onOfferCreated)
callbacks.onOfferCreated(objContract);
});
}
function listenForPendingContracts(signWithLocalPrivateKey, callbacks) {
if (!callbacks)
callbacks = {};
if (!callbacks.onError)
callbacks.onError = console.error;
var start_listening = function(contract) {
var sendUnit = function(accepted){
if (callbacks.onResponseReceived)
callbacks.onResponseReceived(accepted);
if (!accepted) {
return;
}
var arrDefinition =
['and', [
['address', contract.my_address],
['address', contract.peer_address]
]];
var assocSignersByPath = {
'r.0': {
address: contract.my_address,
member_signing_path: 'r',
device_address: device.getMyDeviceAddress()
},
'r.1': {
address: contract.peer_address,
member_signing_path: 'r',
device_address: contract.peer_device_address
}
};
walletDefinedByAddresses.createNewSharedAddress(arrDefinition, assocSignersByPath, {
ifError: function(err){
callbacks.onError(err);
},
ifOk: function(shared_address){
composeAndSend(shared_address);
}
});
// create shared address and deposit some bytes to cover fees
function composeAndSend(shared_address){
prosaic_contract.setField(contract.hash, "shared_address", shared_address);
device.sendMessageToDevice(contract.peer_device_address, "prosaic_contract_update", {hash: contract.hash, field: "shared_address", value: shared_address});
contract.cosigners.forEach(function(cosigner){
if (cosigner != device.getMyDeviceAddress())
prosaic_contract.share(contract.hash, cosigner);
});
var opts = {
asset: "base",
to_address: shared_address,
amount: prosaic_contract.CHARGE_AMOUNT,
arrSigningDeviceAddresses: contract.cosigners
};
headlessWallet.issueChangeAddressAndSendMultiPayment(opts, function(err){
if (err){
callbacks.onError(err);
return;
}
// post a unit with contract text hash and send it for signing to correspondent
var value = {"contract_text_hash": contract.hash};
var objMessage = {
app: "data",
payload_location: "inline",
payload_hash: objectHash.getBase64Hash(value),
payload: value
};
headlessWallet.issueChangeAddressAndSendMultiPayment({
arrSigningDeviceAddresses: contract.cosigners.length ? contract.cosigners.concat([contract.peer_device_address]) : [],
shared_address: shared_address,
messages: [objMessage]
}, function(err, unit) { // can take long if multisig
//indexScope.setOngoingProcess(gettext('proposing a contract'), false);
if (err) {
callbacks.onError(err);
return;
}
prosaic_contract.setField(contract.hash, "unit", unit);
device.sendMessageToDevice(contract.peer_device_address, "prosaic_contract_update", {hash: contract.hash, field: "unit", value: unit});
var explorer = (process.env.testnet ? 'https://testnetexplorer.obyte.org/#' : 'https://explorer.obyte.org/#');
var text = "unit with contract hash for \""+ contract.title +"\" was posted into DAG " + explorer + unit;
device.sendMessageToDevice(contract.peer_device_address, "text", text);
if (callbacks.onSigned)
callbacks.onSigned(contract);
});
});
}
};
eventBus.once("prosaic_contract_response_received" + contract.hash, sendUnit);
}
prosaic_contract.getAllByStatus("pending", function(contracts){
contracts.forEach(function(contract){
if (contractsListened.indexOf(contract.hash) === -1) {
start_listening(contract);
contractsListened.push(contract.hash);
}
});
});
}
exports.offer = offer;
exports.listenForPendingContracts = listenForPendingContracts;