Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@harmony-js/account

Package Overview
Dependencies
Maintainers
1
Versions
95
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@harmony-js/account - npm Package Compare versions

Comparing version 0.1.11 to 0.1.12

10

dist/account.d.ts

@@ -22,2 +22,3 @@ import { EncryptOptions } from '@harmony-js/crypto';

nonce?: number;
shardID: number;
shards: Shards;

@@ -50,5 +51,12 @@ messenger: Messenger;

*/
updateShards(): Promise<string>;
updateBalances(blockNumber?: string): Promise<void>;
signTransaction(transaction: Transaction, updateNonce?: boolean, encodeMode?: string, blockNumber?: string): Promise<Transaction>;
setMessenger(messenger: Messenger): void;
getAddressFromShardID(shardID: number): string | undefined;
getAddresses(): string[];
getShardBalance(shardID: number, blockNumber?: string): Promise<{
address: string;
balance: string;
nonce: number;
}>;
/**

@@ -55,0 +63,0 @@ * @function _new private method create Account

@@ -13,3 +13,2 @@ "use strict";

this.nonce = 0;
this.shards = new Map().set('default', '');
this.encrypted = false;

@@ -23,2 +22,9 @@ this.messenger = messenger;

}
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(this.shardID, {
address: "" + this.bech32Address + utils_1.AddressSuffix + "0",
balance: this.balance || '0',
nonce: this.nonce || 0,
});
}

@@ -138,12 +144,6 @@ /**

if (!this.messenger) return [3 /*break*/, 3];
return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [
this.address,
blockNumber,
])];
return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [this.address, blockNumber], this.messenger.chainPrefix, this.messenger.currentShard || 0)];
case 1:
balance = _a.sent();
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [
this.address,
blockNumber,
])];
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [this.address, blockNumber], this.messenger.chainPrefix, this.messenger.currentShard || 0)];
case 2:

@@ -159,2 +159,3 @@ nonce = _a.sent();

this.nonce = Number.parseInt(utils_1.hexToNumber(nonce.result), 10);
this.shardID = this.messenger.currentShard || 0;
return [3 /*break*/, 4];

@@ -165,3 +166,3 @@ case 3: throw new Error('No Messenger found');

nonce: this.nonce,
shards: this.shards,
shardID: this.shardID,
}];

@@ -180,6 +181,49 @@ case 5:

*/
Account.prototype.updateShards = function () {
Account.prototype.updateBalances = function (blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, ''];
var shardProviders, shardProviders_1, shardProviders_1_1, _a, name_1, val, balanceObject, e_1_1, currentShard;
var e_1, _b;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
shardProviders = this.messenger.shardProviders;
if (!(shardProviders.size > 1)) return [3 /*break*/, 10];
_c.label = 1;
case 1:
_c.trys.push([1, 7, 8, 9]);
shardProviders_1 = tslib_1.__values(shardProviders), shardProviders_1_1 = shardProviders_1.next();
_c.label = 2;
case 2:
if (!!shardProviders_1_1.done) return [3 /*break*/, 6];
_a = tslib_1.__read(shardProviders_1_1.value, 2), name_1 = _a[0], val = _a[1];
return [4 /*yield*/, this.getShardBalance(val.shardID, blockNumber)];
case 3:
balanceObject = _c.sent();
return [4 /*yield*/, this.shards.set(name_1 === val.shardID ? name_1 : val.shardID, balanceObject)];
case 4:
_c.sent();
_c.label = 5;
case 5:
shardProviders_1_1 = shardProviders_1.next();
return [3 /*break*/, 2];
case 6: return [3 /*break*/, 9];
case 7:
e_1_1 = _c.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 9];
case 8:
try {
if (shardProviders_1_1 && !shardProviders_1_1.done && (_b = shardProviders_1.return)) _b.call(shardProviders_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 9: return [3 /*break*/, 12];
case 10: return [4 /*yield*/, this.getShardBalance(this.messenger.currentShard || 0, blockNumber)];
case 11:
currentShard = _c.sent();
this.shards.set(this.messenger.currentShard || 0, currentShard);
_c.label = 12;
case 12: return [2 /*return*/];
}
});

@@ -193,3 +237,3 @@ });

