@harmony-js/account
Advanced tools
Comparing version 0.1.30 to 0.1.31
import { EncryptOptions } from '@harmony-js/crypto'; | ||
import { Transaction } from '@harmony-js/transaction'; | ||
import { StakingTransaction } from '@harmony-js/staking'; | ||
import { Messenger } from '@harmony-js/network'; | ||
@@ -52,2 +53,3 @@ import { Shards } from './types'; | ||
signTransaction(transaction: Transaction, updateNonce?: boolean, encodeMode?: string, blockNumber?: string): Promise<Transaction>; | ||
signStaking(staking: StakingTransaction, updateNonce?: boolean, encodeMode?: string, blockNumber?: string, shardID?: number): Promise<StakingTransaction>; | ||
setMessenger(messenger: Messenger): void; | ||
@@ -54,0 +56,0 @@ getAddressFromShardID(shardID: number): string | undefined; |
@@ -275,2 +275,53 @@ "use strict"; | ||
}; | ||
Account.prototype.signStaking = function (staking, updateNonce, encodeMode, blockNumber, shardID) { | ||
if (updateNonce === void 0) { updateNonce = true; } | ||
if (encodeMode === void 0) { encodeMode = 'rlp'; } | ||
if (blockNumber === void 0) { blockNumber = 'latest'; } | ||
if (shardID === void 0) { shardID = this.messenger.currentShard; } | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var txShardID, shardBalanceObject, shardNonce, _a, signature, rawTransaction; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!this.privateKey || !utils_1.isPrivateKey(this.privateKey)) { | ||
throw new Error(this.privateKey + " is not found or not correct"); | ||
} | ||
if (!updateNonce) return [3 /*break*/, 2]; | ||
txShardID = shardID; | ||
return [4 /*yield*/, this.getShardBalance(typeof txShardID === 'string' ? Number.parseInt(txShardID, 10) : txShardID, blockNumber)]; | ||
case 1: | ||
shardBalanceObject = _b.sent(); | ||
if (shardBalanceObject !== undefined) { | ||
shardNonce = shardBalanceObject.nonce; | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
staking.setNonce(shardNonce); | ||
} | ||
else { | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
staking.setNonce(0); | ||
} | ||
_b.label = 2; | ||
case 2: | ||
if (encodeMode === 'rlp') { | ||
_a = tslib_1.__read(staking.rlpSign(this.privateKey), 2), signature = _a[0], rawTransaction = _a[1]; | ||
staking.setRawTransaction(rawTransaction); | ||
staking.setSignature(signature); | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
return [2 /*return*/, staking]; | ||
} | ||
else { | ||
// TODO: if we use other encode method, eg. protobuf, we should implement this | ||
return [2 /*return*/, staking]; | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Account.prototype.setMessenger = function (messenger) { | ||
@@ -277,0 +328,0 @@ this.messenger = messenger; |
@@ -380,2 +380,53 @@ /** | ||
}; | ||
Account.prototype.signStaking = function (staking, updateNonce, encodeMode, blockNumber, shardID) { | ||
if (updateNonce === void 0) { updateNonce = true; } | ||
if (encodeMode === void 0) { encodeMode = 'rlp'; } | ||
if (blockNumber === void 0) { blockNumber = 'latest'; } | ||
if (shardID === void 0) { shardID = this.messenger.currentShard; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var txShardID, shardBalanceObject, shardNonce, _a, signature, rawTransaction; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!this.privateKey || !utils.isPrivateKey(this.privateKey)) { | ||
throw new Error(this.privateKey + " is not found or not correct"); | ||
} | ||
if (!updateNonce) return [3 /*break*/, 2]; | ||
txShardID = shardID; | ||
return [4 /*yield*/, this.getShardBalance(typeof txShardID === 'string' ? Number.parseInt(txShardID, 10) : txShardID, blockNumber)]; | ||
case 1: | ||
shardBalanceObject = _b.sent(); | ||
if (shardBalanceObject !== undefined) { | ||
shardNonce = shardBalanceObject.nonce; | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
staking.setNonce(shardNonce); | ||
} | ||
else { | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
staking.setNonce(0); | ||
} | ||
_b.label = 2; | ||
case 2: | ||
if (encodeMode === 'rlp') { | ||
_a = __read(staking.rlpSign(this.privateKey), 2), signature = _a[0], rawTransaction = _a[1]; | ||
staking.setRawTransaction(rawTransaction); | ||
staking.setSignature(signature); | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
return [2 /*return*/, staking]; | ||
} | ||
else { | ||
// TODO: if we use other encode method, eg. protobuf, we should implement this | ||
return [2 /*return*/, staking]; | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Account.prototype.setMessenger = function (messenger) { | ||
@@ -808,2 +859,60 @@ this.messenger = messenger; | ||
}; | ||
Wallet.prototype.signStaking = function (staking, account, | ||
// tslint:disable-next-line: no-unnecessary-initializer | ||
password, updateNonce, encodeMode, blockNumber, shardID) { | ||
if (account === void 0) { account = this.signer; } | ||
if (password === void 0) { password = undefined; } | ||
if (updateNonce === void 0) { updateNonce = true; } | ||
if (encodeMode === void 0) { encodeMode = 'rlp'; } | ||
if (blockNumber === void 0) { blockNumber = 'latest'; } | ||
if (shardID === void 0) { shardID = this.messenger.currentShard; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var toSignWith, decrypted, signed, error_6, signed, error_7; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
toSignWith = account || this.signer; | ||
if (!toSignWith) { | ||
throw new Error('no signer found or did not provide correct account'); | ||
} | ||
if (!(toSignWith instanceof Account && toSignWith.encrypted && toSignWith.address)) return [3 /*break*/, 7]; | ||
if (!password) { | ||
throw new Error('must provide password to further execution'); | ||
} | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 5, , 6]); | ||
return [4 /*yield*/, this.decryptAccount(toSignWith.address, password)]; | ||
case 2: | ||
decrypted = _a.sent(); | ||
return [4 /*yield*/, decrypted.signStaking(staking, updateNonce, encodeMode, blockNumber, shardID)]; | ||
case 3: | ||
signed = _a.sent(); | ||
return [4 /*yield*/, this.encryptAccount(toSignWith.address, password)]; | ||
case 4: | ||
_a.sent(); | ||
return [2 /*return*/, signed]; | ||
case 5: | ||
error_6 = _a.sent(); | ||
throw error_6; | ||
case 6: return [3 /*break*/, 13]; | ||
case 7: | ||
if (!(toSignWith instanceof Account && !toSignWith.encrypted && toSignWith.address)) return [3 /*break*/, 12]; | ||
_a.label = 8; | ||
case 8: | ||
_a.trys.push([8, 10, , 11]); | ||
return [4 /*yield*/, toSignWith.signStaking(staking, updateNonce, encodeMode, blockNumber, shardID)]; | ||
case 9: | ||
signed = _a.sent(); | ||
return [2 /*return*/, signed]; | ||
case 10: | ||
error_7 = _a.sent(); | ||
throw error_7; | ||
case 11: return [3 /*break*/, 13]; | ||
case 12: throw new Error('sign transaction failed'); | ||
case 13: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -810,0 +919,0 @@ * @function isValidMnemonic |
@@ -376,2 +376,53 @@ /** | ||
}; | ||
Account.prototype.signStaking = function (staking, updateNonce, encodeMode, blockNumber, shardID) { | ||
if (updateNonce === void 0) { updateNonce = true; } | ||
if (encodeMode === void 0) { encodeMode = 'rlp'; } | ||
if (blockNumber === void 0) { blockNumber = 'latest'; } | ||
if (shardID === void 0) { shardID = this.messenger.currentShard; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var txShardID, shardBalanceObject, shardNonce, _a, signature, rawTransaction; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!this.privateKey || !isPrivateKey(this.privateKey)) { | ||
throw new Error(this.privateKey + " is not found or not correct"); | ||
} | ||
if (!updateNonce) return [3 /*break*/, 2]; | ||
txShardID = shardID; | ||
return [4 /*yield*/, this.getShardBalance(typeof txShardID === 'string' ? Number.parseInt(txShardID, 10) : txShardID, blockNumber)]; | ||
case 1: | ||
shardBalanceObject = _b.sent(); | ||
if (shardBalanceObject !== undefined) { | ||
shardNonce = shardBalanceObject.nonce; | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
staking.setNonce(shardNonce); | ||
} | ||
else { | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
staking.setNonce(0); | ||
} | ||
_b.label = 2; | ||
case 2: | ||
if (encodeMode === 'rlp') { | ||
_a = __read(staking.rlpSign(this.privateKey), 2), signature = _a[0], rawTransaction = _a[1]; | ||
staking.setRawTransaction(rawTransaction); | ||
staking.setSignature(signature); | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
return [2 /*return*/, staking]; | ||
} | ||
else { | ||
// TODO: if we use other encode method, eg. protobuf, we should implement this | ||
return [2 /*return*/, staking]; | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Account.prototype.setMessenger = function (messenger) { | ||
@@ -804,2 +855,60 @@ this.messenger = messenger; | ||
}; | ||
Wallet.prototype.signStaking = function (staking, account, | ||
// tslint:disable-next-line: no-unnecessary-initializer | ||
password, updateNonce, encodeMode, blockNumber, shardID) { | ||
if (account === void 0) { account = this.signer; } | ||
if (password === void 0) { password = undefined; } | ||
if (updateNonce === void 0) { updateNonce = true; } | ||
if (encodeMode === void 0) { encodeMode = 'rlp'; } | ||
if (blockNumber === void 0) { blockNumber = 'latest'; } | ||
if (shardID === void 0) { shardID = this.messenger.currentShard; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var toSignWith, decrypted, signed, error_6, signed, error_7; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
toSignWith = account || this.signer; | ||
if (!toSignWith) { | ||
throw new Error('no signer found or did not provide correct account'); | ||
} | ||
if (!(toSignWith instanceof Account && toSignWith.encrypted && toSignWith.address)) return [3 /*break*/, 7]; | ||
if (!password) { | ||
throw new Error('must provide password to further execution'); | ||
} | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 5, , 6]); | ||
return [4 /*yield*/, this.decryptAccount(toSignWith.address, password)]; | ||
case 2: | ||
decrypted = _a.sent(); | ||
return [4 /*yield*/, decrypted.signStaking(staking, updateNonce, encodeMode, blockNumber, shardID)]; | ||
case 3: | ||
signed = _a.sent(); | ||
return [4 /*yield*/, this.encryptAccount(toSignWith.address, password)]; | ||
case 4: | ||
_a.sent(); | ||
return [2 /*return*/, signed]; | ||
case 5: | ||
error_6 = _a.sent(); | ||
throw error_6; | ||
case 6: return [3 /*break*/, 13]; | ||
case 7: | ||
if (!(toSignWith instanceof Account && !toSignWith.encrypted && toSignWith.address)) return [3 /*break*/, 12]; | ||
_a.label = 8; | ||
case 8: | ||
_a.trys.push([8, 10, , 11]); | ||
return [4 /*yield*/, toSignWith.signStaking(staking, updateNonce, encodeMode, blockNumber, shardID)]; | ||
case 9: | ||
signed = _a.sent(); | ||
return [2 /*return*/, signed]; | ||
case 10: | ||
error_7 = _a.sent(); | ||
throw error_7; | ||
case 11: return [3 /*break*/, 13]; | ||
case 12: throw new Error('sign transaction failed'); | ||
case 13: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -806,0 +915,0 @@ * @function isValidMnemonic |
@@ -377,2 +377,53 @@ /** | ||
}; | ||
Account.prototype.signStaking = function (staking, updateNonce, encodeMode, blockNumber, shardID) { | ||
if (updateNonce === void 0) { updateNonce = true; } | ||
if (encodeMode === void 0) { encodeMode = 'rlp'; } | ||
if (blockNumber === void 0) { blockNumber = 'latest'; } | ||
if (shardID === void 0) { shardID = this.messenger.currentShard; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var txShardID, shardBalanceObject, shardNonce, _a, signature, rawTransaction; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!this.privateKey || !utils.isPrivateKey(this.privateKey)) { | ||
throw new Error(this.privateKey + " is not found or not correct"); | ||
} | ||
if (!updateNonce) return [3 /*break*/, 2]; | ||
txShardID = shardID; | ||
return [4 /*yield*/, this.getShardBalance(typeof txShardID === 'string' ? Number.parseInt(txShardID, 10) : txShardID, blockNumber)]; | ||
case 1: | ||
shardBalanceObject = _b.sent(); | ||
if (shardBalanceObject !== undefined) { | ||
shardNonce = shardBalanceObject.nonce; | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
staking.setNonce(shardNonce); | ||
} | ||
else { | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
staking.setNonce(0); | ||
} | ||
_b.label = 2; | ||
case 2: | ||
if (encodeMode === 'rlp') { | ||
_a = __read(staking.rlpSign(this.privateKey), 2), signature = _a[0], rawTransaction = _a[1]; | ||
staking.setRawTransaction(rawTransaction); | ||
staking.setSignature(signature); | ||
staking.setFromAddress(this.messenger.chainPrefix === "hmy" /* Harmony */ | ||
? this.bech32Address | ||
: this.checksumAddress || '0x'); | ||
return [2 /*return*/, staking]; | ||
} | ||
else { | ||
// TODO: if we use other encode method, eg. protobuf, we should implement this | ||
return [2 /*return*/, staking]; | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Account.prototype.setMessenger = function (messenger) { | ||
@@ -805,2 +856,60 @@ this.messenger = messenger; | ||
}; | ||
Wallet.prototype.signStaking = function (staking, account, | ||
// tslint:disable-next-line: no-unnecessary-initializer | ||
password, updateNonce, encodeMode, blockNumber, shardID) { | ||
if (account === void 0) { account = this.signer; } | ||
if (password === void 0) { password = undefined; } | ||
if (updateNonce === void 0) { updateNonce = true; } | ||
if (encodeMode === void 0) { encodeMode = 'rlp'; } | ||
if (blockNumber === void 0) { blockNumber = 'latest'; } | ||
if (shardID === void 0) { shardID = this.messenger.currentShard; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var toSignWith, decrypted, signed, error_6, signed, error_7; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
toSignWith = account || this.signer; | ||
if (!toSignWith) { | ||
throw new Error('no signer found or did not provide correct account'); | ||
} | ||
if (!(toSignWith instanceof Account && toSignWith.encrypted && toSignWith.address)) return [3 /*break*/, 7]; | ||
if (!password) { | ||
throw new Error('must provide password to further execution'); | ||
} | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 5, , 6]); | ||
return [4 /*yield*/, this.decryptAccount(toSignWith.address, password)]; | ||
case 2: | ||
decrypted = _a.sent(); | ||
return [4 /*yield*/, decrypted.signStaking(staking, updateNonce, encodeMode, blockNumber, shardID)]; | ||
case 3: | ||
signed = _a.sent(); | ||
return [4 /*yield*/, this.encryptAccount(toSignWith.address, password)]; | ||
case 4: | ||
_a.sent(); | ||
return [2 /*return*/, signed]; | ||
case 5: | ||
error_6 = _a.sent(); | ||
throw error_6; | ||
case 6: return [3 /*break*/, 13]; | ||
case 7: | ||
if (!(toSignWith instanceof Account && !toSignWith.encrypted && toSignWith.address)) return [3 /*break*/, 12]; | ||
_a.label = 8; | ||
case 8: | ||
_a.trys.push([8, 10, , 11]); | ||
return [4 /*yield*/, toSignWith.signStaking(staking, updateNonce, encodeMode, blockNumber, shardID)]; | ||
case 9: | ||
signed = _a.sent(); | ||
return [2 /*return*/, signed]; | ||
case 10: | ||
error_7 = _a.sent(); | ||
throw error_7; | ||
case 11: return [3 /*break*/, 13]; | ||
case 12: throw new Error('sign transaction failed'); | ||
case 13: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -807,0 +916,0 @@ * @function isValidMnemonic |
import { EncryptOptions } from '@harmony-js/crypto'; | ||
import { Messenger } from '@harmony-js/network'; | ||
import { Transaction } from '@harmony-js/transaction'; | ||
import { StakingTransaction } from '@harmony-js/staking'; | ||
import { Account } from './account'; | ||
@@ -97,2 +98,3 @@ declare class Wallet { | ||
signTransaction(transaction: Transaction, account?: Account | undefined, password?: string | undefined, updateNonce?: boolean, encodeMode?: string, blockNumber?: string): Promise<Transaction>; | ||
signStaking(staking: StakingTransaction, account?: Account | undefined, password?: string | undefined, updateNonce?: boolean, encodeMode?: string, blockNumber?: string, shardID?: number): Promise<StakingTransaction>; | ||
/** | ||
@@ -99,0 +101,0 @@ * @function isValidMnemonic |
@@ -341,2 +341,60 @@ "use strict"; | ||
}; | ||
Wallet.prototype.signStaking = function (staking, account, | ||
// tslint:disable-next-line: no-unnecessary-initializer | ||
password, updateNonce, encodeMode, blockNumber, shardID) { | ||
if (account === void 0) { account = this.signer; } | ||
if (password === void 0) { password = undefined; } | ||
if (updateNonce === void 0) { updateNonce = true; } | ||
if (encodeMode === void 0) { encodeMode = 'rlp'; } | ||
if (blockNumber === void 0) { blockNumber = 'latest'; } | ||
if (shardID === void 0) { shardID = this.messenger.currentShard; } | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var toSignWith, decrypted, signed, error_6, signed, error_7; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
toSignWith = account || this.signer; | ||
if (!toSignWith) { | ||
throw new Error('no signer found or did not provide correct account'); | ||
} | ||
if (!(toSignWith instanceof account_1.Account && toSignWith.encrypted && toSignWith.address)) return [3 /*break*/, 7]; | ||
if (!password) { | ||
throw new Error('must provide password to further execution'); | ||
} | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 5, , 6]); | ||
return [4 /*yield*/, this.decryptAccount(toSignWith.address, password)]; | ||
case 2: | ||
decrypted = _a.sent(); | ||
return [4 /*yield*/, decrypted.signStaking(staking, updateNonce, encodeMode, blockNumber, shardID)]; | ||
case 3: | ||
signed = _a.sent(); | ||
return [4 /*yield*/, this.encryptAccount(toSignWith.address, password)]; | ||
case 4: | ||
_a.sent(); | ||
return [2 /*return*/, signed]; | ||
case 5: | ||
error_6 = _a.sent(); | ||
throw error_6; | ||
case 6: return [3 /*break*/, 13]; | ||
case 7: | ||
if (!(toSignWith instanceof account_1.Account && !toSignWith.encrypted && toSignWith.address)) return [3 /*break*/, 12]; | ||
_a.label = 8; | ||
case 8: | ||
_a.trys.push([8, 10, , 11]); | ||
return [4 /*yield*/, toSignWith.signStaking(staking, updateNonce, encodeMode, blockNumber, shardID)]; | ||
case 9: | ||
signed = _a.sent(); | ||
return [2 /*return*/, signed]; | ||
case 10: | ||
error_7 = _a.sent(); | ||
throw error_7; | ||
case 11: return [3 /*break*/, 13]; | ||
case 12: throw new Error('sign transaction failed'); | ||
case 13: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -343,0 +401,0 @@ * @function isValidMnemonic |
{ | ||
"name": "@harmony-js/account", | ||
"version": "0.1.30", | ||
"version": "0.1.31", | ||
"description": "account and wallet for harmony", | ||
@@ -21,9 +21,10 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@harmony-js/core": "0.1.30", | ||
"@harmony-js/core": "0.1.31", | ||
"@harmony-js/crypto": "0.1.28", | ||
"@harmony-js/network": "0.1.29", | ||
"@harmony-js/transaction": "0.1.30", | ||
"@harmony-js/network": "0.1.31", | ||
"@harmony-js/staking": "0.1.31", | ||
"@harmony-js/transaction": "0.1.29", | ||
"@harmony-js/utils": "0.1.28" | ||
}, | ||
"gitHead": "60208454d7013bd74d613b8d31d07a9789144e77" | ||
"gitHead": "09a11f531083647905757788a54b6b9a618a29a6" | ||
} |
@@ -22,2 +22,3 @@ import { | ||
import { Transaction, RLPSign } from '@harmony-js/transaction'; | ||
import { StakingTransaction } from '@harmony-js/staking'; | ||
import { Messenger, RPCMethod } from '@harmony-js/network'; | ||
@@ -246,2 +247,55 @@ import { Shards } from './types'; | ||
} | ||
async signStaking( | ||
staking: StakingTransaction, | ||
updateNonce: boolean = true, | ||
encodeMode: string = 'rlp', | ||
blockNumber: string = 'latest', | ||
shardID: number = this.messenger.currentShard, | ||
): Promise<StakingTransaction> { | ||
if (!this.privateKey || !isPrivateKey(this.privateKey)) { | ||
throw new Error(`${this.privateKey} is not found or not correct`); | ||
} | ||
if (updateNonce) { | ||
// await this.updateBalances(blockNumber); | ||
const txShardID = shardID; | ||
const shardBalanceObject = await this.getShardBalance( | ||
typeof txShardID === 'string' ? Number.parseInt(txShardID, 10) : txShardID, | ||
blockNumber, | ||
); | ||
if (shardBalanceObject !== undefined) { | ||
const shardNonce = shardBalanceObject.nonce; | ||
staking.setFromAddress( | ||
this.messenger.chainPrefix === ChainType.Harmony | ||
? this.bech32Address | ||
: this.checksumAddress || '0x', | ||
); | ||
staking.setNonce(shardNonce); | ||
} else { | ||
staking.setFromAddress( | ||
this.messenger.chainPrefix === ChainType.Harmony | ||
? this.bech32Address | ||
: this.checksumAddress || '0x', | ||
); | ||
staking.setNonce(0); | ||
} | ||
} | ||
if (encodeMode === 'rlp') { | ||
const [signature, rawTransaction]: [Signature, string] = staking.rlpSign(this.privateKey); | ||
staking.setRawTransaction(rawTransaction); | ||
staking.setSignature(signature); | ||
staking.setFromAddress( | ||
this.messenger.chainPrefix === ChainType.Harmony | ||
? this.bech32Address | ||
: this.checksumAddress || '0x', | ||
); | ||
return staking; | ||
} else { | ||
// TODO: if we use other encode method, eg. protobuf, we should implement this | ||
return staking; | ||
} | ||
} | ||
setMessenger(messenger: Messenger) { | ||
@@ -248,0 +302,0 @@ this.messenger = messenger; |
@@ -5,2 +5,3 @@ import { bip39, hdkey, EncryptOptions, getAddress, generatePrivateKey } from '@harmony-js/crypto'; | ||
import { Transaction } from '@harmony-js/transaction'; | ||
import { StakingTransaction } from '@harmony-js/staking'; | ||
import { Account } from './account'; | ||
@@ -278,2 +279,51 @@ import { defaultMessenger } from './utils'; | ||
} | ||
async signStaking( | ||
staking: StakingTransaction, | ||
account: Account | undefined = this.signer, | ||
// tslint:disable-next-line: no-unnecessary-initializer | ||
password: string | undefined = undefined, | ||
updateNonce: boolean = true, | ||
encodeMode: string = 'rlp', | ||
blockNumber: string = 'latest', | ||
shardID: number = this.messenger.currentShard, | ||
): Promise<StakingTransaction> { | ||
const toSignWith = account || this.signer; | ||
if (!toSignWith) { | ||
throw new Error('no signer found or did not provide correct account'); | ||
} | ||
if (toSignWith instanceof Account && toSignWith.encrypted && toSignWith.address) { | ||
if (!password) { | ||
throw new Error('must provide password to further execution'); | ||
} | ||
try { | ||
const decrypted = await this.decryptAccount(toSignWith.address, password); | ||
const signed = await decrypted.signStaking( | ||
staking, | ||
updateNonce, | ||
encodeMode, | ||
blockNumber, | ||
shardID, | ||
); | ||
await this.encryptAccount(toSignWith.address, password); | ||
return signed; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} else if (toSignWith instanceof Account && !toSignWith.encrypted && toSignWith.address) { | ||
try { | ||
const signed = await toSignWith.signStaking( | ||
staking, | ||
updateNonce, | ||
encodeMode, | ||
blockNumber, | ||
shardID, | ||
); | ||
return signed; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} else { | ||
throw new Error('sign transaction failed'); | ||
} | ||
} | ||
/** | ||
@@ -280,0 +330,0 @@ * @function isValidMnemonic |
@@ -9,7 +9,8 @@ { | ||
"references": [ | ||
{"path": "../harmony-crypto"}, | ||
{"path": "../harmony-utils"}, | ||
{"path": "../harmony-transaction"}, | ||
{"path": "../harmony-network"} | ||
{ "path": "../harmony-crypto" }, | ||
{ "path": "../harmony-utils" }, | ||
{ "path": "../harmony-transaction" }, | ||
{ "path": "../harmony-staking" }, | ||
{ "path": "../harmony-network" } | ||
] | ||
} |
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
Sorry, the diff of this file is not supported yet
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
485958
5566
6
+ Added@harmony-js/staking@0.1.31
+ Added@harmony-js/contract@0.1.31(transitive)
+ Added@harmony-js/core@0.1.31(transitive)
+ Added@harmony-js/network@0.1.31(transitive)
+ Added@harmony-js/staking@0.1.31(transitive)
+ Added@harmony-js/transaction@0.1.290.1.31(transitive)
+ Added@types/node@22.9.4(transitive)
+ Addedtext-encoding@0.7.0(transitive)
+ Addedundici-types@6.19.8(transitive)
- Removed@harmony-js/contract@0.1.30(transitive)
- Removed@harmony-js/core@0.1.30(transitive)
- Removed@harmony-js/transaction@0.1.30(transitive)
- Removed@types/node@22.10.0(transitive)
- Removedundici-types@6.20.0(transitive)
Updated@harmony-js/core@0.1.31
Updated@harmony-js/network@0.1.31