@zilliqa-js/account
Advanced tools
Comparing version 0.2.0 to 0.2.1-next.20181112
/// <reference types="node" /> | ||
export default class Account { | ||
privateKey: string; | ||
publicKey: string; | ||
address: string; | ||
nonce: number; | ||
export declare class Account { | ||
/** | ||
@@ -18,2 +14,6 @@ * fromFile | ||
static fromFile(file: string, passphrase: string): Promise<Account>; | ||
privateKey: string; | ||
publicKey: string; | ||
address: string; | ||
nonce: number; | ||
constructor(privateKey: string, nonce?: number); | ||
@@ -20,0 +20,0 @@ /** |
@@ -80,3 +80,3 @@ "use strict"; | ||
}()); | ||
exports.default = Account; | ||
exports.Account = Account; | ||
//# sourceMappingURL=account.js.map |
import * as util from './util'; | ||
export { default as Account } from './account'; | ||
export { default as Wallet } from './wallet'; | ||
export { default as Transaction } from './transaction'; | ||
export * from './account'; | ||
export * from './wallet'; | ||
export * from './transaction'; | ||
export * from './transactionFactory'; | ||
export * from './types'; | ||
export { util }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,9 +6,7 @@ "use strict"; | ||
exports.util = util; | ||
var account_1 = require("./account"); | ||
exports.Account = account_1.default; | ||
var wallet_1 = require("./wallet"); | ||
exports.Wallet = wallet_1.default; | ||
var transaction_1 = require("./transaction"); | ||
exports.Transaction = transaction_1.default; | ||
tslib_1.__exportStar(require("./account"), exports); | ||
tslib_1.__exportStar(require("./wallet"), exports); | ||
tslib_1.__exportStar(require("./transaction"), exports); | ||
tslib_1.__exportStar(require("./transactionFactory"), exports); | ||
tslib_1.__exportStar(require("./types"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -10,3 +10,3 @@ /// <reference types="node" /> | ||
*/ | ||
export default class Transaction implements Signable { | ||
export declare class Transaction implements Signable { | ||
/** | ||
@@ -31,2 +31,4 @@ * confirm | ||
provider: Provider; | ||
id?: string; | ||
status: TxStatus; | ||
private version; | ||
@@ -39,3 +41,2 @@ private nonce?; | ||
private gasLimit; | ||
private id?; | ||
private code; | ||
@@ -45,3 +46,2 @@ private data; | ||
private signature?; | ||
status: TxStatus; | ||
readonly bytes: Buffer; | ||
@@ -112,6 +112,5 @@ readonly senderAddress: string; | ||
* | ||
* The polls are performed with exponential backoff, which means the delay | ||
* between attemps are increased exponentially: | ||
* The polls are performed with a linear backoff: | ||
* | ||
* `const delay = Math.random() * (Math.pow(2, attempt) * 60)` | ||
* `const delay = interval * attempt` | ||
* | ||
@@ -123,5 +122,6 @@ * This is a low-level method that you should generally not have to use | ||
* @param {number} maxAttempts | ||
* @param {number} initial interval in milliseconds | ||
* @returns {Promise<Transaction>} | ||
*/ | ||
confirm(txHash: string, maxAttempts?: number): Promise<Transaction>; | ||
confirm(txHash: string, maxAttempts?: number, interval?: number): Promise<Transaction>; | ||
/** | ||
@@ -128,0 +128,0 @@ * map |
@@ -16,2 +16,4 @@ "use strict"; | ||
if (status === void 0) { status = 0 /* Initialised */; } | ||
// parameters | ||
this.version = 0; | ||
// private members | ||
@@ -75,3 +77,3 @@ this.version = params.version; | ||
return { | ||
version: 0, | ||
version: this.version, | ||
toAddr: this.toAddr, | ||
@@ -172,6 +174,5 @@ nonce: this.nonce, | ||
* | ||
* The polls are performed with exponential backoff, which means the delay | ||
* between attemps are increased exponentially: | ||
* The polls are performed with a linear backoff: | ||
* | ||
* `const delay = Math.random() * (Math.pow(2, attempt) * 60)` | ||
* `const delay = interval * attempt` | ||
* | ||
@@ -183,8 +184,10 @@ * This is a low-level method that you should generally not have to use | ||
* @param {number} maxAttempts | ||
* @param {number} initial interval in milliseconds | ||
* @returns {Promise<Transaction>} | ||
*/ | ||
Transaction.prototype.confirm = function (txHash, maxAttempts) { | ||
if (maxAttempts === void 0) { maxAttempts = 5; } | ||
Transaction.prototype.confirm = function (txHash, maxAttempts, interval) { | ||
if (maxAttempts === void 0) { maxAttempts = 20; } | ||
if (interval === void 0) { interval = 1000; } | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var attempt, err_1, delay; | ||
var attempt, err_1; | ||
return tslib_1.__generator(this, function (_a) { | ||
@@ -213,4 +216,3 @@ switch (_a.label) { | ||
if (!(attempt + 1 < maxAttempts)) return [3 /*break*/, 7]; | ||
delay = Math.random() * (Math.pow(2, attempt) * 60); | ||
return [4 /*yield*/, util_2.sleep(delay)]; | ||
return [4 /*yield*/, util_2.sleep(interval * attempt)]; | ||
case 6: | ||
@@ -279,3 +281,3 @@ _a.sent(); | ||
}()); | ||
exports.default = Transaction; | ||
exports.Transaction = Transaction; | ||
//# sourceMappingURL=transaction.js.map |
import BN from 'bn.js'; | ||
export interface Account { | ||
address: string; | ||
privateKey: string; | ||
publicKey: string; | ||
} | ||
export declare const enum TxStatus { | ||
@@ -8,0 +3,0 @@ Initialised = 0, |
@@ -51,3 +51,3 @@ "use strict"; | ||
code: Uint8Array.from(tslib_1.__spread((tx.code || '')).map(function (c) { return c.charCodeAt(0); })), | ||
data: Uint8Array.from(tslib_1.__spread((tx.code || '')).map(function (c) { return c.charCodeAt(0); })), | ||
data: Uint8Array.from(tslib_1.__spread((tx.data || '')).map(function (c) { return c.charCodeAt(0); })), | ||
}); | ||
@@ -54,0 +54,0 @@ return Buffer.from(proto_1.ZilliqaMessage.ProtoTransactionCoreInfo.encode(msg).finish()); |
import { Signer, Provider } from '@zilliqa-js/core'; | ||
import * as zcrypto from '@zilliqa-js/crypto'; | ||
import Account from './account'; | ||
import Transaction from './transaction'; | ||
export default class Wallet extends Signer { | ||
import { Account } from './account'; | ||
import { Transaction } from './transaction'; | ||
export declare class Wallet extends Signer { | ||
accounts: { | ||
@@ -33,4 +33,5 @@ [address: string]: Account; | ||
* | ||
* @param {string} privateKey | ||
* @returns {string} | ||
* @param {string} privateKey - hex-encoded private key | ||
* @returns {string} - the corresponing address, computer from the private | ||
* key. | ||
*/ | ||
@@ -50,5 +51,13 @@ addByPrivateKey(privateKey: string): string; | ||
addByKeystore(keystore: string, passphrase: string): Promise<string>; | ||
private isValidMnemonic; | ||
addByMnemonic(phrase: string, index?: number): Promise<string>; | ||
/** | ||
* addByMnemonic | ||
* | ||
* Adds an `Account` by use of a mnemonic as specified in BIP-32 and BIP-39 | ||
* | ||
* @param {string} phrase - 12-word mnemonic phrase | ||
* @param {number} index=0 - the number of the child key to add | ||
* @returns {string} - the corresponding address | ||
*/ | ||
addByMnemonic(phrase: string, index?: number): string; | ||
/** | ||
* export | ||
@@ -100,3 +109,4 @@ * | ||
signWith(tx: Transaction, account: string): Promise<Transaction>; | ||
private isValidMnemonic; | ||
} | ||
//# sourceMappingURL=wallet.d.ts.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
var zcrypto = tslib_1.__importStar(require("@zilliqa-js/crypto")); | ||
var account_1 = tslib_1.__importDefault(require("./account")); | ||
var account_1 = require("./account"); | ||
var Wallet = /** @class */ (function (_super) { | ||
@@ -44,3 +44,3 @@ tslib_1.__extends(Wallet, _super); | ||
var privateKey = zcrypto.randomBytes(32); | ||
var newAccount = new account_1.default(privateKey); | ||
var newAccount = new account_1.Account(privateKey); | ||
this.accounts = tslib_1.__assign({}, this.accounts, (_a = {}, _a[newAccount.address] = newAccount, _a)); | ||
@@ -57,8 +57,9 @@ if (!this.defaultAccount) { | ||
* | ||
* @param {string} privateKey | ||
* @returns {string} | ||
* @param {string} privateKey - hex-encoded private key | ||
* @returns {string} - the corresponing address, computer from the private | ||
* key. | ||
*/ | ||
Wallet.prototype.addByPrivateKey = function (privateKey) { | ||
var _a; | ||
var newAccount = new account_1.default(privateKey); | ||
var newAccount = new account_1.Account(privateKey); | ||
this.accounts = tslib_1.__assign({}, this.accounts, (_a = {}, _a[newAccount.address] = newAccount, _a)); | ||
@@ -86,3 +87,3 @@ if (!this.defaultAccount) { | ||
switch (_b.label) { | ||
case 0: return [4 /*yield*/, account_1.default.fromFile(keystore, passphrase)]; | ||
case 0: return [4 /*yield*/, account_1.Account.fromFile(keystore, passphrase)]; | ||
case 1: | ||
@@ -99,23 +100,21 @@ newAccount = _b.sent(); | ||
}; | ||
Wallet.prototype.isValidMnemonic = function (phrase) { | ||
if (phrase.trim().split(/\s+/g).length < 12) { | ||
return false; | ||
} | ||
return bip39_1.default.validateMnemonic(phrase); | ||
}; | ||
/** | ||
* addByMnemonic | ||
* | ||
* Adds an `Account` by use of a mnemonic as specified in BIP-32 and BIP-39 | ||
* | ||
* @param {string} phrase - 12-word mnemonic phrase | ||
* @param {number} index=0 - the number of the child key to add | ||
* @returns {string} - the corresponding address | ||
*/ | ||
Wallet.prototype.addByMnemonic = function (phrase, index) { | ||
if (index === void 0) { index = 0; } | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var seed, hdKey, childKey, privateKey; | ||
return tslib_1.__generator(this, function (_a) { | ||
if (!this.isValidMnemonic(phrase)) { | ||
throw new Error("Invalid mnemonic phrase: " + phrase); | ||
} | ||
seed = bip39_1.default.mnemonicToSeed(phrase); | ||
hdKey = hdkey_1.default.fromMasterSeed(seed); | ||
childKey = hdKey.derive("m/44'/8888'/0'/0/" + index); | ||
privateKey = childKey.privateKey.toString('hex'); | ||
return [2 /*return*/, this.addByPrivateKey(privateKey)]; | ||
}); | ||
}); | ||
if (!this.isValidMnemonic(phrase)) { | ||
throw new Error("Invalid mnemonic phrase: " + phrase); | ||
} | ||
var seed = bip39_1.default.mnemonicToSeed(phrase); | ||
var hdKey = hdkey_1.default.fromMasterSeed(seed); | ||
var childKey = hdKey.derive("m/44'/8888'/0'/0/" + index); | ||
var privateKey = childKey.privateKey.toString('hex'); | ||
return this.addByPrivateKey(privateKey); | ||
}; | ||
@@ -222,5 +221,11 @@ /** | ||
}; | ||
Wallet.prototype.isValidMnemonic = function (phrase) { | ||
if (phrase.trim().split(/\s+/g).length < 12) { | ||
return false; | ||
} | ||
return bip39_1.default.validateMnemonic(phrase); | ||
}; | ||
return Wallet; | ||
}(core_1.Signer)); | ||
exports.default = Wallet; | ||
exports.Wallet = Wallet; | ||
//# sourceMappingURL=wallet.js.map |
{ | ||
"name": "@zilliqa-js/account", | ||
"version": "0.2.0", | ||
"version": "0.2.1-next.20181112", | ||
"description": "Used for signing transactions.", | ||
"main": "dist/index.js", | ||
"node": "dist/index.js", | ||
"main": "dist/index.umd.js", | ||
"node": "dist/index.umd.js", | ||
"browser": "dist/index.umd.js", | ||
@@ -21,5 +21,5 @@ "module": "dist/index.esm.js", | ||
"dependencies": { | ||
"@zilliqa-js/core": "^0.2.0", | ||
"@zilliqa-js/crypto": "^0.2.0", | ||
"@zilliqa-js/util": "^0.2.0", | ||
"@zilliqa-js/core": "^0.2.1-next.20181112", | ||
"@zilliqa-js/crypto": "^0.2.1-next.20181112", | ||
"@zilliqa-js/util": "^0.2.1-next.20181112", | ||
"bip39": "^2.5.0", | ||
@@ -34,3 +34,3 @@ "bn.js": "^4.11.8", | ||
}, | ||
"gitHead": "c1a04e0c11848b02233410c7f1e09e00db1efd26" | ||
"gitHead": "51b5325fbe7a8a986931ce089058fb4619cf85d1" | ||
} |
import * as zcrypto from '@zilliqa-js/crypto'; | ||
import Transaction from './transaction'; | ||
export default class Account { | ||
privateKey: string; | ||
publicKey: string; | ||
address: string; | ||
nonce: number; | ||
export class Account { | ||
/** | ||
@@ -31,2 +25,7 @@ * fromFile | ||
privateKey: string; | ||
publicKey: string; | ||
address: string; | ||
nonce: number; | ||
constructor(privateKey: string, nonce?: number) { | ||
@@ -33,0 +32,0 @@ this.privateKey = privateKey; |
import * as util from './util'; | ||
export { default as Account } from './account'; | ||
export { default as Wallet } from './wallet'; | ||
export { default as Transaction } from './transaction'; | ||
export * from './account'; | ||
export * from './wallet'; | ||
export * from './transaction'; | ||
export * from './transactionFactory'; | ||
export * from './types'; | ||
export { util }; |
@@ -16,3 +16,3 @@ import BN from 'bn.js'; | ||
*/ | ||
export default class Transaction implements Signable { | ||
export class Transaction implements Signable { | ||
/** | ||
@@ -43,5 +43,7 @@ * confirm | ||
provider: Provider; | ||
id?: string; | ||
status: TxStatus; | ||
// parameters | ||
private version: number; | ||
private version: number = 0; | ||
private nonce?: number; | ||
@@ -53,3 +55,2 @@ private toAddr: string; | ||
private gasLimit: BN; | ||
private id?: string; | ||
private code: string; | ||
@@ -60,5 +61,2 @@ private data: string; | ||
// internal state | ||
status: TxStatus; | ||
get bytes(): Buffer { | ||
@@ -78,3 +76,3 @@ return encodeTransactionProto(this.txParams); | ||
return { | ||
version: 0, | ||
version: this.version, | ||
toAddr: this.toAddr, | ||
@@ -199,6 +197,5 @@ nonce: this.nonce, | ||
* | ||
* The polls are performed with exponential backoff, which means the delay | ||
* between attemps are increased exponentially: | ||
* The polls are performed with a linear backoff: | ||
* | ||
* `const delay = Math.random() * (Math.pow(2, attempt) * 60)` | ||
* `const delay = interval * attempt` | ||
* | ||
@@ -210,5 +207,10 @@ * This is a low-level method that you should generally not have to use | ||
* @param {number} maxAttempts | ||
* @param {number} initial interval in milliseconds | ||
* @returns {Promise<Transaction>} | ||
*/ | ||
async confirm(txHash: string, maxAttempts = 5): Promise<Transaction> { | ||
async confirm( | ||
txHash: string, | ||
maxAttempts = 20, | ||
interval = 1000, | ||
): Promise<Transaction> { | ||
this.status = TxStatus.Pending; | ||
@@ -225,4 +227,3 @@ for (let attempt = 0; attempt < maxAttempts; attempt++) { | ||
if (attempt + 1 < maxAttempts) { | ||
const delay = Math.random() * (Math.pow(2, attempt) * 60); | ||
await sleep(delay); | ||
await sleep(interval * attempt); | ||
} | ||
@@ -271,5 +272,7 @@ } | ||
); | ||
if (types.isError(res)) { | ||
return false; | ||
} | ||
this.id = res.result.ID; | ||
@@ -276,0 +279,0 @@ this.receipt = res.result.receipt; |
import BN from 'bn.js'; | ||
export interface Account { | ||
address: string; | ||
privateKey: string; | ||
publicKey: string; | ||
} | ||
export const enum TxStatus { | ||
@@ -10,0 +4,0 @@ Initialised, |
@@ -1,7 +0,2 @@ | ||
import { | ||
ReqMiddlewareFn, | ||
RPCRequest, | ||
RPCRequestPayload, | ||
RPCMethod, | ||
} from '@zilliqa-js/core'; | ||
import { ReqMiddlewareFn, RPCMethod } from '@zilliqa-js/core'; | ||
import { bytes, validation } from '@zilliqa-js/util'; | ||
@@ -19,6 +14,6 @@ import { ZilliqaMessage } from 'proto'; | ||
export const encodeTransaction = (tx: TxParams): Buffer => { | ||
let codeHex = Buffer.from(tx.code || '').toString('hex'); | ||
let dataHex = Buffer.from(tx.data || '').toString('hex'); | ||
const codeHex = Buffer.from(tx.code || '').toString('hex'); | ||
const dataHex = Buffer.from(tx.data || '').toString('hex'); | ||
let encoded = | ||
const encoded = | ||
bytes.intToHexArray(tx.version, 64).join('') + | ||
@@ -64,3 +59,3 @@ bytes.intToHexArray(tx.nonce || 0, 64).join('') + | ||
data: Uint8Array.from( | ||
[...(tx.code || '')].map((c) => <number>c.charCodeAt(0)), | ||
[...(tx.data || '')].map((c) => <number>c.charCodeAt(0)), | ||
), | ||
@@ -67,0 +62,0 @@ }); |
import bip39 from 'bip39'; | ||
import hdkey from 'hdkey'; | ||
import { Signer, Provider, RPCResponse } from '@zilliqa-js/core'; | ||
import { Signer, Provider } from '@zilliqa-js/core'; | ||
import * as zcrypto from '@zilliqa-js/crypto'; | ||
import Account from './account'; | ||
import Transaction from './transaction'; | ||
import { Account } from './account'; | ||
import { Transaction } from './transaction'; | ||
export default class Wallet extends Signer { | ||
export class Wallet extends Signer { | ||
accounts: { [address: string]: Account } = {}; | ||
@@ -62,4 +62,5 @@ defaultAccount?: Account; | ||
* | ||
* @param {string} privateKey | ||
* @returns {string} | ||
* @param {string} privateKey - hex-encoded private key | ||
* @returns {string} - the corresponing address, computer from the private | ||
* key. | ||
*/ | ||
@@ -99,10 +100,12 @@ addByPrivateKey(privateKey: string): string { | ||
private isValidMnemonic(phrase: string): boolean { | ||
if (phrase.trim().split(/\s+/g).length < 12) { | ||
return false; | ||
} | ||
return bip39.validateMnemonic(phrase); | ||
} | ||
async addByMnemonic(phrase: string, index: number = 0): Promise<string> { | ||
/** | ||
* addByMnemonic | ||
* | ||
* Adds an `Account` by use of a mnemonic as specified in BIP-32 and BIP-39 | ||
* | ||
* @param {string} phrase - 12-word mnemonic phrase | ||
* @param {number} index=0 - the number of the child key to add | ||
* @returns {string} - the corresponding address | ||
*/ | ||
addByMnemonic(phrase: string, index: number = 0): string { | ||
if (!this.isValidMnemonic(phrase)) { | ||
@@ -115,2 +118,3 @@ throw new Error(`Invalid mnemonic phrase: ${phrase}`); | ||
const privateKey = childKey.privateKey.toString('hex'); | ||
return this.addByPrivateKey(privateKey); | ||
@@ -230,2 +234,9 @@ } | ||
} | ||
private isValidMnemonic(phrase: string): boolean { | ||
if (phrase.trim().split(/\s+/g).length < 12) { | ||
return false; | ||
} | ||
return bip39.validateMnemonic(phrase); | ||
} | ||
} |
@@ -6,4 +6,4 @@ import fs from 'fs'; | ||
import * as zcrypto from '@zilliqa-js/crypto'; | ||
import Account from '../src/account'; | ||
import Transaction from '../src/transaction'; | ||
import { Account } from '../src/account'; | ||
import { Transaction } from '../src/transaction'; | ||
@@ -26,3 +26,3 @@ describe('Account', () => { | ||
try { | ||
const decrypted = await Account.fromFile(keystore, 'weak_password'); | ||
await Account.fromFile(keystore, 'weak_password'); | ||
} catch (err) { | ||
@@ -29,0 +29,0 @@ expect(err.message).toEqual('Could not decrypt keystore file.'); |
import BN from 'bn.js'; | ||
import { RPCMethod, HTTPProvider } from '@zilliqa-js/core'; | ||
import Transaction from '../src/transaction'; | ||
import Wallet from '../src/wallet'; | ||
import { Transaction } from '../src/transaction'; | ||
import { Wallet } from '../src/wallet'; | ||
@@ -63,8 +63,5 @@ import fetch from 'jest-fetch-mock'; | ||
); | ||
const response = await provider.send( | ||
RPCMethod.CreateTransaction, | ||
pending.txParams, | ||
); | ||
await provider.send(RPCMethod.CreateTransaction, pending.txParams); | ||
const confirmed = await pending.confirm('some_hash'); | ||
const state = confirmed.txParams; | ||
@@ -71,0 +68,0 @@ |
@@ -1,3 +0,3 @@ | ||
import { Provider, HTTPProvider } from '@zilliqa-js/core'; | ||
import Wallet from '../src/wallet'; | ||
import { HTTPProvider } from '@zilliqa-js/core'; | ||
import { Wallet } from '../src/wallet'; | ||
@@ -4,0 +4,0 @@ export const createWallet = ( |
@@ -1,2 +0,1 @@ | ||
import axios from 'axios'; | ||
import BN from 'bn.js'; | ||
@@ -14,5 +13,5 @@ | ||
import { createWallet } from './util'; | ||
import Account from '../src/account'; | ||
import Wallet from '../src/wallet'; | ||
import Transaction from '../src/transaction'; | ||
import { Account } from '../src/account'; | ||
import { Wallet } from '../src/wallet'; | ||
import { Transaction } from '../src/transaction'; | ||
@@ -19,0 +18,0 @@ const provider = new HTTPProvider('https://mock.com'); |
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 too big to display
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 too big to display
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
Sorry, the diff of this file is not supported yet
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
6730329
53
81971
0
423