return tslib_1.__awaiter(this, void 0, void 0, function () {
var balanceObject, _a, signature_1, rawTransaction_1;
var txShardID, shardBalanceObject, shardNonce, _a, signature_1, rawTransaction_1;
var _this = this;

@@ -203,8 +247,14 @@ return tslib_1.__generator(this, function (_b) {

if (!updateNonce) return [3 /*break*/, 2];
return [4 /*yield*/, this.getBalance(blockNumber)];
return [4 /*yield*/, this.updateBalances(blockNumber)];
case 1:
balanceObject = _b.sent();
transaction.setParams(tslib_1.__assign({}, transaction.txParams, { from: this.address || '0x',
// nonce is different from Zilliqa's setting, would be current nonce, not nonce + 1
nonce: balanceObject.nonce }));
_b.sent();
txShardID = transaction.txParams.shardID;
shardBalanceObject = this.shards.get(txShardID);
if (shardBalanceObject !== undefined) {
shardNonce = shardBalanceObject.nonce;
transaction.setParams(tslib_1.__assign({}, transaction.txParams, { from: this.checksumAddress || '0x', nonce: shardNonce }));
}
else {
transaction.setParams(tslib_1.__assign({}, transaction.txParams, { from: this.checksumAddress || '0x', nonce: 0 }));
}
_b.label = 2;

@@ -215,3 +265,3 @@ case 2:

return [2 /*return*/, transaction.map(function (obj) {
return tslib_1.__assign({}, obj, { signature: signature_1, rawTransaction: rawTransaction_1, from: _this.address });
return tslib_1.__assign({}, obj, { signature: signature_1, rawTransaction: rawTransaction_1, from: _this.checksumAddress });
})];

@@ -231,2 +281,57 @@ }

};
Account.prototype.getAddressFromShardID = function (shardID) {
var shardObject = this.shards.get(shardID);
if (shardObject) {
return shardObject.address;
}
else {
return undefined;
}
};
Account.prototype.getAddresses = function () {
var e_2, _a;
var addressArray = [];
try {
for (var _b = tslib_1.__values(this.shards), _c = _b.next(); !_c.done; _c = _b.next()) {
var _d = tslib_1.__read(_c.value, 2), name_2 = _d[0], val = _d[1];
var index = typeof name_2 === 'string' ? Number.parseInt(name_2, 10) : name_2;
addressArray[index] = val.address;
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return addressArray;
};
Account.prototype.getShardBalance = function (shardID, blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var balance, nonce;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [this.address, blockNumber], this.messenger.chainPrefix, shardID)];
case 1:
balance = _a.sent();
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [this.address, blockNumber], this.messenger.chainPrefix, shardID)];
case 2:
nonce = _a.sent();
if (balance.isError()) {
throw balance.error.message;
}
if (nonce.isError()) {
throw nonce.error.message;
}
return [2 /*return*/, {
address: "" + this.bech32Address + utils_1.AddressSuffix + shardID,
balance: utils_1.hexToNumber(balance.result),
nonce: Number.parseInt(utils_1.hexToNumber(nonce.result), 10),
}];
}
});
});
};
/**

@@ -255,3 +360,9 @@ * @function _new private method create Account

this.address = crypto_1.getAddressFromPrivateKey(this.privateKey);
this.shards = new Map().set('default', '');
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(this.shardID, {
address: "" + this.bech32Address + utils_1.AddressSuffix + "0",
balance: this.balance || '0',
nonce: this.nonce || 0,
});
this.encrypted = false;

@@ -258,0 +369,0 @@ return this;

@@ -77,2 +77,13 @@ /**

function __values(o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
}
function __read(o, n) {

@@ -101,3 +112,3 @@ var m = typeof Symbol === "function" && o[Symbol.iterator];

var defaultMessenger = new network.Messenger(new network.HttpProvider('http://localhost:8545'), "hmy" /* Harmony */);
var defaultMessenger = new network.Messenger(new network.HttpProvider('http://localhost:9500'), "hmy" /* Harmony */);

@@ -109,3 +120,2 @@ var Account = /** @class */ (function () {

this.nonce = 0;
this.shards = new Map().set('default', '');
this.encrypted = false;

@@ -119,2 +129,9 @@ this.messenger = messenger;

}
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(this.shardID, {
address: "" + this.bech32Address + utils.AddressSuffix + "0",
balance: this.balance || '0',
nonce: this.nonce || 0,
});
}

@@ -234,12 +251,6 @@ /**

if (!this.messenger) return [3 /*break*/, 3];
return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [
this.address,
blockNumber,
])];
return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [this.address, blockNumber], this.messenger.chainPrefix, this.messenger.currentShard || 0)];
case 1:
balance = _a.sent();
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [
this.address,
blockNumber,
])];
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [this.address, blockNumber], this.messenger.chainPrefix, this.messenger.currentShard || 0)];
case 2:

@@ -255,2 +266,3 @@ nonce = _a.sent();

this.nonce = Number.parseInt(utils.hexToNumber(nonce.result), 10);
this.shardID = this.messenger.currentShard || 0;
return [3 /*break*/, 4];

@@ -261,3 +273,3 @@ case 3: throw new Error('No Messenger found');

