-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
188 lines (174 loc) · 5.87 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var contractsModule = require('./lib/contractManager');
var pipes = require('./lib/pipes/pipes');
var utils = require('./lib/utils/utils');
var edbModule = require('eris-db');
var DevPipe = require('./lib/pipes/dev_pipe');
var outputFormatters = require('./lib/output_formatters');
var edb;
/**
* Create a new solidity contracts object from the given pipe.
*
* @param pipe
* @returns {*|contract}
*/
exports.newContractManager = function(pipe){
return contractsModule.newContractManager(pipe);
};
/**
* Create a new solidity contracts object with a DevPipe from the given
* rpc-URL and private key.
*
* @param {string} erisdbURL - The url to the eris-db server. Usually (http://localhost:1337/rpc)
* @param {string} accounts - Used to pass in a list of accounts. NOTE: This is for DEV ONLY. The keys are not protected.
* @param {function(error, data)} [callback] - Callback is only needed if using a websocket client. If callback is not provided,
* the object will be returned, otherwise it is passed as the data param in the (normal error-first) callback.
*/
exports.newContractManagerDev = function(erisdbURL, accounts, callback){
edb = edbModule.createInstance(erisdbURL);
var pipe = new DevPipe(edb, accounts);
var manager = contractsModule.newContractManager(pipe);
if(callback){
edb.start(function(error){
if(error){
callback(error);
} else {
callback(null, manager);
}
})
} else {
return manager;
}
};
/**
* Create a new solidity contracts object from the given pipe.
*
* @deprecated
* @param pipe
* @returns {*|contract}
*/
exports.contracts = function(pipe){
contractsModule.init(pipe);
return contractsModule.contract;
};
/**
* Create a new solidity contracts object with a DevPipe from the given
* rpc-URL and private key.
*
* @param {string} erisdbURL - The url to the eris-db server. Usually (http://localhost:1337/rpc)
* @param {string} privateKey - The 64 byte private key used to make transactions with eris-db/tendermint.
* NOTE: As always, as in every doc we ever write - don't pass private keys around if they actually protect
* something. Only do it in testing where key is basically just a worthless bunch of bytes.
* @param {function(error, data)} [callback] - Callback is only needed if using a websocket client. It will fire when
* the websockets are ready to go. If callback is not provided, the object will be returned,
* otherwise it is passed as the data param in the (normal error-first) callback.
* @deprecated
*/
exports.contractsDev = function(erisdbURL, privateKey, callback){
edb = edbModule.createInstance(erisdbURL);
var pipe = new DevPipe(edb, privateKey);
contractsModule.init(pipe);
var contract = contractsModule.contract;
if(callback){
edb.start(function(error){
if(error){
callback(error);
} else {
callback(null, contract);
}
})
} else {
return contract;
}
};
/**
* Get the eris-db instance.
*
* TODO This might not be set if the user provides their own pipe. Need to remove this asap.
* @returns {*}
* @deprecated
*/
exports.getErisDb = function(){
console.log("DEPRECATED: Access the eris-db instance through the contract manager instead.");
return edb;
};
exports.pipes = pipes;
/**
* Utils has methods for working with strings.
*
* @type {{}}
*/
exports.utils = {};
exports.utils.hexToAscii = utils.hexToAscii;
exports.utils.asciiToHex = utils.asciiToHex;
exports.utils.padLeft = utils.padLeft;
exports.utils.padRight = utils.padRight;
exports.utils.htoa = utils.hexToAscii;
exports.utils.atoh = utils.asciiToHex;
/**
* Output formatters are used to transform the output of contract transactions and calls.
* These objects takes all named params and put them as fields in the object, and also puts
* the raw output into an array.
*
* If the output of a solidity function is (someInt, someBytes), the output will be an
* array by default, for example: [BigNumber(5), "abba"]. What you get after formatting
* with 'outputFormatters.json' is:
*
* var obj = {
* params: {
* someInt: BigNumber(5),
* someBytes: "abba"
* },
* raw: [BigNumber(5), "abba"]
* }
*
* You may also use 'jsonStrings', which would display all numbers as decimal strings instead - in params - but
* leave the values intact in 'raw'.
*
* var stringObj = {
* params: {
* someInt: "5",
* someBytes: "abba"
* },
* raw: [BigNumber(5), "abba"]
* }
*
* Finally, there's 'paramsToJson' that will do the 'jsonStrings' conversion, then JSON.stringify the 'params'
* object and return it alone. This is good when passing the values on to a http response.
*
* What 'paramsToJson' will return is the result of: JSON.stringify(stringObj.params)
*
* NOTE: 'paramsToJson' will only work if all output params are named. Otherwise they will not be included in 'params'
* and therefore not in the JSON-formatted output. When working with unnamed params, you should probably just
* JSON.stringify the output array.
*
* @type {{}}
*/
exports.outputFormatters = outputFormatters;
exports.outputFormatters.paramsToJson = outputFormatters.valuesToJsonString(outputFormatters.jsonStrings);
/**
* @deprecated
*/
exports.solidityContracts = function(pipe){
contractsModule.init(pipe);
return contractsModule.contract;
};
/**
* @deprecated
*/
exports.solidityContractsDev = function(erisdbURL, privateKey, callback){
var edb = edbModule.createInstance(erisdbURL);
var pipe = new DevPipe(edb, privateKey);
contractsModule.init(pipe);
var contract = contractsModule.contract;
if(callback){
edb.start(function(error){
if(error){
callback(error);
} else {
callback(null, contract);
}
})
} else {
return contract;
}
};