Skip to content

Commit fc73e30

Browse files
authored
TN-2.2.0 (#4)
* Updating for TN-2.2.0
1 parent a3ad7ba commit fc73e30

File tree

7 files changed

+154
-90
lines changed

7 files changed

+154
-90
lines changed

config/config_production.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"routes": {
55
"delegateList": "/delegates",
66
"transactionSend": "/transactions",
7-
"transactionStatus": "/receipts/",
7+
"transactionStatus": "/transactions/",
88
"accountStatus": "/accounts/",
99
"compileSolidity": "/compile"
1010
}

config/config_stage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"routes": {
55
"delegateList": "/delegates",
66
"transactionSend": "/transactions",
7-
"transactionStatus": "/receipts/",
7+
"transactionStatus": "/transactions/",
88
"accountStatus": "/accounts/",
99
"compileSolidity": "/compile"
1010
}

lib/models/Account.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ const Account = module.exports = class Account {
124124
return this._address;
125125
}
126126
}
127+
get abi() {
128+
if (this.transaction !== undefined) {
129+
return this.transaction.abi;
130+
}
131+
}
127132

128133
toJSON() {
129134
return {
@@ -132,6 +137,7 @@ const Account = module.exports = class Account {
132137
privateKey: this.privateKey,
133138
publicKey: this.publicKey,
134139
balance: this.balance,
140+
transaction: this.transaction,
135141
created: this.created,
136142
updated: this.updated
137143
};
@@ -176,7 +182,18 @@ const Account = module.exports = class Account {
176182
this.balance = d.data.balance;
177183
this.created = new Date(d.data.created);
178184
this.updated = new Date(d.data.updated);
179-
resolve(d.data);
185+
if (d.data.transactionHash !== undefined) {
186+
this.transaction = new Transaction(d.data.transactionHash);
187+
this.transaction.status()
188+
.then((tData) => {
189+
resolve(d.data);
190+
}, (err) => {
191+
process.env.DEBUG ? console.log('Account.transaction.status() error: ' + err) : null;
192+
resolve(d.data);
193+
});
194+
} else {
195+
resolve(d.data);
196+
}
180197
} else {
181198
reject(d);
182199
}
@@ -318,7 +335,6 @@ const Account = module.exports = class Account {
318335
* @param {string|Account|Transaction} to - The address of an existing contract, an Account representing the contract, or the contract creation Transaction.
319336
* @param {string} method - The method in the contract to call.
320337
* @param {array} params - The parameters to use during the method call.
321-
* @param {string|array} code - The ABI of the contract being called.
322338
* @param {number} value - The number of tokens to send to the contract for the method call.
323339
* @returns {Transaction} Returns a transaction which has already been sent.
324340
* @api public
@@ -333,6 +349,8 @@ const Account = module.exports = class Account {
333349
}
334350
if (to.constructor.name === 'Transaction') {
335351
to = new Account(to.address);
352+
} else if (Object.prototype.toString.call(to) === '[object String]') {
353+
to = new Account(to);
336354
}
337355
const tx = new Transaction({
338356
type: 2,

lib/models/Transaction.js

Lines changed: 97 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function numberToBuffer(value) {
2424
bytes[i] = byte;
2525
value = (value - byte) / 256;
2626
}
27-
return new Buffer(bytes);
27+
return new Buffer.from(bytes);
2828
}
2929

3030
/**
@@ -43,43 +43,55 @@ function numberToBuffer(value) {
4343
*/
4444
const Transaction = module.exports = class Transaction {
4545
constructor(data) {
46-
this.type = data.type !== undefined ? data.type : 0; // default to token transfer
47-
this.from = data.from;
48-
this.to = data.to || '';
49-
this.value = data.value;
50-
this.time = data.time !== undefined ? data.time : new Date();
51-
52-
this.code = data.code;
53-
this.abi = data.abi;
54-
this.method = data.method;
55-
this.params = data.params;
56-
57-
this.hash = data.hash;
58-
this.signature = data.signature;
59-
this.address = data.address;
60-
this._id = data.id;
61-
62-
this.hertz = data.hertz;
46+
// Object argument is a full transaction
47+
if (Object.prototype.toString.call(data) === '[object Object]') {
48+
this.type = data.type !== undefined ? data.type : 0; // default to token transfer
49+
this.from = data.from;
50+
this.to = data.to || '';
51+
this.value = data.value;
52+
this.time = data.time !== undefined ? data.time : new Date();
53+
54+
this.code = data.code;
55+
this.abi = data.abi;
56+
this.method = data.method;
57+
this.params = data.params;
58+
59+
this.hash = data.hash;
60+
this.signature = data.signature;
61+
this.address = data.address;
62+
this.gossip = data.gossip;
63+
this._id = data.id;
64+
65+
this.hertz = data.hertz;
66+
67+
// String argument is assumed to be a hash
68+
} else if (Object.prototype.toString.call(data) === '[object String]') {
69+
this.hash = data;
70+
}
6371
}
6472

6573
set type(type) {
66-
assert.isNumber(type, messages.TRANSACTION_TYPE_ISNUMBERINRANGE);
67-
assert.isNumberInRange(type, 0, 2, messages.TRANSACTION_TYPE_ISNUMBERINRANGE);
68-
this._type = type;
74+
if (type !== undefined) {
75+
assert.isNumber(type, messages.TRANSACTION_TYPE_ISNUMBERINRANGE);
76+
assert.isNumberInRange(type, 0, 2, messages.TRANSACTION_TYPE_ISNUMBERINRANGE);
77+
this._type = type;
78+
}
6979
}
7080
get type() {
7181
return this._type || 0;
7282
}
7383
get typeBuffer() {
74-
return Buffer.from(('0' + this.type).slice(-2), 'hex');
84+
return new Buffer.from(('0' + this.type).slice(-2), 'hex');
7585
}
7686

7787
set from(from) {
78-
assert.isAccountable(from, messages.TRANSACTION_FROM_ISACCOUNTABLE);
79-
if (from.constructor.name !== 'Account') {
80-
this._from = new Account(from);
81-
} else {
82-
this._from = from;
88+
if (from !== undefined) {
89+
assert.isAccountable(from, messages.TRANSACTION_FROM_ISACCOUNTABLE);
90+
if (from.constructor.name !== 'Account') {
91+
this._from = new Account(from);
92+
} else {
93+
this._from = from;
94+
}
8395
}
8496
}
8597
get from() {
@@ -145,18 +157,24 @@ const Transaction = module.exports = class Transaction {
145157
set abi(abi) {
146158
if (abi !== undefined) {
147159
if (Object.prototype.toString.call(abi) === '[object String]') {
148-
abi = JSON.parse(abi);
160+
try {
161+
abi = JSON.parse(abi);
162+
} catch (e) {
163+
abi = undefined;
164+
}
165+
}
166+
if (abi !== undefined) {
167+
assert.isArray(abi, messages.TRANSACTION_ABI_ISARRAY);
168+
this._abi = abi;
149169
}
150-
assert.isArray(abi, messages.TRANSACTION_ABI_ISARRAY);
151-
this._abi = abi;
152170
}
153171
}
154172
get abi() {
155173
return this._abi || [];
156174
}
157175
get abiString() {
158176
if (this.type === 2) {
159-
return new Buffer(JSON.stringify(this.abi)).toString('hex');
177+
return new Buffer.from(JSON.stringify(this.abi)).toString('hex');
160178
}
161179
return JSON.stringify(this.abi);
162180
}
@@ -191,17 +209,19 @@ const Transaction = module.exports = class Transaction {
191209
if (this._hash !== undefined) {
192210
return this._hash;
193211
}
194-
assert.isString(this.from.address, messages.TRANSACTION_FROMACCOUNT_ISVALID);
195-
this._hash = keccak('keccak256').update(Buffer.concat([
196-
this.typeBuffer,
197-
new Buffer.from(this.from.address, 'hex'),
198-
new Buffer.from(this.to.address !== null ? this.to.address : '', 'hex'),
199-
new numberToBuffer(this.value),
200-
new Buffer.from(this.code, 'hex'),
201-
new Buffer.from(this.abiString),
202-
new Buffer.from(this.method),
203-
this.timeBuffer
204-
])).digest().toString('hex');
212+
if (this.from !== undefined) {
213+
assert.isString(this.from.address, messages.TRANSACTION_FROMACCOUNT_ISVALID);
214+
this._hash = keccak('keccak256').update(Buffer.concat([
215+
this.typeBuffer,
216+
new Buffer.from(this.from.address, 'hex'),
217+
new Buffer.from(this.to.address !== null ? this.to.address : '', 'hex'),
218+
new numberToBuffer(this.value),
219+
new Buffer.from(this.code, 'hex'),
220+
new Buffer.from(this.abiString),
221+
new Buffer.from(this.method),
222+
this.timeBuffer
223+
])).digest().toString('hex');
224+
}
205225
return this._hash;
206226
}
207227
get id() {
@@ -218,14 +238,14 @@ const Transaction = module.exports = class Transaction {
218238
if (this._signature !== undefined) {
219239
return this._signature;
220240
}
221-
if (this.from.privateKey !== undefined) {
241+
if (this.from !== undefined && this.from.privateKey !== undefined) {
222242
const sig = secp256k1.sign(Buffer.from(this.hash, 'hex'), Buffer.from(this.from.privateKey, 'hex'));
223243
const signatureBytes = new Uint8Array(65);
224244
for (let i = 0; i < 64; i++) {
225245
signatureBytes[i] = sig.signature[i];
226246
}
227247
signatureBytes[64] = sig.recovery;
228-
this._signature = new Buffer(signatureBytes).toString('hex');
248+
this._signature = new Buffer.from(signatureBytes).toString('hex');
229249
}
230250
return this._signature;
231251
}
@@ -250,8 +270,8 @@ const Transaction = module.exports = class Transaction {
250270
return {
251271
hash: this.hash,
252272
type: this.type,
253-
from: this.from.address,
254-
to: this.to.address,
273+
from: this.from !== undefined ? this.from.address : null,
274+
to: this.to !== undefined ? this.to.address : null,
255275
value: this.value,
256276
code: this.code,
257277
abi: this.abiString,
@@ -260,9 +280,10 @@ const Transaction = module.exports = class Transaction {
260280
time: +(this.time),
261281
signature: this.signature,
262282
hertz: this.hertz || 0,
263-
fromName: this.from.name,
264-
toName: this.to.name,
265-
address: this.address
283+
fromName: this.from !== undefined ? this.from.name : null,
284+
toName: this.to !== undefined ? this.to.name : null,
285+
address: this.address,
286+
gossip: this.gossip
266287
};
267288
}
268289

@@ -294,6 +315,7 @@ const Transaction = module.exports = class Transaction {
294315
*/
295316
send() {
296317
if (!this._sendCall) {
318+
assert.exists(this.from, messages.TRANSACTION_FROM_ISACCOUNTABLE);
297319
let network = new Network();
298320
this._sendCall = network.postToDelegate(
299321
{
@@ -342,13 +364,23 @@ const Transaction = module.exports = class Transaction {
342364
path: '/' + network.config.apiVersion + network.config.routes.transactionStatus + this.id
343365
}
344366
).then((d) => {
345-
if (d.data && d.data.status) {
346-
if (d.data.contractAddress) {
347-
this.address = d.data.contractAddress;
348-
}
349-
resolve(d.data);
350-
} else {
367+
if (d.status === undefined || d.status !== 'Ok') {
351368
reject(d);
369+
} else {
370+
if (d.data && d.data.receipt && d.data.receipt.status === 'Ok') {
371+
if (d.data.receipt.contractAddress) {
372+
this.address = d.data.receipt.contractAddress;
373+
}
374+
if (d.data.abi) {
375+
this.abi = d.data.abi;
376+
}
377+
if (d.data.gossip) {
378+
this.gossip = d.data.gossip;
379+
}
380+
resolve(d.data);
381+
} else {
382+
reject(d);
383+
}
352384
}
353385
delete this._statusCall;
354386
}, (e) => {
@@ -412,10 +444,10 @@ const Transaction = module.exports = class Transaction {
412444
}
413445
self.status()
414446
.then((data) => {
415-
if (data.status === status) {
447+
if (data.receipt.status === status) {
416448
resolve(data);
417449
} else {
418-
if (['Pending','NotFound'].indexOf(data.status) > -1) {
450+
if (['Pending','NotFound'].indexOf(data.receipt.status) > -1) {
419451
last = data;
420452
setTimeout(getStatus, 500);
421453
} else {
@@ -424,7 +456,12 @@ const Transaction = module.exports = class Transaction {
424456
}
425457
})
426458
.catch((e) => {
427-
reject(e);
459+
if (['NotFound'].indexOf(e.status) > -1) {
460+
last = e;
461+
setTimeout(getStatus, 500);
462+
} else {
463+
reject(e);
464+
}
428465
});
429466
}
430467
getStatus();
@@ -487,7 +524,7 @@ const Transaction = module.exports = class Transaction {
487524
errors: [],
488525
warnings: []
489526
};
490-
if (compiled.contracts.source) {
527+
if (compiled.contracts !== undefined && compiled.contracts.source !== undefined) {
491528
Object.keys(compiled.contracts.source).forEach((k) => {
492529
ret.contracts.push({
493530
contract: k,
@@ -528,6 +565,7 @@ const Transaction = module.exports = class Transaction {
528565
return findImports(path);
529566
}
530567
};
568+
input.language = 'Solidity';
531569
input.settings = Object.assign({
532570
optimizer: { enabled: true },
533571
outputSelection: {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dispatchlabs/disnode-sdk",
3-
"version": "1.3.2",
3+
"version": "1.4.0",
44
"description": "The Dispatch SDK for Node developers.",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)