nonce: this.nonce,
shards: this.shards,
shardID: this.shardID,
}];

@@ -276,6 +288,49 @@ case 5:

*/
Account.prototype.updateShards = function () {
Account.prototype.updateBalances = function (blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ''];
var shardProviders, shardProviders_1, shardProviders_1_1, _a, name_1, val, balanceObject, e_1_1, currentShard;
var e_1, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
shardProviders = this.messenger.shardProviders;
if (!(shardProviders.size > 1)) return [3 /*break*/, 10];
_c.label = 1;
case 1:
_c.trys.push([1, 7, 8, 9]);
shardProviders_1 = __values(shardProviders), shardProviders_1_1 = shardProviders_1.next();
_c.label = 2;
case 2:
if (!!shardProviders_1_1.done) return [3 /*break*/, 6];
_a = __read(shardProviders_1_1.value, 2), name_1 = _a[0], val = _a[1];
return [4 /*yield*/, this.getShardBalance(val.shardID, blockNumber)];
case 3:
balanceObject = _c.sent();
return [4 /*yield*/, this.shards.set(name_1 === val.shardID ? name_1 : val.shardID, balanceObject)];
case 4:
_c.sent();
_c.label = 5;
case 5:
shardProviders_1_1 = shardProviders_1.next();
return [3 /*break*/, 2];
case 6: return [3 /*break*/, 9];
case 7:
e_1_1 = _c.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 9];
case 8:
try {
if (shardProviders_1_1 && !shardProviders_1_1.done && (_b = shardProviders_1.return)) _b.call(shardProviders_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 9: return [3 /*break*/, 12];
case 10: return [4 /*yield*/, this.getShardBalance(this.messenger.currentShard || 0, blockNumber)];
case 11:
currentShard = _c.sent();
this.shards.set(this.messenger.currentShard || 0, currentShard);
_c.label = 12;
case 12: return [2 /*return*/];
}
});

@@ -289,3 +344,3 @@ });

return __awaiter(this, void 0, void 0, function () {
var balanceObject, _a, signature_1, rawTransaction_1;
var txShardID, shardBalanceObject, shardNonce, _a, signature_1, rawTransaction_1;
var _this = this;

@@ -299,8 +354,14 @@ return __generator(this, function (_b) {

if (!updateNonce) return [3 /*break*/, 2];
return [4 /*yield*/, this.getBalance(blockNumber)];
return [4 /*yield*/, this.updateBalances(blockNumber)];
case 1:
balanceObject = _b.sent();
transaction$$1.setParams(__assign({}, transaction$$1.txParams, { from: this.address || '0x',
// nonce is different from Zilliqa's setting, would be current nonce, not nonce + 1
nonce: balanceObject.nonce }));
_b.sent();
txShardID = transaction$$1.txParams.shardID;
shardBalanceObject = this.shards.get(txShardID);
if (shardBalanceObject !== undefined) {
shardNonce = shardBalanceObject.nonce;
transaction$$1.setParams(__assign({}, transaction$$1.txParams, { from: this.checksumAddress || '0x', nonce: shardNonce }));
}
else {
transaction$$1.setParams(__assign({}, transaction$$1.txParams, { from: this.checksumAddress || '0x', nonce: 0 }));
}
_b.label = 2;

@@ -311,3 +372,3 @@ case 2:

return [2 /*return*/, transaction$$1.map(function (obj) {
return __assign({}, obj, { signature: signature_1, rawTransaction: rawTransaction_1, from: _this.address });
return __assign({}, obj, { signature: signature_1, rawTransaction: rawTransaction_1, from: _this.checksumAddress });
})];

@@ -327,2 +388,57 @@ }

};
Account.prototype.getAddressFromShardID = function (shardID) {
var shardObject = this.shards.get(shardID);
if (shardObject) {
return shardObject.address;
}
else {
return undefined;
}
};
Account.prototype.getAddresses = function () {
var e_2, _a;
var addressArray = [];
try {
for (var _b = __values(this.shards), _c = _b.next(); !_c.done; _c = _b.next()) {
var _d = __read(_c.value, 2), name_2 = _d[0], val = _d[1];
var index = typeof name_2 === 'string' ? Number.parseInt(name_2, 10) : name_2;
addressArray[index] = val.address;
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return addressArray;
};
Account.prototype.getShardBalance = function (shardID, blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return __awaiter(this, void 0, void 0, function () {
var balance, nonce;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [this.address, blockNumber], this.messenger.chainPrefix, shardID)];
case 1:
balance = _a.sent();
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [this.address, blockNumber], this.messenger.chainPrefix, shardID)];
case 2:
nonce = _a.sent();
if (balance.isError()) {
throw balance.error.message;
}
if (nonce.isError()) {
throw nonce.error.message;
}
return [2 /*return*/, {
address: "" + this.bech32Address + utils.AddressSuffix + shardID,
balance: utils.hexToNumber(balance.result),
nonce: Number.parseInt(utils.hexToNumber(nonce.result), 10),
}];
}
});
});
};
/**

@@ -351,3 +467,9 @@ * @function _new private method create Account

this.address = crypto.getAddressFromPrivateKey(this.privateKey);
this.shards = new Map().set('default', '');
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(this.shardID, {
address: "" + this.bech32Address + utils.AddressSuffix + "0",
balance: this.balance || '0',
nonce: this.nonce || 0,
});
this.encrypted = false;

@@ -354,0 +476,0 @@ return this;

@@ -7,3 +7,3 @@ /**

import { generatePrivateKey, getAddressFromPrivateKey, getPubkeyFromPrivateKey, encrypt, decrypt, getAddress, bip39, hdkey, BN } from '@harmony-js/crypto';
import { isPrivateKey, add0xToString, hexToNumber, isAddress, HDPath, isHttp, isWs, Unit, isHex } from '@harmony-js/utils';
import { isPrivateKey, add0xToString, hexToNumber, AddressSuffix, isAddress, HDPath, isHttp, isWs, Unit, isHex } from '@harmony-js/utils';
import { RLPSign, Transaction } from '@harmony-js/transaction';

@@ -74,2 +74,13 @@

function __values(o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
}
function __read(o, n) {

@@ -98,3 +109,3 @@ var m = typeof Symbol === "function" && o[Symbol.iterator];

var defaultMessenger = new Messenger(new HttpProvider('http://localhost:8545'), "hmy" /* Harmony */);
var defaultMessenger = new Messenger(new HttpProvider('http://localhost:9500'), "hmy" /* Harmony */);

