[WIP] update @harmony/core

@types
neeboo 6 years ago
parent cf7f7be197
commit 40ae81a5bc
  1. 11
      README.md
  2. 4
      examples/temp.html
  3. 29
      examples/testNode.js
  4. 1
      gulpfile.js
  5. 26
      packages/harmony-core/package.json
  6. 26
      packages/harmony-core/src/core.ts
  7. 2
      packages/harmony-core/src/index.ts
  8. 0
      packages/harmony-core/src/types.ts
  9. 15
      packages/harmony-core/tsconfig.json
  10. 6
      packages/harmony-network/src/providers/http.ts
  11. 3
      packages/harmony-transaction/package.json
  12. 28
      packages/harmony-transaction/src/factory.ts
  13. 88
      packages/harmony-transaction/src/transaction.ts
  14. 9
      packages/harmony-transaction/src/types.ts
  15. 10
      packages/harmony-transaction/src/utils.ts
  16. 3
      packages/harmony-transaction/tsconfig.json
  17. 1
      scripts/packages.js
  18. 1
      scripts/packagesList.js
  19. 1
      scripts/packagesTs.ts
  20. 1
      tsconfig.json

@ -6,11 +6,12 @@ It's a mono-repo library, not yet published to npm.
# Packages
1. [harmony-account](https://github.com/FireStack-Lab/Harmony-sdk-core/tree/master/packages/harmony-account)
2. [harmony-crypto](https://github.com/FireStack-Lab/Harmony-sdk-core/tree/master/packages/harmony-crypto)
3. [harmony-network](https://github.com/FireStack-Lab/Harmony-sdk-core/tree/master/packages/harmony-network)
4. [harmony-utils](https://github.com/FireStack-Lab/Harmony-sdk-core/tree/master/packages/harmony-utils)
5. [harmony-transaction](https://github.com/FireStack-Lab/Harmony-sdk-core/tree/master/packages/harmony-transaction)
1. [harmony-core](https://github.com/FireStack-Lab/Harmony-sdk-core/tree/master/packages/harmony-core)
2. [harmony-account](https://github.com/FireStack-Lab/Harmony-sdk-core/tree/master/packages/harmony-account)
3. [harmony-crypto](https://github.com/FireStack-Lab/Harmony-sdk-core/tree/master/packages/harmony-crypto)
4. [harmony-network](https://github.com/FireStack-Lab/Harmony-sdk-core/tree/master/packages/harmony-network)
5. [harmony-utils](https://github.com/FireStack-Lab/Harmony-sdk-core/tree/master/packages/harmony-utils)
6. [harmony-transaction](https://github.com/FireStack-Lab/Harmony-sdk-core/tree/master/packages/harmony-transaction)
# Usage

@ -35,9 +35,7 @@
</div>
<script src="../dist/HarmonyAccount.browser.js"></script>
<script src="../dist/HarmonyNetwork.browser.js"></script>
<script src="../dist/HarmonyJs.browser.js"></script>
</body>
</html>

@ -9,7 +9,7 @@ const {
strip0x,
} = require('@harmony/utils');
const { HttpProvider, Messenger } = require('@harmony/network');
const { Transaction } = require('@harmony/transaction');
const { Transaction, TransactionFactory } = require('@harmony/transaction');
const {
arrayify,
hexlify,
@ -24,8 +24,11 @@ const {
getAddressFromPublicKey,
} = require('@harmony/crypto');
const { Harmony } = require('@harmony/core');
const msgr = new Messenger(new HttpProvider('https://dev-api.zilliqa.com'));
const wallet = new Wallet(msgr);
const transactions = new TransactionFactory(msgr);
async function testEncrypt() {
const mne = wallet.generateMnemonic();
@ -80,16 +83,17 @@ const acc = wallet.addByPrivateKey(
// console.log(txn.getRLPUnsigned()[0]);
const signed = wallet
.getAccount(acc.address)
.signTransaction(txn, false)
.then((tx) => {
const newTx = tx.recover(tx.unsignedTxnHash);
// console.log(newTx);
acc.signTransaction(newTx, false, 'rlp').then((signed) => {
console.log(signed);
});
});
// const signed = wallet
// .getAccount(acc.address)
// .signTransaction(txn, false)
// .then((tx) => {
// const newTx = tx.recover(tx.unsignedTxnHash);
// // console.log(newTx);
// acc.signTransaction(newTx, false, 'rlp').then((signed) => {
// const ttt = transactions.recover(signed.txnHash);
// console.log(ttt);
// });
// });
// recoverAddress();
// console.log(wallet.messenger);
@ -119,3 +123,6 @@ const signed = wallet
// });
// console.log(getContractAddress(acc.publicKey, 248));
const harmony = new Harmony('https://devnet.harmony.one');
console.log(harmony);

@ -2,6 +2,7 @@ const { task } = require('gulp');
const del = require('del');
const packages = [
'harmony-core',
'harmony-crypto',
'harmony-account',
'harmony-network',

@ -0,0 +1,26 @@
{
"name": "@harmony/core",
"version": "0.0.1",
"description": "harmony core package",
"main": "dist/index.js",
"node": "dist/index.js",
"browser": "dist/index.js",
"module": "dist/index.esm.js",
"jsnext:main": "dist/index.esm.js",
"typings": "dist/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"publishConfig": {
"access": "public"
},
"author": "neeboo@firestack.one",
"license": "ISC",
"dependencies": {
"@harmony/account": "^0.0.1",
"@harmony/crypto": "^0.0.1",
"@harmony/network": "^0.0.1",
"@harmony/transaction": "^0.0.1",
"@harmony/utils": "^0.0.1"
}
}

@ -0,0 +1,26 @@
import * as crypto from '@harmony/crypto';
import * as utils from '@harmony/utils';
import { HttpProvider, Messenger } from '@harmony/network';
import { TransactionFactory } from '@harmony/transaction';
import { Wallet } from '@harmony/account';
class Harmony {
messenger: Messenger;
transactions: TransactionFactory;
wallet: Wallet;
crypto: any;
utils: any;
private provider: HttpProvider;
constructor(url: string) {
this.provider = new HttpProvider(url);
this.messenger = new Messenger(this.provider);
this.transactions = new TransactionFactory(this.messenger);
this.wallet = new Wallet(this.messenger);
this.crypto = crypto;
this.utils = utils;
}
}
export { Harmony };

@ -0,0 +1,2 @@
export * from './core';
// export * from './types';

@ -0,0 +1,15 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"include": ["src", "../../typings/**/*.d.ts"],
"references": [
{ "path": "../harmony-account" },
{ "path": "../harmony-crypto" },
{ "path": "../harmony-utils" },
{ "path": "../harmony-network" },
{ "path": "../harmony-transaction" }
]
}

@ -19,9 +19,9 @@ const defaultOptions = {
class HttpProvider extends BaseProvider {
url: string;
fetcher: any;
options: any;
constructor(url: string, options: any, fetcher: any) {
fetcher?: any;
options?: any;
constructor(url: string, options?: any, fetcher?: any) {
super(url);
this.url = url || 'http://localhost:4201';
this.fetcher = fetcher || fetchRPC;

@ -15,6 +15,7 @@
"license": "ISC",
"dependencies": {
"@harmony/crypto": "^0.0.1",
"@harmony/utils": "^0.0.1"
"@harmony/utils": "^0.0.1",
"@harmony/network":"^0.0.1"
}
}

@ -1,3 +1,29 @@
class TransactionFactory {}
import { getContractAddress } from '@harmony/crypto';
import { Messenger } from '@harmony/network';
import { Transaction } from './transaction';
import { TxParams, TxStatus } from './types';
class TransactionFactory {
messenger: Messenger;
constructor(messenger: Messenger) {
this.messenger = messenger;
}
getContractAddress(tx: Transaction) {
const { from, nonce } = tx.txParams;
return getContractAddress(from, Number.parseInt(`${nonce}`, 10));
}
newTx(txParams: TxParams): Transaction {
return new Transaction(txParams, this.messenger, TxStatus.INTIALIZED);
}
recover(txHash: string): Transaction {
const newTxn = new Transaction(
undefined,
this.messenger,
TxStatus.INTIALIZED,
);
newTxn.recover(txHash);
return newTxn;
}
}
export { TransactionFactory };

@ -1,31 +1,21 @@
import {
BN,
encode,
// keccak256,
// decode,
// toChecksumAddress,
arrayify,
hexlify,
stripZeros,
Signature,
splitSignature,
// hexZeroPad,
} from '@harmony/crypto';
import { add0xToString } from '@harmony/utils';
import { TxParams } from './types';
import { recover } from './utils';
export const transactionFields = [
{ name: 'nonce', length: 32, fix: false },
{ name: 'gasPrice', length: 32, fix: false, transform: 'hex' },
{ name: 'gasLimit', length: 32, fix: false, transform: 'hex' },
{ name: 'to', length: 20, fix: true },
{ name: 'value', length: 32, fix: false, transform: 'hex' },
{ name: 'data', fix: false },
];
import { Messenger, RPCMethod } from '@harmony/network';
import { TxParams, TxStatus } from './types';
import { recover, transactionFields } from './utils';
class Transaction {
// private hash?: string;
messenger?: Messenger;
txStatus: TxStatus;
private id: string;
private from: string;
private nonce: number | string;
private to: string;
@ -39,7 +29,12 @@ class Transaction {
private signature: Signature;
// constructor
constructor(params?: TxParams) {
constructor(
params?: TxParams,
messenger?: Messenger,
txStatus = TxStatus.INTIALIZED,
) {
this.id = params ? params.id : '0x';
this.from = params ? params.from : '0x';
this.nonce = params ? params.nonce : 0;
this.gasPrice = params ? params.gasPrice : new BN(0);
@ -58,6 +53,8 @@ class Transaction {
recoveryParam: 0,
v: 0,
};
this.messenger = messenger;
this.txStatus = txStatus;
}
getRLPUnsigned(): [string, any[]] {
@ -126,6 +123,7 @@ class Transaction {
get txParams(): TxParams {
return {
id: this.id || '0x',
from: this.from || '',
nonce: this.nonce || 0,
gasPrice: this.gasPrice || new BN(0),
@ -140,6 +138,7 @@ class Transaction {
};
}
setParams(params: TxParams) {
this.id = params ? params.id : '0x';
this.from = params ? params.from : '0x';
this.nonce = params ? params.nonce : 0;
this.gasPrice = params ? params.gasPrice : new BN(0);
@ -158,12 +157,67 @@ class Transaction {
recoveryParam: 0,
v: 0,
};
if (this.txnHash !== '0x') {
this.setTxStatus(TxStatus.SIGNED);
} else {
this.setTxStatus(TxStatus.INTIALIZED);
}
}
map(fn: any) {
const newParams = fn(this.txParams);
this.setParams(newParams);
return this;
}
setTxStatus(txStatus: TxStatus): void {
this.txStatus = txStatus;
}
getTxStatus(): TxStatus {
return this.txStatus;
}
// get status
isInitialized(): boolean {
return this.getTxStatus() === TxStatus.INTIALIZED;
}
isSigned(): boolean {
return this.getTxStatus() === TxStatus.SIGNED;
}
isPending(): boolean {
return this.getTxStatus() === TxStatus.PENDING;
}
isRejected(): boolean {
return this.getTxStatus() === TxStatus.REJECTED;
}
isConfirmed(): boolean {
return this.getTxStatus() === TxStatus.CONFIRMED;
}
async sendTransaction(): Promise<[Transaction, string]> {
// TODO: we use eth RPC setting for now, incase we have other params, we should add here
if (this.txnHash === 'tx' || this.txnHash === undefined) {
throw new Error('Transaction not signed');
}
if (!this.messenger) {
throw new Error('Messenger not found');
}
const result = await this.messenger.send(
RPCMethod.SendTransaction,
this.txnHash,
);
// temporarilly hard coded
if (typeof result === 'string') {
this.id = result;
this.setTxStatus(TxStatus.PENDING);
return [this, result];
} else {
this.setTxStatus(TxStatus.REJECTED);
throw new Error('transaction failed');
}
}
}
export { Transaction };

@ -1,5 +1,6 @@
import { BN, Signature } from '@harmony/crypto';
export interface TxParams {
id: string;
from: string;
to: string;
nonce: number | string;
@ -12,3 +13,11 @@ export interface TxParams {
unsignedTxnHash: string;
signature: Signature;
}
export const enum TxStatus {
INTIALIZED = 'INITIALIZED',
SIGNED = 'SIGNED',
PENDING = 'PENDING',
CONFIRMED = 'CONFIRMED',
REJECTED = 'REJECTED',
}

@ -10,6 +10,15 @@ import {
} from '@harmony/crypto';
import { TxParams } from './types';
export const transactionFields = [
{ name: 'nonce', length: 32, fix: false },
{ name: 'gasPrice', length: 32, fix: false, transform: 'hex' },
{ name: 'gasLimit', length: 32, fix: false, transform: 'hex' },
{ name: 'to', length: 20, fix: true },
{ name: 'value', length: 32, fix: false, transform: 'hex' },
{ name: 'data', fix: false },
];
export const handleNumber = (value: string) => {
if (isHex(value) && value === '0x') {
return hexToNumber('0x00');
@ -37,6 +46,7 @@ export const recover = (rawTransaction: string) => {
}
const tx: TxParams = {
id: '0x',
from: '0x',
txnHash: '0x',
unsignedTxnHash: '0x',

@ -7,6 +7,7 @@
"include": ["src", "../../typings/**/*.d.ts"],
"references": [
{ "path": "../harmony-utils" },
{ "path": "../harmony-crypto" }
{ "path": "../harmony-crypto" },
{ "path": "../harmony-network" }
]
}

@ -1,4 +1,5 @@
export default [
'harmony-core',
'harmony-utils',
'harmony-crypto',
'harmony-account',

@ -1,4 +1,5 @@
module.exports = [
{ name: 'HarmonyJs', dest: 'harmony-core' },
{ name: 'HarmonyNetwork', dest: 'harmony-network' },
{ name: 'HarmonyUtils', dest: 'harmony-utils' },
{ name: 'HarmonyCrypto', dest: 'harmony-crypto' },

@ -1,4 +1,5 @@
const packages = [
'harmony-core',
'harmony-utils',
'harmony-crypto',
'harmony-account',

@ -1,6 +1,7 @@
{
"files": [],
"references": [
{ "path": "packages/harmony-core" },
{ "path": "packages/harmony-account" },
{ "path": "packages/harmony-crypto" },
{ "path": "packages/harmony-utils" },

Loading…
Cancel
Save