@@ -106,3 +117,2 @@ var Account = /** @class */ (function () {

this.nonce = 0;
this.shards = new Map().set('default', '');
this.encrypted = false;

@@ -116,2 +126,9 @@ this.messenger = messenger;

}
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(this.shardID, {
address: "" + this.bech32Address + AddressSuffix + "0",
balance: this.balance || '0',
nonce: this.nonce || 0,
});
}

@@ -231,12 +248,6 @@ /**

if (!this.messenger) return [3 /*break*/, 3];
return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [
this.address,
blockNumber,
])];
return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [this.address, blockNumber], this.messenger.chainPrefix, this.messenger.currentShard || 0)];
case 1:
balance = _a.sent();
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [
this.address,
blockNumber,
])];
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [this.address, blockNumber], this.messenger.chainPrefix, this.messenger.currentShard || 0)];
case 2:

@@ -252,2 +263,3 @@ nonce = _a.sent();

this.nonce = Number.parseInt(hexToNumber(nonce.result), 10);
this.shardID = this.messenger.currentShard || 0;
return [3 /*break*/, 4];

@@ -258,3 +270,3 @@ case 3: throw new Error('No Messenger found');

nonce: this.nonce,
shards: this.shards,
shardID: this.shardID,
}];

@@ -273,6 +285,49 @@ case 5:

*/
Account.prototype.updateShards = function () {
Account.prototype.updateBalances = function (blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ''];
var shardProviders, shardProviders_1, shardProviders_1_1, _a, name_1, val, balanceObject, e_1_1, currentShard;
var e_1, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
shardProviders = this.messenger.shardProviders;
if (!(shardProviders.size > 1)) return [3 /*break*/, 10];
_c.label = 1;
case 1:
_c.trys.push([1, 7, 8, 9]);
shardProviders_1 = __values(shardProviders), shardProviders_1_1 = shardProviders_1.next();
_c.label = 2;
case 2:
if (!!shardProviders_1_1.done) return [3 /*break*/, 6];
_a = __read(shardProviders_1_1.value, 2), name_1 = _a[0], val = _a[1];
return [4 /*yield*/, this.getShardBalance(val.shardID, blockNumber)];
case 3:
balanceObject = _c.sent();
return [4 /*yield*/, this.shards.set(name_1 === val.shardID ? name_1 : val.shardID, balanceObject)];
case 4:
_c.sent();
_c.label = 5;
case 5:
shardProviders_1_1 = shardProviders_1.next();
return [3 /*break*/, 2];
case 6: return [3 /*break*/, 9];
case 7:
e_1_1 = _c.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 9];
case 8:
try {
if (shardProviders_1_1 && !shardProviders_1_1.done && (_b = shardProviders_1.return)) _b.call(shardProviders_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 9: return [3 /*break*/, 12];
case 10: return [4 /*yield*/, this.getShardBalance(this.messenger.currentShard || 0, blockNumber)];
case 11:
currentShard = _c.sent();
this.shards.set(this.messenger.currentShard || 0, currentShard);
_c.label = 12;
case 12: return [2 /*return*/];
}
});

@@ -286,3 +341,3 @@ });

return __awaiter(this, void 0, void 0, function () {
var balanceObject, _a, signature_1, rawTransaction_1;
var txShardID, shardBalanceObject, shardNonce, _a, signature_1, rawTransaction_1;
var _this = this;

@@ -296,8 +351,14 @@ return __generator(this, function (_b) {

if (!updateNonce) return [3 /*break*/, 2];
return [4 /*yield*/, this.getBalance(blockNumber)];
return [4 /*yield*/, this.updateBalances(blockNumber)];
case 1:
balanceObject = _b.sent();
transaction.setParams(__assign({}, transaction.txParams, { from: this.address || '0x',
// nonce is different from Zilliqa's setting, would be current nonce, not nonce + 1
nonce: balanceObject.nonce }));
_b.sent();
txShardID = transaction.txParams.shardID;
shardBalanceObject = this.shards.get(txShardID);
if (shardBalanceObject !== undefined) {
shardNonce = shardBalanceObject.nonce;
transaction.setParams(__assign({}, transaction.txParams, { from: this.checksumAddress || '0x', nonce: shardNonce }));
}
else {
transaction.setParams(__assign({}, transaction.txParams, { from: this.checksumAddress || '0x', nonce: 0 }));
}
_b.label = 2;

@@ -308,3 +369,3 @@ case 2:

return [2 /*return*/, transaction.map(function (obj) {
return __assign({}, obj, { signature: signature_1, rawTransaction: rawTransaction_1, from: _this.address });
return __assign({}, obj, { signature: signature_1, rawTransaction: rawTransaction_1, from: _this.checksumAddress });
})];

@@ -324,2 +385,57 @@ }

};
Account.prototype.getAddressFromShardID = function (shardID) {
var shardObject = this.shards.get(shardID);
if (shardObject) {
return shardObject.address;
}
else {
return undefined;
}
};
Account.prototype.getAddresses = function () {
var e_2, _a;
var addressArray = [];
try {
for (var _b = __values(this.shards), _c = _b.next(); !_c.done; _c = _b.next()) {
var _d = __read(_c.value, 2), name_2 = _d[0], val = _d[1];
var index = typeof name_2 === 'string' ? Number.parseInt(name_2, 10) : name_2;
addressArray[index] = val.address;
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return addressArray;
};
Account.prototype.getShardBalance = function (shardID, blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return __awaiter(this, void 0, void 0, function () {
var balance, nonce;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [this.address, blockNumber], this.messenger.chainPrefix, shardID)];
case 1:
balance = _a.sent();
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [this.address, blockNumber], this.messenger.chainPrefix, shardID)];
case 2:
nonce = _a.sent();
if (balance.isError()) {
throw balance.error.message;
}
if (nonce.isError()) {
throw nonce.error.message;
}
return [2 /*return*/, {
address: "" + this.bech32Address + AddressSuffix + shardID,
balance: hexToNumber(balance.result),
nonce: Number.parseInt(hexToNumber(nonce.result), 10),
}];
}
});
});
};
/**

@@ -348,3 +464,9 @@ * @function _new private method create Account

this.address = getAddressFromPrivateKey(this.privateKey);
this.shards = new Map().set('default', '');
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(this.shardID, {
address: "" + this.bech32Address + AddressSuffix + "0",
balance: this.balance || '0',
nonce: this.nonce || 0,
});
this.encrypted = false;

@@ -351,0 +473,0 @@ return this;

@@ -74,2 +74,13 @@ /**

function __values(o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
}
function __read(o, n) {

@@ -98,3 +109,3 @@ var m = typeof Symbol === "function" && o[Symbol.iterator];

var defaultMessenger = new network.Messenger(new network.HttpProvider('http://localhost:8545'), "hmy" /* Harmony */);
var defaultMessenger = new network.Messenger(new network.HttpProvider('http://localhost:9500'), "hmy" /* Harmony */);

@@ -106,3 +117,2 @@ var Account = /** @class */ (function () {

this.nonce = 0;
this.shards = new Map().set('default', '');
this.encrypted = false;

@@ -116,2 +126,9 @@ this.messenger = messenger;

}
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(this.shardID, {
address: "" + this.bech32Address + utils.AddressSuffix + "0",
balance: this.balance || '0',
nonce: this.nonce || 0,
});
}

@@ -231,12 +248,6 @@ /**

if (!this.messenger) return [3 /*break*/, 3];
return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [
this.address,
blockNumber,
])];
return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [this.address, blockNumber], this.messenger.chainPrefix, this.messenger.currentShard || 0)];
case 1:
balance = _a.sent();
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [
this.address,
blockNumber,
])];
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [this.address, blockNumber], this.messenger.chainPrefix, this.messenger.currentShard || 0)];
case 2:

@@ -252,2 +263,3 @@ nonce = _a.sent();

this.nonce = Number.parseInt(utils.hexToNumber(nonce.result), 10);
this.shardID = this.messenger.currentShard || 0;
return [3 /*break*/, 4];

@@ -258,3 +270,3 @@ case 3: throw new Error('No Messenger found');

nonce: this.nonce,
shards: this.shards,
shardID: this.shardID,
}];

@@ -273,6 +285,49 @@ case 5:

*/
Account.prototype.updateShards = function () {
Account.prototype.updateBalances = function (blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ''];
var shardProviders, shardProviders_1, shardProviders_1_1, _a, name_1, val, balanceObject, e_1_1, currentShard;
var e_1, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
shardProviders = this.messenger.shardProviders;
if (!(shardProviders.size > 1)) return [3 /*break*/, 10];
_c.label = 1;
case 1:
_c.trys.push([1, 7, 8, 9]);
shardProviders_1 = __values(shardProviders), shardProviders_1_1 = shardProviders_1.next();
_c.label = 2;
case 2:
if (!!shardProviders_1_1.done) return [3 /*break*/, 6];
_a = __read(shardProviders_1_1.value, 2), name_1 = _a[0], val = _a[1];
return [4 /*yield*/, this.getShardBalance(val.shardID, blockNumber)];
case 3:
balanceObject = _c.sent();
return [4 /*yield*/, this.shards.set(name_1 === val.shardID ? name_1 : val.shardID, balanceObject)];
case 4:
_c.sent();
_c.label = 5;
case 5:
shardProviders_1_1 = shardProviders_1.next();
return [3 /*break*/, 2];
case 6: return [3 /*break*/, 9];
case 7:
e_1_1 = _c.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 9];
case 8:
try {
if (shardProviders_1_1 && !shardProviders_1_1.done && (_b = shardProviders_1.return)) _b.call(shardProviders_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 9: return [3 /*break*/, 12];
case 10: return [4 /*yield*/, this.getShardBalance(this.messenger.currentShard || 0, blockNumber)];
case 11:
currentShard = _c.sent();
this.shards.set(this.messenger.currentShard || 0, currentShard);
_c.label = 12;
case 12: return [2 /*return*/];
}
});

@@ -286,3 +341,3 @@ });

return __awaiter(this, void 0, void 0, function () {
var balanceObject, _a, signature_1, rawTransaction_1;
var txShardID, shardBalanceObject, shardNonce, _a, signature_1, rawTransaction_1;
var _this = this;

@@ -296,8 +351,14 @@ return __generator(this, function (_b) {

if (!updateNonce) return [3 /*break*/, 2];
return [4 /*yield*/, this.getBalance(blockNumber)];
return [4 /*yield*/, this.updateBalances(blockNumber)];
case 1:
balanceObject = _b.sent();
transaction$$1.setParams(__assign({}, transaction$$1.txParams, { from: this.address || '0x',
// nonce is different from Zilliqa's setting, would be current nonce, not nonce + 1
nonce: balanceObject.nonce }));
_b.sent();
txShardID = transaction$$1.txParams.shardID;
shardBalanceObject = this.shards.get(txShardID);
if (shardBalanceObject !== undefined) {
shardNonce = shardBalanceObject.nonce;
transaction$$1.setParams(__assign({}, transaction$$1.txParams, { from: this.checksumAddress || '0x', nonce: shardNonce }));
}
else {
transaction$$1.setParams(__assign({}, transaction$$1.txParams, { from: this.checksumAddress || '0x', nonce: 0 }));
}
_b.label = 2;

@@ -308,3 +369,3 @@ case 2:

return [2 /*return*/, transaction$$1.map(function (obj) {
return __assign({}, obj, { signature: signature_1, rawTransaction: rawTransaction_1, from: _this.address });
return __assign({}, obj, { signature: signature_1, rawTransaction: rawTransaction_1, from: _this.checksumAddress });
})];

@@ -324,2 +385,57 @@ }

};
Account.prototype.getAddressFromShardID = function (shardID) {
var shardObject = this.shards.get(shardID);
if (shardObject) {
return shardObject.address;
}
else {
return undefined;
}
};
Account.prototype.getAddresses = function () {
var e_2, _a;
var addressArray = [];
try {
for (var _b = __values(this.shards), _c = _b.next(); !_c.done; _c = _b.next()) {
var _d = __read(_c.value, 2), name_2 = _d[0], val = _d[1];
var index = typeof name_2 === 'string' ? Number.parseInt(name_2, 10) : name_2;
addressArray[index] = val.address;
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return addressArray;
};
Account.prototype.getShardBalance = function (shardID, blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return __awaiter(this, void 0, void 0, function () {
var balance, nonce;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.messenger.send("hmy_getBalance" /* GetBalance */, [this.address, blockNumber], this.messenger.chainPrefix, shardID)];
case 1:
balance = _a.sent();
return [4 /*yield*/, this.messenger.send("hmy_getTransactionCount" /* GetTransactionCount */, [this.address, blockNumber], this.messenger.chainPrefix, shardID)];
case 2:
nonce = _a.sent();
if (balance.isError()) {
throw balance.error.message;
}
if (nonce.isError()) {
throw nonce.error.message;
}
return [2 /*return*/, {
address: "" + this.bech32Address + utils.AddressSuffix + shardID,
balance: utils.hexToNumber(balance.result),
nonce: Number.parseInt(utils.hexToNumber(nonce.result), 10),
}];
}
});
});
};
/**

@@ -348,3 +464,9 @@ * @function _new private method create Account

this.address = crypto.getAddressFromPrivateKey(this.privateKey);
this.shards = new Map().set('default', '');
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(this.shardID, {
address: "" + this.bech32Address + utils.AddressSuffix + "0",
balance: this.balance || '0',
nonce: this.nonce || 0,
});
this.encrypted = false;

@@ -351,0 +473,0 @@ return this;

@@ -5,3 +5,8 @@ /**

export declare type ShardID = string | number;
export declare type Shards = Map<ShardID, string>;
export interface BalanceObject {
address: string;
balance: string;
nonce: number;
}
export declare type Shards = Map<ShardID, BalanceObject>;
//# sourceMappingURL=types.d.ts.map

2

dist/utils.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var network_1 = require("@harmony-js/network");
exports.defaultMessenger = new network_1.Messenger(new network_1.HttpProvider('http://localhost:8545'), "hmy" /* Harmony */);
exports.defaultMessenger = new network_1.Messenger(new network_1.HttpProvider('http://localhost:9500'), "hmy" /* Harmony */);
//# sourceMappingURL=utils.js.map
{
"name": "@harmony-js/account",
"version": "0.1.11",
"version": "0.1.12",
"description": "account and wallet for harmony",

@@ -21,9 +21,9 @@ "main": "dist/index.js",

"dependencies": {
"@harmony-js/core": "0.1.11",
"@harmony-js/crypto": "0.1.7",
"@harmony-js/network": "0.1.7",
"@harmony-js/transaction": "0.1.7",
"@harmony-js/utils": "0.1.6"
"@harmony-js/core": "0.1.12",
"@harmony-js/crypto": "0.1.12",
"@harmony-js/network": "0.1.12",
"@harmony-js/transaction": "0.1.12",
"@harmony-js/utils": "0.1.12"
},
"gitHead": "c87f712e37d4a782d2d8b78eff3f0594ad14565d"
"gitHead": "b6b3ff2a6830cfae9db3301b88e1fe636657c7ae"
}

@@ -14,3 +14,3 @@ import {

import { isPrivateKey, add0xToString, hexToNumber } from '@harmony-js/utils';
import { isPrivateKey, add0xToString, hexToNumber, AddressSuffix } from '@harmony-js/utils';
import { Transaction, RLPSign } from '@harmony-js/transaction';

@@ -45,3 +45,4 @@ import { Messenger, RPCMethod } from '@harmony-js/network';

nonce?: number = 0;
shards: Shards = new Map().set('default', '');
shardID: number;
shards: Shards;
messenger: Messenger;

@@ -79,2 +80,9 @@ encrypted: boolean = false;

}
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(this.shardID, {
address: `${this.bech32Address}${AddressSuffix}0`,
balance: this.balance || '0',
nonce: this.nonce || 0,
});
}

@@ -117,11 +125,15 @@

if (this.messenger) {
const balance = await this.messenger.send(RPCMethod.GetBalance, [
this.address,
blockNumber,
]);
const balance = await this.messenger.send(
RPCMethod.GetBalance,
[this.address, blockNumber],
this.messenger.chainPrefix,
this.messenger.currentShard || 0,
);
const nonce = await this.messenger.send(RPCMethod.GetTransactionCount, [
this.address,
blockNumber,
]);
const nonce = await this.messenger.send(
RPCMethod.GetTransactionCount,
[this.address, blockNumber],
this.messenger.chainPrefix,
this.messenger.currentShard || 0,
);
if (balance.isError()) {

@@ -136,2 +148,3 @@ throw balance.error.message;

this.nonce = Number.parseInt(hexToNumber(nonce.result), 10);
this.shardID = this.messenger.currentShard || 0;
} else {

@@ -143,3 +156,3 @@ throw new Error('No Messenger found');

nonce: this.nonce,
shards: this.shards,
shardID: this.shardID,
};

@@ -155,4 +168,17 @@ } catch (error) {

*/
async updateShards(): Promise<string> {
return '';
async updateBalances(blockNumber: string = 'latest'): Promise<void> {
// this.messenger.setShardingProviders();
const shardProviders = this.messenger.shardProviders;
if (shardProviders.size > 1) {
for (const [name, val] of shardProviders) {
const balanceObject = await this.getShardBalance(val.shardID, blockNumber);
await this.shards.set(name === val.shardID ? name : val.shardID, balanceObject);
}
} else {
const currentShard = await this.getShardBalance(
this.messenger.currentShard || 0,
blockNumber,
);
this.shards.set(this.messenger.currentShard || 0, currentShard);
}
}

@@ -169,12 +195,23 @@

}
// let signed = '';
if (updateNonce) {
const balanceObject: any = await this.getBalance(blockNumber);
transaction.setParams({
...transaction.txParams,
from: this.address || '0x',
// nonce is different from Zilliqa's setting, would be current nonce, not nonce + 1
nonce: balanceObject.nonce,
});
await this.updateBalances(blockNumber);
const txShardID = transaction.txParams.shardID;
const shardBalanceObject = this.shards.get(txShardID);
if (shardBalanceObject !== undefined) {
const shardNonce = shardBalanceObject.nonce;
transaction.setParams({
...transaction.txParams,
from: this.checksumAddress || '0x',
nonce: shardNonce,
});
} else {
transaction.setParams({
...transaction.txParams,
from: this.checksumAddress || '0x',
nonce: 0,
});
}
}
if (encodeMode === 'rlp') {

@@ -186,3 +223,3 @@ const [signature, rawTransaction]: [Signature, string] = RLPSign(

return transaction.map((obj: any) => {
return { ...obj, signature, rawTransaction, from: this.address };
return { ...obj, signature, rawTransaction, from: this.checksumAddress };
});

@@ -197,2 +234,47 @@ } else {

}
getAddressFromShardID(shardID: number) {
const shardObject = this.shards.get(shardID);
if (shardObject) {
return shardObject.address;
} else {
return undefined;
}
}
getAddresses(): string[] {
const addressArray: string[] = [];
for (const [name, val] of this.shards) {
const index: number = typeof name === 'string' ? Number.parseInt(name, 10) : name;
addressArray[index] = val.address;
}
return addressArray;
}
async getShardBalance(shardID: number, blockNumber: string = 'latest') {
const balance = await this.messenger.send(
RPCMethod.GetBalance,
[this.address, blockNumber],
this.messenger.chainPrefix,
shardID,
);
const nonce = await this.messenger.send(
RPCMethod.GetTransactionCount,
[this.address, blockNumber],
this.messenger.chainPrefix,
shardID,
);
if (balance.isError()) {
throw balance.error.message;
}
if (nonce.isError()) {
throw nonce.error.message;
}
return {
address: `${this.bech32Address}${AddressSuffix}${shardID}`,
balance: hexToNumber(balance.result),
nonce: Number.parseInt(hexToNumber(nonce.result), 10),
};
}
/**

@@ -222,22 +304,14 @@ * @function _new private method create Account

this.address = getAddressFromPrivateKey(this.privateKey);
this.shards = new Map().set('default', '');
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(this.shardID, {
address: `${this.bech32Address}${AddressSuffix}0`,
balance: this.balance || '0',
nonce: this.nonce || 0,
});
this.encrypted = false;
return this;
}
// /**
// * @function addShard add shard to this Account
// * @param {ShardId} id - ShardId to the Account
// */
// private addShard(id: ShardId): void {
// if (this.shards && this.shards.has('default')) {
// this.shards.set(id, '');
// } else {
// throw new Error(
// 'This account has no default shard or shard is not exist',
// );
// }
// }
}
export { Account };

@@ -5,2 +5,7 @@ /**

export type ShardID = string | number;
export type Shards = Map<ShardID, string>;
export interface BalanceObject {
address: string;
balance: string;
nonce: number;
}
export type Shards = Map<ShardID, BalanceObject>;

@@ -5,4 +5,4 @@ import { HttpProvider, Messenger } from '@harmony-js/network';

export const defaultMessenger = new Messenger(
new HttpProvider('http://localhost:8545'),
new HttpProvider('http://localhost:9500'),
ChainType.Harmony,
);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc