Compare commits
1 Commits
master
...
staking_tn
Author | SHA1 | Date |
---|---|---|
Raptor | 44d58f1ba1 | 4 years ago |
@ -0,0 +1,5 @@ |
||||
GENESIS_PRIV_KEY=45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e |
||||
HTTP_PROVIDER=http://localhost:9500 |
||||
CHAIN_TYPE=hmy |
||||
CHAIN_ID=2 |
||||
|
@ -0,0 +1,5 @@ |
||||
GENESIS_PRIV_KEY=45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e |
||||
HTTP_PROVIDER=http://localhost:9500 |
||||
CHAIN_TYPE=hmy |
||||
CHAIN_ID=2 |
||||
|
@ -1,169 +1,88 @@ |
||||
import fetch from 'jest-fetch-mock'; |
||||
import { woop, checkCalledMethod } from './woop'; |
||||
import {harmony} from './harmony'; |
||||
|
||||
import demoAccounts from '../fixtures/testAccount.json'; |
||||
import { RPCMethod } from '@woop-js/network'; |
||||
|
||||
const bc = woop.blockchain; |
||||
const bc = harmony.blockchain; |
||||
|
||||
const testAccount = demoAccounts.Accounts[1]; |
||||
|
||||
describe('e2e test blockchain', () => { |
||||
beforeEach(() => { |
||||
fetch.resetMocks(); |
||||
}); |
||||
// net_*
|
||||
it('should test net_peerCount', async () => { |
||||
fetch.mockResponseOnce( |
||||
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x0"}), |
||||
); |
||||
const peerCount = await bc.net_peerCount(); |
||||
expect(checkCalledMethod(0, RPCMethod.PeerCount)).toEqual(true); |
||||
expect(woop.utils.isHex(peerCount.result)).toEqual(true); |
||||
expect(harmony.utils.isHex(peerCount.result)).toEqual(true); |
||||
}); |
||||
|
||||
it('should test net_version', async () => { |
||||
fetch.mockResponseOnce( |
||||
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "5"}), |
||||
); |
||||
const netVersion = await bc.net_version(); |
||||
const versionNumber = parseInt(netVersion.result as string, 10); |
||||
expect(netVersion.result).toEqual(`${versionNumber}`); |
||||
expect(checkCalledMethod(0, RPCMethod.NetVersion)).toEqual(true); |
||||
}); |
||||
it('should test wiki_protocolVersion', async () => { |
||||
fetch.mockResponseOnce( |
||||
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x10"}), |
||||
); |
||||
it('should test hmy_protocolVersion', async () => { |
||||
const protocolVersion = await bc.getProtocolVersion(); |
||||
expect(woop.utils.isHex(protocolVersion.result)).toEqual(true); |
||||
expect(checkCalledMethod(0, RPCMethod.ProtocolVersion)).toEqual(true); |
||||
expect(harmony.utils.isHex(protocolVersion.result)).toEqual(true); |
||||
}); |
||||
|
||||
// block chain info
|
||||
it('should test wiki_blockNumber', async () => { |
||||
fetch.mockResponseOnce( |
||||
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x10"}), |
||||
); |
||||
it('should test hmy_blockNumber', async () => { |
||||
const res = await bc.getBlockNumber(); |
||||
expect(res.responseType).toEqual('raw'); |
||||
expect(woop.utils.isHex(res.result)).toEqual(true); |
||||
expect(checkCalledMethod(0, RPCMethod.BlockNumber)).toEqual(true); |
||||
expect(harmony.utils.isHex(res.result)).toEqual(true); |
||||
}); |
||||
|
||||
it('should test wiki_getBlockByNumber', async () => { |
||||
fetch.mockResponse( |
||||
JSON.stringify({ |
||||
"jsonrpc": "2.0", |
||||
"id": 1, |
||||
"result": { |
||||
"size": "0x1", |
||||
"difficulty": 5, |
||||
"extraData": "0x", |
||||
"gasLimit": "0x80", |
||||
"gasUsed": "0x40", |
||||
"hash": "0x8a3390ab500Fbca6514eB326d2fcD9B3BFCFbA7DA392593cB4885b8e3399a2D8", |
||||
"logsBloom": "0x0", |
||||
"miner": "one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3", |
||||
"mixHash": "0x3A7c1Ae14AfecFf55Da298F66b75F4FfB771c3EaBDeAa267FF33A77d4d0be220", |
||||
"nonce": 1, |
||||
"number": "0x1", |
||||
"parentHash": "0x7CebC07e456F0bCD09dbc9A6f271074d93E27B40A4C67Dcc402e6513e12B9aF9", |
||||
"receiptsRoot": "0x02B82e11eDC07775Dc6fCF706be2cdAF9165750Ea7bC1B3Eb48ea16Bb3072F4D", |
||||
"stateRoot": "0xAaDc89C8bA4e3fCfC140cFcc8D3efD3BE7a49ab31534A5a3F0E1DEA09aae0f4a", |
||||
"timestamp": "0x62c44c0a", |
||||
"transactionsRoot": "0xc4bfa888fDCC8ca70E2b0CcdCEcc2fF545acCC2D655Ba33DaF4aBc31cFDBd9Ac", |
||||
"uncles": [] |
||||
} |
||||
}), |
||||
); |
||||
const res = await bc.getBlockByNumber({ blockNumber: 'latest' }); |
||||
it('should test hmy_getBlockByNumber', async () => { |
||||
const res = await bc.getBlockByNumber({blockNumber: 'latest'}); |
||||
const size = res.result.size; |
||||
expect(res.responseType).toEqual('raw'); |
||||
expect(woop.utils.isHex(size)).toEqual(true); |
||||
expect(harmony.utils.isHex(size)).toEqual(true); |
||||
expect(checkBlockData(res.result)).toEqual(true); |
||||
const res2 = await bc.getBlockByNumber({ blockNumber: res.result.number }); |
||||
const res2 = await bc.getBlockByNumber({blockNumber: res.result.number}); |
||||
expect(res2.responseType).toEqual('raw'); |
||||
expect(woop.utils.isHex(res2.result.size)).toEqual(true); |
||||
expect(harmony.utils.isHex(res2.result.size)).toEqual(true); |
||||
expect(checkBlockData(res2.result)).toEqual(true); |
||||
const res3 = await bc.getBlockByNumber({ returnObject: true }); |
||||
const res3 = await bc.getBlockByNumber({returnObject: true}); |
||||
expect(res3.responseType).toEqual('raw'); |
||||
expect(checkBlockData(res3.result)).toEqual(true); |
||||
for(let i = 0; i < 3; i++) { |
||||
expect(checkCalledMethod(i, RPCMethod.GetBlockByNumber)).toEqual(true); |
||||
} |
||||
}); |
||||
|
||||
it('should test wiki_getBlockByHash', async () => { |
||||
fetch.mockResponse( |
||||
JSON.stringify({ |
||||
"jsonrpc": "2.0", |
||||
"id": 1, |
||||
"result": { |
||||
"size": "0x1", |
||||
"difficulty": 5, |
||||
"extraData": "0x", |
||||
"gasLimit": "0x80", |
||||
"gasUsed": "0x40", |
||||
"hash": "0x8a3390ab500Fbca6514eB326d2fcD9B3BFCFbA7DA392593cB4885b8e3399a2D8", |
||||
"logsBloom": "0x0", |
||||
"miner": "one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3", |
||||
"mixHash": "0x3A7c1Ae14AfecFf55Da298F66b75F4FfB771c3EaBDeAa267FF33A77d4d0be220", |
||||
"nonce": 1, |
||||
"number": "0x1", |
||||
"parentHash": "0x7CebC07e456F0bCD09dbc9A6f271074d93E27B40A4C67Dcc402e6513e12B9aF9", |
||||
"receiptsRoot": "0x02B82e11eDC07775Dc6fCF706be2cdAF9165750Ea7bC1B3Eb48ea16Bb3072F4D", |
||||
"stateRoot": "0xAaDc89C8bA4e3fCfC140cFcc8D3efD3BE7a49ab31534A5a3F0E1DEA09aae0f4a", |
||||
"timestamp": "0x62c44c0a", |
||||
"transactionsRoot": "0xc4bfa888fDCC8ca70E2b0CcdCEcc2fF545acCC2D655Ba33DaF4aBc31cFDBd9Ac", |
||||
"uncles": [] |
||||
} |
||||
}), |
||||
); |
||||
const latestBlock = await bc.getBlockByNumber({ blockNumber: 'latest' }); |
||||
const res = await bc.getBlockByHash({ blockHash: latestBlock.result.hash }); |
||||
it('should test hmy_getBlockByHash', async () => { |
||||
const latestBlock = await bc.getBlockByNumber({blockNumber: 'latest'}); |
||||
const res = await bc.getBlockByHash({blockHash: latestBlock.result.hash}); |
||||
expect(res.responseType).toEqual('raw'); |
||||
expect(latestBlock.result.hash).toEqual(res.result.hash); |
||||
expect(woop.utils.isHex(res.result.size)).toEqual(true); |
||||
expect(harmony.utils.isHex(res.result.size)).toEqual(true); |
||||
expect(checkBlockData(res.result)).toEqual(true); |
||||
expect(checkCalledMethod(0, RPCMethod.GetBlockByNumber)).toEqual(true); |
||||
expect(checkCalledMethod(1, RPCMethod.GetBlockByHash)).toEqual(true); |
||||
}); |
||||
|
||||
// account related
|
||||
it('should test wiki_getBalance', async () => { |
||||
fetch.mockResponseOnce( |
||||
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x10"}), |
||||
); |
||||
const balance = await bc.getBalance({ address: testAccount.Address }); |
||||
expect(woop.utils.isHex(balance.result)).toEqual(true); |
||||
expect(checkCalledMethod(0, RPCMethod.GetBalance)).toEqual(true); |
||||
it('should test hmy_getBalance', async () => { |
||||
const balance = await bc.getBalance({address: testAccount.Address}); |
||||
expect(harmony.utils.isHex(balance.result)).toEqual(true); |
||||
}); |
||||
}); |
||||
|
||||
function checkBlockData(data: any) { |
||||
return woop.utils.validateArgs( |
||||
return harmony.utils.validateArgs( |
||||
data, |
||||
{ |
||||
difficulty: [woop.utils.isNumber], |
||||
difficulty: [harmony.utils.isNumber], |
||||
// tslint:disable-next-line: no-shadowed-variable
|
||||
extraData: [(data: any) => data === '0x' || woop.utils.isHex(data)], |
||||
gasLimit: [woop.utils.isHex], |
||||
gasUsed: [woop.utils.isHex], |
||||
hash: [woop.utils.isHash], |
||||
logsBloom: [woop.utils.isHex], |
||||
miner: [woop.utils.isBech32Address], |
||||
mixHash: [woop.utils.isHash], |
||||
nonce: [woop.utils.isNumber], |
||||
number: [woop.utils.isHex], |
||||
parentHash: [woop.utils.isHash], |
||||
receiptsRoot: [woop.utils.isHash], |
||||
size: [woop.utils.isHex], |
||||
stateRoot: [woop.utils.isHash], |
||||
timestamp: [woop.utils.isHex], |
||||
transactionsRoot: [woop.utils.isHash], |
||||
uncles: [woop.utils.isArray], |
||||
extraData: [(data: any) => data === '0x' || harmony.utils.isHex(data)], |
||||
gasLimit: [harmony.utils.isHex], |
||||
gasUsed: [harmony.utils.isHex], |
||||
hash: [harmony.utils.isHash], |
||||
logsBloom: [harmony.utils.isHex], |
||||
miner: [harmony.utils.isAddress], |
||||
mixHash: [harmony.utils.isHash], |
||||
nonce: [harmony.utils.isNumber], |
||||
number: [harmony.utils.isHex], |
||||
parentHash: [harmony.utils.isHash], |
||||
receiptsRoot: [harmony.utils.isHash], |
||||
size: [harmony.utils.isHex], |
||||
stateRoot: [harmony.utils.isHash], |
||||
timestamp: [harmony.utils.isHex], |
||||
transactionsRoot: [harmony.utils.isHash], |
||||
uncles: [harmony.utils.isArray], |
||||
}, |
||||
{ transactions: [woop.utils.isArray] }, |
||||
{transactions: [harmony.utils.isArray]}, |
||||
); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,29 @@ |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import { Harmony } from '@harmony-js/core'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import { ChainType } from '@harmony-js/utils'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import { Account } from '@harmony-js/account'; |
||||
|
||||
const CHAIN_ID: number = parseInt(process.env.CHAIN_ID as string, 10); |
||||
const CHAIN_TYPE: string = process.env.CHAIN_TYPE as string; |
||||
const HTTP_PROVIDER: string = process.env.HTTP_PROVIDER as string; |
||||
const GENESIS_PRIV_KEY: string = process.env.GENESIS_PRIV_KEY as string; |
||||
|
||||
let chainType: ChainType = ChainType.Harmony; |
||||
|
||||
if (CHAIN_TYPE === 'hmy') { |
||||
chainType = ChainType.Harmony; |
||||
} else if (CHAIN_TYPE === 'eth') { |
||||
chainType = ChainType.Ethereum; |
||||
} |
||||
|
||||
export const harmony: Harmony = new Harmony(HTTP_PROVIDER, { |
||||
chainId: CHAIN_ID, |
||||
chainType, |
||||
chainUrl: HTTP_PROVIDER, |
||||
}); |
||||
|
||||
export const myAccount: Account = harmony.wallet.addByPrivateKey( |
||||
GENESIS_PRIV_KEY, |
||||
); |
@ -1,39 +0,0 @@ |
||||
import fetch from 'jest-fetch-mock'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import { Woop } from '@woop-js/core'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import { ChainType } from '@woop-js/utils'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import { Account } from '@woop-js/account'; |
||||
|
||||
const CHAIN_ID: number = 2; |
||||
const CHAIN_TYPE: string = 'wiki'; |
||||
const HTTP_PROVIDER: string = 'http://localhost:9500'; |
||||
const GENESIS_PRIV_KEY: string = '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e'; |
||||
|
||||
let chainType: ChainType = ChainType.Woop; |
||||
|
||||
if (CHAIN_TYPE === 'wiki') { |
||||
chainType = ChainType.Woop; |
||||
} else if (CHAIN_TYPE === 'eth') { |
||||
chainType = ChainType.Ethereum; |
||||
} |
||||
|
||||
export const woop: Woop = new Woop(HTTP_PROVIDER, { |
||||
chainId: CHAIN_ID, |
||||
chainType, |
||||
chainUrl: HTTP_PROVIDER, |
||||
}); |
||||
|
||||
export const myAccount: Account = woop.wallet.addByPrivateKey( |
||||
GENESIS_PRIV_KEY, |
||||
); |
||||
|
||||
export function checkCalledMethod(i: number, s: string) { |
||||
let params: (string | undefined) = fetch.mock.calls[i][1]?.body?.toString(); |
||||
if (params) { |
||||
let method: string = JSON.parse(params).method; |
||||
return method === s; |
||||
} |
||||
return false; |
||||
} |
@ -0,0 +1,134 @@ |
||||
# Quick start |
||||
|
||||
1. You need Harmony local testnet running. |
||||
instruction here:[harmony-one/harmony](https://github.com/harmony-one/harmony) |
||||
2. Run this example under nodejs enviorment. |
||||
|
||||
```javascript |
||||
// import or require Harmony class |
||||
const { Harmony } = require('@harmony-js/core'); |
||||
|
||||
// import or require settings |
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
|
||||
// 1. initialize the Harmony instance |
||||
|
||||
const harmony = new Harmony( |
||||
// rpc url |
||||
'http://localhost:9500', |
||||
{ |
||||
// chainType set to Harmony |
||||
chainType: ChainType.Harmony, |
||||
// chainType set to HmyLocal |
||||
chainId: ChainID.HmyLocal, |
||||
}, |
||||
); |
||||
|
||||
// 2. get wallet ready |
||||
// specify the privateKey |
||||
const privateKey = '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e'; |
||||
// add privateKey to wallet |
||||
const sender = harmony.wallet.addByPrivateKey(privateKey); |
||||
|
||||
// 3. get sharding info |
||||
async function setSharding() { |
||||
// Harmony is a sharded blockchain, each endpoint have sharding structure, |
||||
// However sharding structure is different between mainnet, testnet and local testnet |
||||
// We need to get sharding info before doing cross-shard transaction |
||||
const res = await harmony.blockchain.getShardingStructure(); |
||||
harmony.shardingStructures(res.result); |
||||
} |
||||
|
||||
// 4. get transaction payload ready |
||||
|
||||
async function transfer() { |
||||
// run set sharding first, if you want to make a cross-shard transaction |
||||
await setSharding(); |
||||
|
||||
const txn = harmony.transactions.newTx({ |
||||
// token send to |
||||
to: 'one166axnkjmghkf3df7xfvd0hn4dft8kemrza4cd2', |
||||
// amount to send |
||||
value: '10000', |
||||
// gas limit, you can use string |
||||
gasLimit: '210000', |
||||
// send token from shardID |
||||
shardID: 0, |
||||
// send token to toShardID |
||||
toShardID: 0, |
||||
// gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN |
||||
gasPrice: new harmony.utils.Unit('100').asGwei().toWei(), |
||||
}); |
||||
|
||||
// sign the transaction use wallet; |
||||
|
||||
const signedTxn = await harmony.wallet.signTransaction(txn); |
||||
|
||||
// Now you can use `Transaction.observed()` to listen events |
||||
|
||||
signedTxn |
||||
.observed() |
||||
.on('transactionHash', (txnHash) => { |
||||
console.log(''); |
||||
console.log('--- hash ---'); |
||||
console.log(''); |
||||
console.log(txnHash); |
||||
console.log(''); |
||||
}) |
||||
.on('receipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- receipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('cxReceipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- cxReceipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('error', (error) => { |
||||
console.log(''); |
||||
console.log('--- error ---'); |
||||
console.log(''); |
||||
console.log(error); |
||||
console.log(''); |
||||
}); |
||||
|
||||
// send the txn, get [Transaction, transactionHash] as result |
||||
|
||||
const [sentTxn, txnHash] = await signedTxn.sendTransaction(); |
||||
|
||||
// to confirm the result if it is already there |
||||
|
||||
const confiremdTxn = await sentTxn.confirm(txnHash); |
||||
|
||||
// if the transactino is cross-shard transaction |
||||
if (!confiremdTxn.isCrossShard()) { |
||||
if (confiremdTxn.isConfirmed()) { |
||||
console.log('--- Result ---'); |
||||
console.log(''); |
||||
console.log('Normal transaction'); |
||||
console.log(`${txnHash} is confirmed`); |
||||
console.log(''); |
||||
process.exit(); |
||||
} |
||||
} |
||||
if (confiremdTxn.isConfirmed() && confiremdTxn.isCxConfirmed()) { |
||||
console.log('--- Result ---'); |
||||
console.log(''); |
||||
console.log('Cross-Shard transaction'); |
||||
console.log(`${txnHash} is confirmed`); |
||||
console.log(''); |
||||
process.exit(); |
||||
} |
||||
} |
||||
|
||||
transfer(); |
||||
``` |
||||
|
||||
# More examples |
||||
|
||||
* [dapp-examples](https://github.com/harmony-one/dapp-examples) |
@ -0,0 +1,31 @@ |
||||
const { Harmony } = require('@harmony-js/core'); |
||||
|
||||
// import or require settings
|
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
|
||||
const URL_TESTNET = `https://api.s0.b.hmny.io`; |
||||
const URL_MAINNET = `https://api.s0.t.hmny.io`; |
||||
|
||||
// 1. initialize the Harmony instance
|
||||
|
||||
const harmony = new Harmony( |
||||
// rpc url
|
||||
URL_TESTNET, |
||||
{ |
||||
// chainType set to Harmony
|
||||
chainType: ChainType.Harmony, |
||||
// chainType set to HmyLocal
|
||||
chainId: ChainID.HmyTestnet, |
||||
}, |
||||
); |
||||
|
||||
harmony.blockchain |
||||
.getBalance({ |
||||
address: `one1vjywuur8ckddmc4dsyx6qdgf590eu07ag9fg4a`, |
||||
}) |
||||
.then((res) => { |
||||
console.log(new harmony.utils.Unit(res.result).asWei().toEther()); |
||||
}) |
||||
.catch((err) => { |
||||
console.log(err); |
||||
}); |
@ -0,0 +1,45 @@ |
||||
// import the Account class
|
||||
const { Account } = require('@harmony-js/account'); |
||||
|
||||
// suppose we have an account
|
||||
const myPrivateKey = '0x831f0b3ff835d5ec4602878742fda25591b6d3bb2731366621ac83a05216bec8'; |
||||
const myAccountWithMyPrivateKey = new Account(myPrivateKey); |
||||
|
||||
// suppose we have a password, and we want to encrypt the account above
|
||||
const myStrongPassword = '123'; |
||||
|
||||
async function encryptAndDecrypt(password) { |
||||
// we get the privateKey before encrypted as comparison
|
||||
const unencryptedPrivateKey = myAccountWithMyPrivateKey.privateKey; |
||||
|
||||
// export the account to keyStore string, which will make the privateKey encrpyted
|
||||
const keyStoreFile = await myAccountWithMyPrivateKey.toFile(password); |
||||
// exported keyStoreFile
|
||||
console.log({ keyStoreFile }); |
||||
// see if the account is encrypted
|
||||
console.log(`Is this account encrypted? \n ${myAccountWithMyPrivateKey.encrypted}`); |
||||
// keystore file should be equal to encrypted privateKey
|
||||
console.log( |
||||
`Is privateKey equal to keyStore string? \n ${keyStoreFile === |
||||
myAccountWithMyPrivateKey.privateKey}`,
|
||||
); |
||||
} |
||||
|
||||
encryptAndDecrypt(myStrongPassword); |
||||
|
||||
// suppose we have keyStorefile, in this example, we just use same password and keystore string encrypted above
|
||||
const someKeyStoreFile = |
||||
'{"version":3,"id":"38326265-6165-4961-a338-353063643962","address":"1cc87010760602a576455d6d2f03a3bf92d2c2ca","crypto":{"ciphertext":"a4ee9120b27ba66fb9d3fabd6372fa3a11060cf439a6a1777ced1e6253de8c29","cipherparams":{"iv":"c18772a882ac461fffd1971e9ec57861"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"salt":"da4efedeca407279be65e02fc94b7c4b7c74c3396447c71e659c74a73a5d9131","n":8192,"r":8,"p":1,"dklen":32},"mac":"547ee6616dcdf424273c113ceb00728ccdda17ff6449f2cb84a1a8352c87b4e6"}}'; |
||||
|
||||
async function importKeyStoreFileAndDecrypt(keyStoreFile, password) { |
||||
// import keyStore string and provide the password, remember to make a new Account first
|
||||
const importedAccount = await Account.new().fromFile(keyStoreFile, password); |
||||
// the account should decypted which `Account.encrypted` is false
|
||||
console.log(`Is this account encrypted? \n ${importedAccount.encrypted}`); |
||||
// see if the privatekey is equal to unencrypted one?
|
||||
console.log( |
||||
`Is the account recovered from keystore? \n ${importedAccount.privateKey === myPrivateKey}`, |
||||
); |
||||
} |
||||
|
||||
importKeyStoreFileAndDecrypt(someKeyStoreFile, myStrongPassword); |
@ -0,0 +1,14 @@ |
||||
{ |
||||
"name": "example", |
||||
"version": "1.0.0", |
||||
"description": "", |
||||
"main": "test.js", |
||||
"scripts": { |
||||
"test": "echo \"Error: no test specified\" && exit 1" |
||||
}, |
||||
"author": "", |
||||
"license": "ISC", |
||||
"dependencies": { |
||||
"@harmony-js/core": "^0.1.41" |
||||
} |
||||
} |
@ -0,0 +1,79 @@ |
||||
const { Harmony } = require('@harmony-js/core'); |
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
const { |
||||
StakingTransaction, |
||||
CreateValidator, |
||||
Delegate, |
||||
Undelegate, |
||||
CollectRewards, |
||||
StakingFactory, |
||||
} = require('@harmony-js/staking'); |
||||
|
||||
const harmony = new Harmony('http://localhost:9500', { |
||||
chainId: ChainID.HmyLocal, |
||||
chainType: ChainType.Harmony, |
||||
}); |
||||
|
||||
const stakingTxn = harmony.stakings |
||||
.createValidator({ |
||||
validatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9', |
||||
description: { |
||||
name: 'Alice', |
||||
identity: 'alice', |
||||
website: 'alice.harmony.one', |
||||
securityContact: 'Bob', |
||||
details: "Don't mess with me!!!", |
||||
}, |
||||
commissionRate: { |
||||
rate: '0.1', |
||||
maxRate: '0.9', |
||||
maxChangeRate: '0.05', |
||||
}, |
||||
minSelfDelegation: '0xa', |
||||
maxTotalDelegation: '0x0bb8', |
||||
slotPubKeys: [ |
||||
'0xb9486167ab9087ab818dc4ce026edb5bf216863364c32e42df2af03c5ced1ad181e7d12f0e6dd5307a73b62247608611', |
||||
], |
||||
amount: '0x64', |
||||
}) |
||||
.setTxParams({ |
||||
nonce: '0x2', |
||||
gasPrice: '0x', |
||||
gasLimit: '0x64', |
||||
chainId: 0, |
||||
}) |
||||
.build(); |
||||
|
||||
stakingTxn |
||||
.sendTransaction() |
||||
.then(([stakingTxn, res]) => { |
||||
console.log(res); |
||||
}) |
||||
.catch((err) => { |
||||
console.log(err); |
||||
}); |
||||
|
||||
// const delegateMsg = Delegate({
|
||||
// delegatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// validatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// amount: '0xa',
|
||||
// });
|
||||
|
||||
// const stakingTxn = StakingTransaction({
|
||||
// directive: '0x2',
|
||||
// stakeMsg: delegateMsg,
|
||||
// nonce: '0x2',
|
||||
// gasPrice: '0x',
|
||||
// gasLimit: '0x64',
|
||||
// chainId: 0,
|
||||
// from: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// });
|
||||
|
||||
// stakingTxn
|
||||
// .sendTransaction()
|
||||
// .then((stakingTxn, res) => {
|
||||
// console.log(res);
|
||||
// })
|
||||
// .catch((err) => {
|
||||
// console.log(err);
|
||||
// });
|
@ -0,0 +1,172 @@ |
||||
const { Harmony } = require('@harmony-js/core'); |
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
const { Delegate, StakingTransaction, StakingFactory } = require('@harmony-js/staking'); //../packages/harmony-staking
|
||||
const { TxStatus } = require('@harmony-js/transaction'); |
||||
|
||||
const LOCALNET = `http://localhost:9500`; |
||||
|
||||
// 1. initialize the Harmony instance
|
||||
|
||||
const harmony = new Harmony( |
||||
// rpc url
|
||||
LOCALNET, |
||||
{ |
||||
// chainType set to Harmony
|
||||
chainType: ChainType.Harmony, |
||||
// chainType set to HmyLocal
|
||||
chainId: ChainID.HmyLocal, |
||||
}, |
||||
); |
||||
|
||||
// 2. get wallet ready
|
||||
|
||||
// one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
// surge welcome lion goose gate consider taste injury health march debris kick
|
||||
|
||||
// add privateKey to wallet
|
||||
const private = '63e35b761e9df0d50ddcdaa8e33c235b60c991bfed22925a12768b0c08ef822f'; |
||||
// one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy
|
||||
const sender = harmony.wallet.addByPrivateKey(private); |
||||
console.log(sender.address); |
||||
// const phrase =
|
||||
// 'genius cable radar memory high catch blossom correct middle wish gentle
|
||||
// fiscal';
|
||||
|
||||
// one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
// surge welcome lion goose gate consider taste injury health march debris kick
|
||||
|
||||
// add privateKey to wallet
|
||||
// const sender = harmony.wallet.addByMnemonic(phrase);
|
||||
// let r =
|
||||
// '0xf8f180f8a4940b585f8daefbc68a311fbd4cb20d9174ad174016f83885416c69636585616c69636591616c6963652e6861726d6f6e792e6f6e6583426f6295446f6e2774206d6573732077697468206d65212121ddc988016345785d8a0000c9880c7d713b49da0000c887b1a2bc2ec500000a820bb8f1b0b9486167ab9087ab818dc4ce026edb5bf216863364c32e42df2af03c5ced1ad181e7d12f0e6dd5307a73b6224760861164008080830927c028a064b1b835f5b70a72228920db24e44c0a57d954c1d3dcac3b33c79d9593f96191a05577fd05064a37043a33ff7febb67ab126a8e1f0b67c92b7cab793a87ddf2c82';
|
||||
|
||||
// const delegateMsg = new Delegate(
|
||||
// 'one1pf75h0t4am90z8uv3y0dgunfqp4lj8wr3t5rsp', // from delegate command.
|
||||
// 'one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy', // fd416cb87dcf8ed187e85545d7734a192fc8e976f5b540e9e21e896ec2bc25c3
|
||||
// '0xde0b6b3a7640000', // 0x56BC75E2D63100000
|
||||
// );
|
||||
|
||||
// // one12fuf7x9rgtdgqg7vgq0962c556m3p7afsxgvll;
|
||||
|
||||
// const stakingTxn = new StakingTransaction(
|
||||
// '0x2',
|
||||
// delegateMsg,
|
||||
// '0x2',
|
||||
// '0x',
|
||||
// '0x0927c0',
|
||||
// ChainID.HmyLocal,
|
||||
// );
|
||||
|
||||
const stakingTxn = harmony.stakings |
||||
.delegate({ |
||||
delegatorAddress: 'one1pf75h0t4am90z8uv3y0dgunfqp4lj8wr3t5rsp', |
||||
validatorAddress: 'one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy', |
||||
amount: '0xde0b6b3a7640000', |
||||
}) |
||||
.setTxParams({ nonce: '0x2', gasPrice: '0x', gasLimit: '0x0927c0', chainId: ChainID.HmyLocal }) |
||||
.build(); |
||||
|
||||
// 3. get sharding info
|
||||
async function setSharding() { |
||||
// Harmony is a sharded blockchain, each endpoint have sharding structure,
|
||||
// However sharding structure is different between mainnet, testnet and local
|
||||
// testnet We need to get sharding info before doing cross-shard transaction
|
||||
const res = await harmony.blockchain.getShardingStructure(); |
||||
harmony.shardingStructures(res.result); |
||||
} |
||||
|
||||
async function execute() { |
||||
await setSharding(); |
||||
|
||||
const signedTxn = await harmony.wallet.signStaking(stakingTxn); |
||||
signedTxn |
||||
.observed() |
||||
.on('transactionHash', (txnHash) => { |
||||
console.log(''); |
||||
console.log('--- hash ---'); |
||||
console.log(''); |
||||
console.log(txnHash); |
||||
console.log(''); |
||||
}) |
||||
.on('receipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- receipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('cxReceipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- cxReceipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('error', (error) => { |
||||
console.log(''); |
||||
console.log('--- error ---'); |
||||
console.log(''); |
||||
console.log(error); |
||||
console.log(''); |
||||
}); |
||||
|
||||
// console.log(signedTxn);
|
||||
|
||||
const [sentTxn, txnHash] = await signedTxn.sendTransaction(); |
||||
// signedTxn.sendTransaction()
|
||||
// .then(res => {
|
||||
// console.log(res);
|
||||
// })
|
||||
// .catch(err => {
|
||||
// console.log(err);
|
||||
// });
|
||||
|
||||
// to confirm the result if it is already there
|
||||
// console.log(txnHash);
|
||||
console.log(sentTxn); |
||||
|
||||
const confiremdTxn = await sentTxn.confirm(txnHash); |
||||
|
||||
// if the transactino is cross-shard transaction
|
||||
if (confiremdTxn.isConfirmed()) { |
||||
console.log('--- Result ---'); |
||||
console.log(''); |
||||
console.log('Normal transaction'); |
||||
console.log(`${txnHash} is confirmed`); |
||||
console.log(''); |
||||
process.exit(); |
||||
} |
||||
// if (confiremdTxn.isConfirmed() && confiremdTxn.isCxConfirmed()) {
|
||||
// console.log('--- Result ---');
|
||||
// console.log('');
|
||||
// console.log('Cross-Shard transaction');
|
||||
// console.log(`${txnHash} is confirmed`);
|
||||
// console.log('');
|
||||
// process.exit();
|
||||
// }
|
||||
} |
||||
execute(); |
||||
// const delegateMsg = Delegate({
|
||||
// delegatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// validatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// amount: '0xa',
|
||||
// });
|
||||
|
||||
// const stakingTxn = StakingTransaction({
|
||||
// directive: '0x2',
|
||||
// stakeMsg: delegateMsg,
|
||||
// nonce: '0x2',
|
||||
// gasPrice: '0x',
|
||||
// gasLimit: '0x64',
|
||||
// chainId: 0,
|
||||
// from: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// });
|
||||
|
||||
// stakingTxn
|
||||
// .sendTransaction()
|
||||
// .then((stakingTxn, res) => {
|
||||
// console.log(res);
|
||||
// })
|
||||
// .catch((err) => {
|
||||
// console.log(err);
|
||||
// });
|
@ -0,0 +1,74 @@ |
||||
const { Harmony } = require('@harmony-js/core'); |
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
const { StakingFactory } = require('@harmony-js/staking'); |
||||
|
||||
const harmony = new Harmony('http://localhost:9500', { |
||||
chainId: ChainID.HmyLocal, |
||||
chainType: ChainType.Harmony, |
||||
}); |
||||
|
||||
const createMsg = { |
||||
validatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9', |
||||
description: { |
||||
name: 'Alice', |
||||
identity: 'alice', |
||||
website: 'alice.harmony.one', |
||||
securityContact: 'Bob', |
||||
details: "Don't mess with me!!!", |
||||
}, |
||||
commissionRate: { |
||||
rate: '0.1', |
||||
maxRate: '0.9', |
||||
maxChangeRate: '0.05', |
||||
}, |
||||
minSelfDelegation: '0xa', |
||||
maxTotalDelegation: '0x0bb8', |
||||
slotPubKeys: [ |
||||
'0xb9486167ab9087ab818dc4ce026edb5bf216863364c32e42df2af03c5ced1ad181e7d12f0e6dd5307a73b62247608611', |
||||
], |
||||
amount: '0x64', |
||||
}; |
||||
|
||||
const stakingTxn = harmony.stakings |
||||
.createValidator(createMsg) |
||||
.setTxParams({ |
||||
nonce: '0x2', |
||||
gasPrice: '0x', |
||||
gasLimit: '0x64', |
||||
chainId: 0, |
||||
}) |
||||
.build(); |
||||
|
||||
stakingTxn |
||||
.sendTransaction() |
||||
.then(([stakingTxn, res]) => { |
||||
console.log(res); |
||||
}) |
||||
.catch((err) => { |
||||
console.log(err); |
||||
}); |
||||
|
||||
// const delegateMsg = Delegate({
|
||||
// delegatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// validatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// amount: '0xa',
|
||||
// });
|
||||
|
||||
// const stakingTxn = StakingTransaction({
|
||||
// directive: '0x2',
|
||||
// stakeMsg: delegateMsg,
|
||||
// nonce: '0x2',
|
||||
// gasPrice: '0x',
|
||||
// gasLimit: '0x64',
|
||||
// chainId: 0,
|
||||
// from: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// });
|
||||
|
||||
// stakingTxn
|
||||
// .sendTransaction()
|
||||
// .then((stakingTxn, res) => {
|
||||
// console.log(res);
|
||||
// })
|
||||
// .catch((err) => {
|
||||
// console.log(err);
|
||||
// });
|
@ -0,0 +1,177 @@ |
||||
const { Harmony } = require('@harmony-js/core'); |
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
const { |
||||
Description, |
||||
Decimal, |
||||
CommissionRate, |
||||
StakingTransaction, |
||||
CreateValidator, |
||||
} = require('@harmony-js/staking'); //../packages/harmony-staking
|
||||
const { TxStatus } = require('@harmony-js/transaction'); |
||||
|
||||
const LOCALNET = `http://localhost:9500`; |
||||
|
||||
// 1. initialize the Harmony instance
|
||||
|
||||
const harmony = new Harmony( |
||||
// rpc url
|
||||
LOCALNET, |
||||
{ |
||||
// chainType set to Harmony
|
||||
chainType: ChainType.Harmony, |
||||
// chainType set to HmyLocal
|
||||
chainId: ChainID.HmyLocal, |
||||
}, |
||||
); |
||||
|
||||
// 2. get wallet ready
|
||||
|
||||
// one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
// surge welcome lion goose gate consider taste injury health march debris kick
|
||||
|
||||
// add privateKey to wallet
|
||||
const private = 'fd416cb87dcf8ed187e85545d7734a192fc8e976f5b540e9e21e896ec2bc25c3'; |
||||
const sender = harmony.wallet.addByPrivateKey(private); |
||||
// const phrase =
|
||||
// 'genius cable radar memory high catch blossom correct middle wish gentle
|
||||
// fiscal';
|
||||
|
||||
// one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
// surge welcome lion goose gate consider taste injury health march debris kick
|
||||
|
||||
// add privateKey to wallet
|
||||
// const sender = harmony.wallet.addByMnemonic(phrase);
|
||||
|
||||
const stakingTxn = harmony.stakings |
||||
.createValidator({ |
||||
validatorAddress: 'one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy', |
||||
description: { |
||||
name: 'Alice', |
||||
identity: 'alice', |
||||
website: 'alice.harmony.one', |
||||
securityContact: 'Bob', |
||||
details: "Don't mess with me!!", |
||||
}, |
||||
commissionRate: { rate: '0.1', maxRate: '0.9', maxChangeRate: '0.05' }, |
||||
minSelfDelegation: '0x8AC7230489E80000', |
||||
maxTotalDelegation: '0xA2A15D09519BE00000', |
||||
slotPubKeys: [ |
||||
'0xb9486167ab9087ab818dc4ce026edb5bf216863364c32e42df2af03c5ced1ad181e7d12f0e6dd5307a73b62247608611', |
||||
], |
||||
amount: '0x56BC75E2D63100000', |
||||
}) |
||||
.setTxParams({ |
||||
nonce: '0x2', |
||||
gasPrice: '0x', |
||||
gasLimit: '0x0927c0', |
||||
chainId: ChainID.HmyLocal, |
||||
}) |
||||
.build(); |
||||
|
||||
// 3. get sharding info
|
||||
async function setSharding() { |
||||
// Harmony is a sharded blockchain, each endpoint have sharding structure,
|
||||
// However sharding structure is different between mainnet, testnet and local
|
||||
// testnet We need to get sharding info before doing cross-shard transaction
|
||||
const res = await harmony.blockchain.getShardingStructure(); |
||||
harmony.shardingStructures(res.result); |
||||
} |
||||
|
||||
async function execute() { |
||||
await setSharding(); |
||||
|
||||
const signedTxn = await harmony.wallet.signStaking(stakingTxn); |
||||
signedTxn |
||||
.observed() |
||||
.on('transactionHash', (txnHash) => { |
||||
console.log(''); |
||||
console.log('--- hash ---'); |
||||
console.log(''); |
||||
console.log(txnHash); |
||||
console.log(''); |
||||
}) |
||||
.on('receipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- receipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('cxReceipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- cxReceipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('error', (error) => { |
||||
console.log(''); |
||||
console.log('--- error ---'); |
||||
console.log(''); |
||||
console.log(error); |
||||
console.log(''); |
||||
}); |
||||
|
||||
// console.log(signedTxn);
|
||||
|
||||
const [sentTxn, txnHash] = await signedTxn.sendTransaction(); |
||||
// signedTxn
|
||||
// .sendTransaction()
|
||||
// .then((res) => {
|
||||
// console.log(res);
|
||||
// })
|
||||
// .catch((err) => {
|
||||
// console.log(err);
|
||||
// });
|
||||
|
||||
// to confirm the result if it is already there
|
||||
console.log(txnHash); |
||||
console.log(sentTxn); |
||||
|
||||
const confiremdTxn = await sentTxn.confirm(txnHash); |
||||
|
||||
// if the transactino is cross-shard transaction
|
||||
// if (!confiremdTxn.isCrossShard()) {
|
||||
if (confiremdTxn.isConfirmed()) { |
||||
console.log('--- Result ---'); |
||||
console.log(''); |
||||
console.log('Staking transaction'); |
||||
console.log(`${txnHash} is confirmed`); |
||||
console.log(''); |
||||
process.exit(); |
||||
} |
||||
// }
|
||||
// if (confiremdTxn.isConfirmed() && confiremdTxn.isCxConfirmed()) {
|
||||
// console.log('--- Result ---');
|
||||
// console.log('');
|
||||
// console.log('Cross-Shard transaction');
|
||||
// console.log(`${txnHash} is confirmed`);
|
||||
// console.log('');
|
||||
// process.exit();
|
||||
// }
|
||||
} |
||||
execute(); |
||||
// const delegateMsg = Delegate({
|
||||
// delegatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// validatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// amount: '0xa',
|
||||
// });
|
||||
|
||||
// const stakingTxn = StakingTransaction({
|
||||
// directive: '0x2',
|
||||
// stakeMsg: delegateMsg,
|
||||
// nonce: '0x2',
|
||||
// gasPrice: '0x',
|
||||
// gasLimit: '0x64',
|
||||
// chainId: 0,
|
||||
// from: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// });
|
||||
|
||||
// stakingTxn
|
||||
// .sendTransaction()
|
||||
// .then((stakingTxn, res) => {
|
||||
// console.log(res);
|
||||
// })
|
||||
// .catch((err) => {
|
||||
// console.log(err);
|
||||
// });
|
@ -0,0 +1,173 @@ |
||||
const { Harmony } = require('@harmony-js/core'); |
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
const { Undelegate, StakingTransaction, StakingFactory } = require('@harmony-js/staking'); //../packages/harmony-staking
|
||||
const { TxStatus } = require('@harmony-js/transaction'); |
||||
|
||||
const LOCALNET = `http://localhost:9500`; |
||||
|
||||
// 1. initialize the Harmony instance
|
||||
|
||||
const harmony = new Harmony( |
||||
// rpc url
|
||||
LOCALNET, |
||||
{ |
||||
// chainType set to Harmony
|
||||
chainType: ChainType.Harmony, |
||||
// chainType set to HmyLocal
|
||||
chainId: ChainID.HmyLocal, |
||||
}, |
||||
); |
||||
|
||||
// 2. get wallet ready
|
||||
|
||||
// one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
// surge welcome lion goose gate consider taste injury health march debris kick
|
||||
|
||||
// add privateKey to wallet
|
||||
const private = '63e35b761e9df0d50ddcdaa8e33c235b60c991bfed22925a12768b0c08ef822f'; |
||||
const sender = harmony.wallet.addByPrivateKey(private); |
||||
// const phrase =
|
||||
// 'genius cable radar memory high catch blossom correct middle wish gentle
|
||||
// fiscal';
|
||||
|
||||
// one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
// surge welcome lion goose gate consider taste injury health march debris kick
|
||||
|
||||
// add privateKey to wallet
|
||||
// const sender = harmony.wallet.addByMnemonic(phrase);
|
||||
// let r =
|
||||
// '0xf8f180f8a4940b585f8daefbc68a311fbd4cb20d9174ad174016f83885416c69636585616c69636591616c6963652e6861726d6f6e792e6f6e6583426f6295446f6e2774206d6573732077697468206d65212121ddc988016345785d8a0000c9880c7d713b49da0000c887b1a2bc2ec500000a820bb8f1b0b9486167ab9087ab818dc4ce026edb5bf216863364c32e42df2af03c5ced1ad181e7d12f0e6dd5307a73b6224760861164008080830927c028a064b1b835f5b70a72228920db24e44c0a57d954c1d3dcac3b33c79d9593f96191a05577fd05064a37043a33ff7febb67ab126a8e1f0b67c92b7cab793a87ddf2c82';
|
||||
|
||||
// const undelegateMsg = new Undelegate(
|
||||
// 'one1pf75h0t4am90z8uv3y0dgunfqp4lj8wr3t5rsp', // signed should match this
|
||||
// 'one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy',
|
||||
// '0x16345785d8a0000',
|
||||
// );
|
||||
|
||||
// const stakingTxn = new StakingTransaction(
|
||||
// '0x3',
|
||||
// undelegateMsg,
|
||||
// '0x2',
|
||||
// '0x',
|
||||
// '0x0927c0',
|
||||
// ChainID.HmyLocal,
|
||||
// );
|
||||
|
||||
const stakingTxn = harmony.stakings |
||||
.undelegate({ |
||||
delegatorAddress: 'one1pf75h0t4am90z8uv3y0dgunfqp4lj8wr3t5rsp', |
||||
validatorAddress: 'one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy', |
||||
amount: '0x16345785d8a0000', |
||||
}) |
||||
.setTxParams({ |
||||
nonce: '0x2', |
||||
gasPrice: '0x', |
||||
gasLimit: '0x0927c0', |
||||
chainId: ChainID.HmyLocal, |
||||
}) |
||||
.build(); |
||||
|
||||
// 3. get sharding info
|
||||
async function setSharding() { |
||||
// Harmony is a sharded blockchain, each endpoint have sharding structure,
|
||||
// However sharding structure is different between mainnet, testnet and local
|
||||
// testnet We need to get sharding info before doing cross-shard transaction
|
||||
const res = await harmony.blockchain.getShardingStructure(); |
||||
harmony.shardingStructures(res.result); |
||||
} |
||||
|
||||
async function execute() { |
||||
await setSharding(); |
||||
|
||||
const signedTxn = await harmony.wallet.signStaking(stakingTxn); |
||||
signedTxn |
||||
.observed() |
||||
.on('transactionHash', (txnHash) => { |
||||
console.log(''); |
||||
console.log('--- hash ---'); |
||||
console.log(''); |
||||
console.log(txnHash); |
||||
console.log(''); |
||||
}) |
||||
.on('receipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- receipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('cxReceipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- cxReceipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('error', (error) => { |
||||
console.log(''); |
||||
console.log('--- error ---'); |
||||
console.log(''); |
||||
console.log(error); |
||||
console.log(''); |
||||
}); |
||||
|
||||
// console.log(signedTxn);
|
||||
|
||||
const [sentTxn, txnHash] = await signedTxn.sendTransaction(); |
||||
// signedTxn.sendTransaction()
|
||||
// .then(res => {
|
||||
// console.log(res);
|
||||
// })
|
||||
// .catch(err => {
|
||||
// console.log(err);
|
||||
// });
|
||||
|
||||
// to confirm the result if it is already there
|
||||
// console.log(txnHash);
|
||||
console.log(sentTxn); |
||||
|
||||
const confiremdTxn = await sentTxn.confirm(txnHash); |
||||
|
||||
// if the transactino is cross-shard transaction
|
||||
if (confiremdTxn.isConfirmed()) { |
||||
console.log('--- Result ---'); |
||||
console.log(''); |
||||
console.log('Normal transaction'); |
||||
console.log(`${txnHash} is confirmed`); |
||||
console.log(''); |
||||
process.exit(); |
||||
} |
||||
// if (confiremdTxn.isConfirmed() && confiremdTxn.isCxConfirmed()) {
|
||||
// console.log('--- Result ---');
|
||||
// console.log('');
|
||||
// console.log('Cross-Shard transaction');
|
||||
// console.log(`${txnHash} is confirmed`);
|
||||
// console.log('');
|
||||
// process.exit();
|
||||
// }
|
||||
} |
||||
execute(); |
||||
// const delegateMsg = Delegate({
|
||||
// delegatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// validatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// amount: '0xa',
|
||||
// });
|
||||
|
||||
// const stakingTxn = StakingTransaction({
|
||||
// directive: '0x2',
|
||||
// stakeMsg: delegateMsg,
|
||||
// nonce: '0x2',
|
||||
// gasPrice: '0x',
|
||||
// gasLimit: '0x64',
|
||||
// chainId: 0,
|
||||
// from: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
|
||||
// });
|
||||
|
||||
// stakingTxn
|
||||
// .sendTransaction()
|
||||
// .then((stakingTxn, res) => {
|
||||
// console.log(res);
|
||||
// })
|
||||
// .catch((err) => {
|
||||
// console.log(err);
|
||||
// });
|
@ -0,0 +1,31 @@ |
||||
const { Harmony } = require('@harmony-js/core'); |
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
const { encryptPhrase, decryptPhrase } = require('@harmony-js/crypto'); |
||||
|
||||
const harmony = new Harmony('http://localhost:9500', { chainId: 2, chainType: 'hmy' }); |
||||
const myPhrase = harmony.wallet.newMnemonic(); |
||||
const pwd = '1234'; |
||||
|
||||
async function encryptThePhrase(phrase, pass) { |
||||
const result = await encryptPhrase(phrase, pass); |
||||
return result; |
||||
} |
||||
|
||||
async function decryptThePhrase(keystore, pass) { |
||||
const result = await decryptPhrase(keystore, pass); |
||||
return result; |
||||
} |
||||
|
||||
async function phraseKeyStore() { |
||||
const keyStore = await encryptThePhrase(myPhrase, pwd); |
||||
const recoveredPhrase = await decryptThePhrase(JSON.parse(keyStore), pwd); |
||||
return { myPhrase, keyStore, recoveredPhrase }; |
||||
} |
||||
|
||||
phraseKeyStore().then((result) => { |
||||
const { myPhrase, keyStore, recoveredPhrase } = result; |
||||
|
||||
harmony.wallet.addByMnemonic(myPhrase); |
||||
console.log({ myPhrase, keyStore, recoveredPhrase }); |
||||
console.log(harmony.wallet); |
||||
}); |
@ -0,0 +1,40 @@ |
||||
const { Harmony } = require('@harmony-js/core'); |
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
const { |
||||
randomBytes, |
||||
generatePrivateKey, |
||||
getAddress, |
||||
getAddressFromPrivateKey, |
||||
encryptPhrase, |
||||
decryptPhrase, |
||||
} = require('@harmony-js/crypto'); |
||||
|
||||
const harmony = new Harmony('http://localhost:9500', { chainId: 2, chainType: 'hmy' }); |
||||
const myPhrase = harmony.wallet.newMnemonic(); |
||||
const pwd = '1234'; |
||||
|
||||
async function encryptThePhrase(phrase, pass) { |
||||
const result = await encryptPhrase(phrase, pass); |
||||
return result; |
||||
} |
||||
|
||||
async function decryptThePhrase(keystore, pass) { |
||||
const result = await decryptPhrase(keystore, pass); |
||||
return result; |
||||
} |
||||
|
||||
async function phraseKeyStore() { |
||||
const keyStore = await encryptThePhrase(myPhrase, pwd); |
||||
const recoveredPhrase = await decryptThePhrase(JSON.parse(keyStore), pwd); |
||||
return { myPhrase, keyStore, recoveredPhrase }; |
||||
} |
||||
|
||||
phraseKeyStore().then((result) => { |
||||
const { myPhrase, keyStore, recoveredPhrase } = result; |
||||
|
||||
const anotherPhrase = 'wall public vague under poem acid jaguar describe net scene sponsor neck'; |
||||
harmony.wallet.addByMnemonic(myPhrase); |
||||
harmony.wallet.addByMnemonic(anotherPhrase); |
||||
console.log({ myPhrase, keyStore, recoveredPhrase }); |
||||
console.log(harmony.wallet); |
||||
}); |
@ -0,0 +1,153 @@ |
||||
// import or require Harmony class
|
||||
const { Harmony } = require('@harmony-js/core'); |
||||
|
||||
// import or require settings
|
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
|
||||
const URL_TESTNET = `https://api.s0.b.hmny.io`; |
||||
const URL_MAINNET = `https://api.s0.t.hmny.io`; |
||||
const URL_PANGAEA = 'https://api.s0.pga.hmny.io'; |
||||
|
||||
// 1. initialize the Harmony instance
|
||||
|
||||
const harmony = new Harmony( |
||||
// rpc url
|
||||
URL_PANGAEA, |
||||
{ |
||||
// chainType set to Harmony
|
||||
chainType: ChainType.Harmony, |
||||
// chainType set to HmyLocal
|
||||
chainId: ChainID.HmyPangaea, |
||||
}, |
||||
); |
||||
|
||||
// 2. get wallet ready
|
||||
// one18n8e7472pg5fqvcfcr5hg0npquha24wsxmjheg
|
||||
const phrase = 'genius cable radar memory high catch blossom correct middle wish gentle fiscal'; |
||||
// const phrase =
|
||||
// 'resemble rent deposit unique garment ripple burst negative else decorate menu theme';
|
||||
|
||||
// one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
// surge welcome lion goose gate consider taste injury health march debris kick
|
||||
|
||||
// add privateKey to wallet
|
||||
|
||||
const privateKey = '63e35b761e9df0d50ddcdaa8e33c235b60c991bfed22925a12768b0c08ef822f'; |
||||
|
||||
const sender = harmony.wallet.addByMnemonic(phrase); |
||||
const sender2 = harmony.wallet.addByPrivateKey(privateKey); |
||||
|
||||
harmony.wallet.setSigner(sender2.address); |
||||
|
||||
console.log('sender2Address is : ', sender2.bech32Address); |
||||
|
||||
// 3. get sharding info
|
||||
async function setSharding() { |
||||
// Harmony is a sharded blockchain, each endpoint have sharding structure,
|
||||
// However sharding structure is different between mainnet, testnet and local testnet
|
||||
// We need to get sharding info before doing cross-shard transaction
|
||||
const res = await harmony.blockchain.getShardingStructure(); |
||||
|
||||
harmony.shardingStructures(res.result); |
||||
} |
||||
|
||||
// 4. get transaction payload ready
|
||||
|
||||
async function transfer(receiver) { |
||||
// run set sharding first, if you want to make a cross-shard transaction
|
||||
await setSharding(); |
||||
|
||||
//1e18
|
||||
const txn = harmony.transactions.newTx({ |
||||
// token send to
|
||||
to: receiver, |
||||
// amount to send
|
||||
value: '100000000', |
||||
// gas limit, you can use string
|
||||
gasLimit: '210000', |
||||
// send token from shardID
|
||||
shardID: 0, |
||||
// send token to toShardID
|
||||
toShardID: 1, |
||||
// gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
|
||||
gasPrice: new harmony.utils.Unit('10').asGwei().toWei(), |
||||
}); |
||||
|
||||
// sign the transaction use wallet;
|
||||
|
||||
// This will happen at the chrome extension.
|
||||
const signedTxn = await harmony.wallet.signTransaction(txn); |
||||
|
||||
// Now you can use `Transaction.observed()` to listen events
|
||||
|
||||
// Frontend received back the signedTxn and do the followings to Send transaction.
|
||||
signedTxn |
||||
.observed() |
||||
.on('transactionHash', (txnHash) => { |
||||
console.log(''); |
||||
console.log('--- hash ---'); |
||||
console.log(''); |
||||
console.log(txnHash); |
||||
console.log(''); |
||||
}) |
||||
.on('receipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- receipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('cxReceipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- cxReceipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('error', (error) => { |
||||
console.log(''); |
||||
console.log('--- error ---'); |
||||
console.log(''); |
||||
console.log(error); |
||||
console.log(''); |
||||
}); |
||||
|
||||
// send the txn, get [Transaction, transactionHash] as result
|
||||
|
||||
const [sentTxn, txnHash] = await signedTxn.sendTransaction(); |
||||
|
||||
// to confirm the result if it is already there
|
||||
|
||||
const confiremdTxn = await sentTxn.confirm(txnHash); |
||||
|
||||
// if the transactino is cross-shard transaction
|
||||
if (!confiremdTxn.isCrossShard()) { |
||||
if (confiremdTxn.isConfirmed()) { |
||||
console.log('--- Result ---'); |
||||
console.log(''); |
||||
console.log('Normal transaction'); |
||||
console.log(`${txnHash} is confirmed`); |
||||
console.log(''); |
||||
console.log('please see detail in explorer:'); |
||||
console.log(''); |
||||
console.log('https://explorer.harmony.one/#/tx/' + txnHash); |
||||
console.log(''); |
||||
process.exit(); |
||||
} |
||||
} |
||||
if (confiremdTxn.isConfirmed() && confiremdTxn.isCxConfirmed()) { |
||||
console.log('--- Result ---'); |
||||
console.log(''); |
||||
console.log('Cross-Shard transaction'); |
||||
console.log(`${txnHash} is confirmed`); |
||||
console.log(''); |
||||
console.log('please see detail in explorer:'); |
||||
console.log(''); |
||||
console.log('https://explorer.harmony.one/#/tx/' + txnHash); |
||||
console.log(''); |
||||
process.exit(); |
||||
} |
||||
} |
||||
|
||||
// sending from one18n8e7472pg5fqvcfcr5hg0npquha24wsxmjheg to one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
(async () => await transfer('one1pf75h0t4am90z8uv3y0dgunfqp4lj8wr3t5rsp'))(); |
@ -0,0 +1,151 @@ |
||||
// import or require Harmony class
|
||||
const { Harmony } = require('@harmony-js/core'); |
||||
|
||||
// import or require settings
|
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
|
||||
// const URL_TESTNET = `https://api.s0.pga.hmny.io`;
|
||||
const URL_MAINNET = `https://api.s0.t.hmny.io`; |
||||
// const LOCAL_TESTNET = `http://localhost:9500`;
|
||||
const DEVNET = 'https://api.s0.pga.hmny.io'; |
||||
// 1. initialize the Harmony instance
|
||||
|
||||
const harmony = new Harmony( |
||||
// rpc url
|
||||
DEVNET, |
||||
{ |
||||
// chainType set to Harmony
|
||||
chainType: ChainType.Harmony, |
||||
// chainType set to HmyLocal
|
||||
chainId: ChainID.HmyPangaea, |
||||
}, |
||||
); |
||||
|
||||
// 2. get wallet ready
|
||||
// one18n8e7472pg5fqvcfcr5hg0npquha24wsxmjheg;
|
||||
const phrase = 'genius cable radar memory high catch blossom correct middle wish gentle fiscal'; |
||||
|
||||
// one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
// surge welcome lion goose gate consider taste injury health march debris kick
|
||||
|
||||
// add privateKey to wallet
|
||||
// const sender = harmony.wallet.addByMnemonic(phrase);
|
||||
|
||||
// add privateKey to wallet
|
||||
const private = '63e35b761e9df0d50ddcdaa8e33c235b60c991bfed22925a12768b0c08ef822f'; |
||||
// one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy
|
||||
const sender = harmony.wallet.addByPrivateKey(private); |
||||
|
||||
// const sender = harmony.wallet.addByPrivateKey(
|
||||
// 'fd416cb87dcf8ed187e85545d7734a192fc8e976f5b540e9e21e896ec2bc25c3',
|
||||
// );
|
||||
|
||||
// 3. get sharding info
|
||||
async function setSharding() { |
||||
// Harmony is a sharded blockchain, each endpoint have sharding structure,
|
||||
// However sharding structure is different between mainnet, testnet and local testnet
|
||||
// We need to get sharding info before doing cross-shard transaction
|
||||
const res = await harmony.blockchain.getShardingStructure(); |
||||
harmony.shardingStructures(res.result); |
||||
} |
||||
|
||||
// 4. get transaction payload ready
|
||||
|
||||
async function transfer(receiver) { |
||||
// run set sharding first, if you want to make a cross-shard transaction
|
||||
await setSharding(); |
||||
|
||||
//1e18
|
||||
const txn = harmony.transactions.newTx({ |
||||
// token send to
|
||||
to: receiver, |
||||
// amount to send
|
||||
value: '100000000000000000', |
||||
// gas limit, you can use string
|
||||
gasLimit: '210000', |
||||
// send token from shardID
|
||||
shardID: 0, |
||||
// send token to toShardID
|
||||
toShardID: 0, |
||||
// gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
|
||||
gasPrice: new harmony.utils.Unit('100').asGwei().toWei(), |
||||
}); |
||||
|
||||
// sign the transaction use wallet;
|
||||
|
||||
// This will happen at the chrome extension.
|
||||
const signedTxn = await harmony.wallet.signTransaction(txn); |
||||
|
||||
// Now you can use `Transaction.observed()` to listen events
|
||||
|
||||
// Frontend received back the signedTxn and do the followings to Send transaction.
|
||||
signedTxn |
||||
.observed() |
||||
.on('transactionHash', (txnHash) => { |
||||
console.log(''); |
||||
console.log('--- hash ---'); |
||||
console.log(''); |
||||
console.log(txnHash); |
||||
console.log(''); |
||||
}) |
||||
.on('receipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- receipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('cxReceipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- cxReceipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('error', (error) => { |
||||
console.log(''); |
||||
console.log('--- error ---'); |
||||
console.log(''); |
||||
console.log(error); |
||||
console.log(''); |
||||
}); |
||||
|
||||
// send the txn, get [Transaction, transactionHash] as result
|
||||
|
||||
const [sentTxn, txnHash] = await signedTxn.sendTransaction(); |
||||
|
||||
// to confirm the result if it is already there
|
||||
|
||||
const confiremdTxn = await sentTxn.confirm(txnHash); |
||||
|
||||
// if the transactino is cross-shard transaction
|
||||
// if (!confiremdTxn.isCrossShard()) {
|
||||
if (confiremdTxn.isConfirmed()) { |
||||
console.log('--- Result ---'); |
||||
console.log(''); |
||||
console.log('Normal transaction'); |
||||
console.log(`${txnHash} is confirmed`); |
||||
console.log(''); |
||||
console.log('please see detail in explorer:'); |
||||
console.log(''); |
||||
console.log('https://explorer.harmony.one/#/tx/' + txnHash); |
||||
console.log(''); |
||||
process.exit(); |
||||
} |
||||
// }
|
||||
if (confiremdTxn.isConfirmed() && confiremdTxn.isCxConfirmed()) { |
||||
console.log('--- Result ---'); |
||||
console.log(''); |
||||
console.log('Cross-Shard transaction'); |
||||
console.log(`${txnHash} is confirmed`); |
||||
console.log(''); |
||||
console.log('please see detail in explorer:'); |
||||
console.log(''); |
||||
console.log('https://explorer.harmony.one/#/tx/' + txnHash); |
||||
console.log(''); |
||||
process.exit(); |
||||
} |
||||
} |
||||
|
||||
// sending from one18n8e7472pg5fqvcfcr5hg0npquha24wsxmjheg to one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
(async () => await transfer('one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy'))(); |
@ -0,0 +1,137 @@ |
||||
// import or require Harmony class
|
||||
const { Harmony } = require('@harmony-js/core'); |
||||
|
||||
// import or require settings
|
||||
const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
|
||||
//const URL_TESTNET = `https://api.s0.b.hmny.io`;
|
||||
const URL_TESTNET = `localhost:9500`; |
||||
const URL_MAINNET = `https://api.s0.t.hmny.io`; |
||||
|
||||
// 1. initialize the Harmony instance
|
||||
|
||||
const harmony = new Harmony( |
||||
// rpc url
|
||||
URL_TESTNET, |
||||
{ |
||||
// chainType set to Harmony
|
||||
chainType: ChainType.Harmony, |
||||
// chainType set to HmyLocal
|
||||
chainId: ChainID.HmyTestnet, |
||||
}, |
||||
); |
||||
|
||||
// 2. get wallet ready
|
||||
// one18n8e7472pg5fqvcfcr5hg0npquha24wsxmjheg
|
||||
//const phrase = 'genius cable radar memory high catch blossom correct middle wish gentle fiscal';
|
||||
|
||||
// one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
// surge welcome lion goose gate consider taste injury health march debris kick
|
||||
|
||||
// add privateKey to wallet
|
||||
//const sender = harmony.wallet.addByMnemonic(phrase);
|
||||
// add privateKey to wallet
|
||||
const private = 'fd416cb87dcf8ed187e85545d7734a192fc8e976f5b540e9e21e896ec2bc25c3'; |
||||
const sender = harmony.wallet.addByPrivateKey(private); |
||||
|
||||
// 3. get sharding info
|
||||
async function setSharding() { |
||||
// Harmony is a sharded blockchain, each endpoint have sharding structure,
|
||||
// However sharding structure is different between mainnet, testnet and local testnet
|
||||
// We need to get sharding info before doing cross-shard transaction
|
||||
const res = await harmony.blockchain.getShardingStructure(); |
||||
harmony.shardingStructures(res.result); |
||||
} |
||||
|
||||
// 4. get transaction payload ready
|
||||
|
||||
async function transfer(receiver) { |
||||
// run set sharding first, if you want to make a cross-shard transaction
|
||||
await setSharding(); |
||||
|
||||
//1e18
|
||||
const txn = harmony.transactions.newTx({ |
||||
// token send to
|
||||
to: receiver, |
||||
// amount to send
|
||||
value: '100000000000000000', |
||||
// gas limit, you can use string
|
||||
gasLimit: '210000', |
||||
// send token from shardID
|
||||
shardID: 0, |
||||
// send token to toShardID
|
||||
toShardID: 0, |
||||
// gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
|
||||
gasPrice: new harmony.utils.Unit('100').asGwei().toWei(), |
||||
}); |
||||
|
||||
// sign the transaction use wallet;
|
||||
|
||||
// This will happen at the chrome extension.
|
||||
const signedTxn = await harmony.wallet.signTransaction(txn); |
||||
|
||||
// Now you can use `Transaction.observed()` to listen events
|
||||
|
||||
// Frontend received back the signedTxn and do the followings to Send transaction.
|
||||
signedTxn |
||||
.observed() |
||||
.on('transactionHash', (txnHash) => { |
||||
console.log(''); |
||||
console.log('--- hash ---'); |
||||
console.log(''); |
||||
console.log(txnHash); |
||||
console.log(''); |
||||
}) |
||||
.on('receipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- receipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('cxReceipt', (receipt) => { |
||||
console.log(''); |
||||
console.log('--- cxReceipt ---'); |
||||
console.log(''); |
||||
console.log(receipt); |
||||
console.log(''); |
||||
}) |
||||
.on('error', (error) => { |
||||
console.log(''); |
||||
console.log('--- error ---'); |
||||
console.log(''); |
||||
console.log(error); |
||||
console.log(''); |
||||
}); |
||||
|
||||
// send the txn, get [Transaction, transactionHash] as result
|
||||
|
||||
const [sentTxn, txnHash] = await signedTxn.sendTransaction(); |
||||
|
||||
// to confirm the result if it is already there
|
||||
|
||||
const confiremdTxn = await sentTxn.confirm(txnHash); |
||||
|
||||
// if the transactino is cross-shard transaction
|
||||
if (!confiremdTxn.isCrossShard()) { |
||||
if (confiremdTxn.isConfirmed()) { |
||||
console.log('--- Result ---'); |
||||
console.log(''); |
||||
console.log('Normal transaction'); |
||||
console.log(`${txnHash} is confirmed`); |
||||
console.log(''); |
||||
process.exit(); |
||||
} |
||||
} |
||||
if (confiremdTxn.isConfirmed() && confiremdTxn.isCxConfirmed()) { |
||||
console.log('--- Result ---'); |
||||
console.log(''); |
||||
console.log('Cross-Shard transaction'); |
||||
console.log(`${txnHash} is confirmed`); |
||||
console.log(''); |
||||
process.exit(); |
||||
} |
||||
} |
||||
|
||||
// sending from one18n8e7472pg5fqvcfcr5hg0npquha24wsxmjheg to one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65
|
||||
transfer('one1a2rhuaqjcvfu69met9sque2l3w5v9z6qcdcz65'); |
File diff suppressed because it is too large
Load Diff
@ -1,12 +1,10 @@ |
||||
# Packages available are: |
||||
|
||||
1. [@woop-js/core](https://github.com/woop-chain/sdk/tree/master/packages/woop-core) |
||||
2. [@woop-js/account](https://github.com/woop-chain/sdk/tree/master/packages/woop-account) |
||||
3. [@woop-js/crypto](https://github.com/woop-chain/sdk/tree/master/packages/woop-crypto) |
||||
4. [@woop-js/network](https://github.com/woop-chain/sdk/tree/master/packages/woop-network) |
||||
5. [@woop-js/utils](https://github.com/woop-chain/sdk/tree/master/packages/woop-utils) |
||||
6. [@woop-js/transaction](https://github.com/woop-chain/sdk/tree/master/packages/woop-transaction) |
||||
7. [@woop-js/contract](https://github.com/woop-chain/sdk/tree/master/packages/woop-contract) |
||||
8. [@woop-js/staking](https://github.com/woop-chain/sdk/tree/master/packages/woop-staking) |
||||
|
||||
<mark>Package level documentation and examples are inside each package</mark> |
||||
1. [@harmony-js/core](https://github.com/harmony-one/sdk/tree/master/packages/harmony-core) |
||||
2. [@harmony-js/account](https://github.com/harmony-one/sdk/tree/master/packages/harmony-account) |
||||
3. [@harmony-js/crypto](https://github.com/harmony-one/sdk/tree/master/packages/harmony-crypto) |
||||
4. [@harmony-js/network](https://github.com/harmony-one/sdk/tree/master/packages/harmony-network) |
||||
5. [@harmony-js/utils](https://github.com/harmony-one/sdk/tree/master/packages/harmony-utils) |
||||
6. [@harmony-js/transaction](https://github.com/harmony-one/sdk/tree/master/packages/harmony-transaction) |
||||
7. [@harmony-js/contract](https://github.com/harmony-one/sdk/tree/master/packages/harmony-contract) |
||||
8. [@harmony-js/staking](https://github.com/harmony-one/sdk/tree/master/packages/harmony-staking) |
||||
|
@ -0,0 +1,274 @@ |
||||
/** |
||||
## About This Package |
||||
|
||||
`@harmony-js/account` is dealing with account related features. |
||||
|
||||
Developers can use this package to: |
||||
- Create `Account` instance |
||||
- Create `Wallet` instance |
||||
- Sign `Transaction` |
||||
- Convert address format |
||||
- Manage `privateKey` or `mnemonic phrases` and do the `encrypt` and `decrypt` job |
||||
|
||||
There are 2 main classes in this package, `Account` and `Wallet`. |
||||
|
||||
- The `Account` class is basic instance that contains most features mentioned above. |
||||
- The `Wallet` class is class that stores all `Account` instance, you can do CRUD on it. |
||||
|
||||
|
||||
## Usage of Account |
||||
|
||||
### Dependencies |
||||
- @harmony-js/network |
||||
- @harmony-js/staking |
||||
- @harmony-js/transaction |
||||
- @harmony-js/utils |
||||
|
||||
### Examples |
||||
|
||||
Create a random account |
||||
```javascript
|
||||
// import the Account class
|
||||
import {Account} from '@harmony-js/account' |
||||
|
||||
// Messenger is optional, by default, we have a defaultMessenger
|
||||
// If you like to change, you will import related package here.
|
||||
import { HttpProvider, Messenger } from '@harmony-js/network'; |
||||
import { ChainType, ChainID } from '@harmony-js/utils'; |
||||
|
||||
// create a custom messenger
|
||||
const customMessenger = new Messenger( |
||||
new HttpProvider('http://localhost:9500'), |
||||
ChainType.Harmony, // if you are connected to Harmony's blockchain
|
||||
ChainID.HmyLocal, // check if the chainId is correct
|
||||
) |
||||
|
||||
// to create an Account with random privateKey
|
||||
// and you can setMessenger later
|
||||
const randomAccount = new Account() |
||||
randomAccount.setMessenger(customMessenger) |
||||
|
||||
// or you can set messenger on `new`
|
||||
const randomAccountWithCustomMessenger = new Account(undefined, customMessenger) |
||||
|
||||
// you can display the account
|
||||
console.log({randomAccount,randomAccountWithCustomMessenger}) |
||||
|
||||
// or you can use static method to create an Account
|
||||
const staticCreatedAccount = Account.new() |
||||
// but you have to set messenger manually after
|
||||
staticCreatedAccount.setMessenger(customMessenger) |
||||
|
||||
console.log({staticCreatedAccount}) |
||||
``` |
||||
|
||||
### Import an existing privateKey to create Account |
||||
|
||||
```typescript
|
||||
|
||||
// import the Account class
|
||||
import {Account} from '@harmony-js/account' |
||||
|
||||
// NOTED: Key with or without `0x` are accepted, makes no different
|
||||
// NOTED: DO NOT import `mnemonic phrase` using `Account` class, use `Wallet` instead
|
||||
const myPrivateKey = '0xe19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930' |
||||
|
||||
const myAccountWithMyPrivateKey = new Account(myPrivateKey) |
||||
|
||||
// you can also import privateKey use static method
|
||||
const myAccountWithMyPrivateKeyUsingStatic = Account.add(myPrivateKey) |
||||
|
||||
console.log({ myAccountWithMyPrivateKey, myAccountWithMyPrivateKeyUsingStatic }) |
||||
|
||||
``` |
||||
|
||||
### Encrypt/Export keyStore file, Decrypt/Import keyStore file
|
||||
|
||||
```typescript
|
||||
|
||||
// import the Account class
|
||||
import {Account} from '@harmony-js/account' |
||||
|
||||
// suppose we have an account
|
||||
const myPrivateKey = '0xe19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930' |
||||
const myAccountWithMyPrivateKey = new Account(myPrivateKey) |
||||
|
||||
// suppose we have a password, and we want to encrypt the account above
|
||||
const myStrongPassword = '123' |
||||
|
||||
async function encryptAndDecrypt(password) { |
||||
// we get the privateKey before encrypted as comparison
|
||||
const unencryptedPrivateKey = myAccountWithMyPrivateKey.privateKey |
||||
|
||||
// export the account to keyStore string, which will make the privateKey encrpyted
|
||||
const keyStoreFile = await myAccountWithMyPrivateKey.toFile(password) |
||||
// exported keyStoreFile
|
||||
console.log({ keyStoreFile }) |
||||
// see if the account is encrypted
|
||||
console.log(`Is this account encrypted? \n ${myAccountWithMyPrivateKey.encrypted}`) |
||||
// keystore file should be equal to encrypted privateKey
|
||||
console.log( |
||||
`Is privateKey equal to keyStore string? \n ${keyStoreFile === |
||||
myAccountWithMyPrivateKey.privateKey}`,
|
||||
) |
||||
} |
||||
|
||||
encryptAndDecrypt(myStrongPassword) |
||||
|
||||
|
||||
// suppose we have keyStorefile, in this example, we just use same password and keystore string encrypted above
|
||||
const someKeyStoreFile = |
||||
'{"version":3,"id":"62326332-3139-4839-b534-656134623066","address":"1fe3da351d9fc0c4f02de5412ad7def8aee956c5","Crypto":{"ciphertext":"b86ab81682c9f5a35738ad9bd38cd9b46c1b852ef33f16dd83068f79e20d5531","cipherparams":{"iv":"44efb5a514f34968e92cafad80566487"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"salt":"d70ae1f311601562113f98c8ebe382f52a332dca1588886e5ea91e2f8a647134","n":8192,"r":8,"p":1,"dklen":32},"mac":"7b63e4e31a75a22b7091291bb58302655b738539ef3e30b30a7a7f170f6163ef"}}' |
||||
|
||||
async function importKeyStoreFileAndDecrypt(keyStoreFile, password) { |
||||
// import keyStore string and provide the password, remember to make a new Account first
|
||||
const importedAccount = await Account.new().fromFile(keyStoreFile, password) |
||||
// the account should decypted which `Account.encrypted` is false
|
||||
console.log(`Is this account encrypted? \n ${importedAccount.encrypted}`) |
||||
// see if the privatekey is equal to unencrypted one?
|
||||
console.log( |
||||
`Is the account recovered from keystore? \n ${importedAccount.privateKey === myPrivateKey}`, |
||||
) |
||||
} |
||||
|
||||
importKeyStoreFileAndDecrypt(someKeyStoreFile, myStrongPassword) |
||||
|
||||
|
||||
``` |
||||
|
||||
|
||||
### Address format getter |
||||
|
||||
```typescript
|
||||
|
||||
// import the Account class
|
||||
import {Account} from '@harmony-js/account' |
||||
|
||||
// suppose we have an account
|
||||
const myPrivateKey = '0xe19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930' |
||||
const myAccountWithMyPrivateKey = new Account(myPrivateKey) |
||||
|
||||
// Account.address is bytes20/base16 address
|
||||
console.log(myAccountWithMyPrivateKey.address) |
||||
// Account.bech32Address is bech32 format address, in Harmony, it's `one1` prefixed
|
||||
console.log(myAccountWithMyPrivateKey.bech32Address) |
||||
// Account.bech32TestNetAddress is bech32 format address, in Harmony, it's `tone1` prefixed, used in testnet
|
||||
console.log(myAccountWithMyPrivateKey.bech32TestNetAddress) |
||||
// Account.checksumAddress is checksumed address from base16
|
||||
console.log(myAccountWithMyPrivateKey.checksumAddress) |
||||
|
||||
``` |
||||
|
||||
|
||||
### Sign a transaction |
||||
|
||||
```typescript
|
||||
|
||||
// import the Account class
|
||||
import {Account} from '@harmony-js/account' |
||||
|
||||
// import Transaction class from '@harmony-js/transaction'
|
||||
import {Transaction} from '@harmony-js/transaction' |
||||
|
||||
// Messenger is optional, by default, we have a defaultMessenger
|
||||
// If you like to change, you will import related package here.
|
||||
import { HttpProvider, Messenger } from '@harmony-js/network'; |
||||
import { ChainType, ChainID, Unit } from '@harmony-js/utils'; |
||||
|
||||
// create a custom messenger
|
||||
const customMessenger = new Messenger( |
||||
new HttpProvider('http://localhost:9500'), |
||||
ChainType.Harmony, // if you are connected to Harmony's blockchain
|
||||
ChainID.HmyLocal, // check if the chainId is correct
|
||||
) |
||||
// suppose we have an account
|
||||
const myPrivateKey = '0xe19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930' |
||||
const myAccountWithMyPrivateKey = new Account(myPrivateKey) |
||||
|
||||
|
||||
const txnObject = { |
||||
// token send to
|
||||
to: 'one166axnkjmghkf3df7xfvd0hn4dft8kemrza4cd2', |
||||
// amount to send
|
||||
value: '1000000000000000000000', |
||||
// gas limit, you can use string or use BN value
|
||||
gasLimit: '210000', |
||||
// send token from shardID
|
||||
shardID: 0, |
||||
// send token to toShardID
|
||||
toShardID: 0, |
||||
// gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
|
||||
gasPrice: new Unit('100').asGwei().toWei(), |
||||
// if you set nonce manually here, and remember, the `updateNonce` of `Account.signTransaction` should be set to false
|
||||
nonce: 0, |
||||
}; |
||||
|
||||
const txn = new Transaction(txnObject, customMessenger); |
||||
|
||||
async function signTheTxn() { |
||||
// Account.signTransaction(transaction: Transaction, updateNonce?: boolean, encodeMode?: string, blockNumber?: string): Promise<Transaction>
|
||||
// If the 2nd parameter `updateNonce` is set to true, it will query and update account's nonce before it signs
|
||||
const signedTxn = await myAccountWithMyPrivateKey.signTransaction(txn, false, 'rlp', 'latest'); |
||||
|
||||
// see if the transaction is signed
|
||||
console.log(`\n see if transaction is signed: \n ${signedTxn.isSigned()} \n`); |
||||
|
||||
// get the tranaction bytes
|
||||
console.log(`\n the signed bytes is: \n ${signedTxn.getRawTransaction()} \n`); |
||||
|
||||
return signTheTxn; |
||||
} |
||||
|
||||
signTheTxn(); |
||||
``` |
||||
|
||||
|
||||
## Usage of Wallet |
||||
|
||||
### Dependencies |
||||
- @harmony-js/crypto |
||||
- @harmony-js/network |
||||
- @harmony-js/staking |
||||
- @harmony-js/transaction |
||||
- @harmony-js/utils |
||||
|
||||
```typescript
|
||||
// constructor
|
||||
const { Wallet } = require('@harmony-js/account'); |
||||
const wallet = new Wallet(customMessenger); |
||||
|
||||
// get signer
|
||||
const wallet = new Wallet(customMessenger); |
||||
const key_1 = '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e'; |
||||
console.log(wallet.addByPrivateKey(key_1)); |
||||
console.log(wallet.signer) |
||||
|
||||
// createAccount
|
||||
console.log(wallet.accounts); |
||||
wallet.createAccount(); |
||||
wallet.createAccount(); |
||||
console.log(wallet.accounts); |
||||
|
||||
// encryptAccount
|
||||
const key_1 = '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e'; |
||||
wallet.addByPrivateKey(key_1); |
||||
wallet.encryptAccount('one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7', '12345').then((value) => { |
||||
console.log(value); |
||||
}) |
||||
|
||||
// decrptAccount
|
||||
const key_1 = '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e'; |
||||
wallet.addByPrivateKey(key_1); |
||||
wallet.encryptAccount('one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7', '12345').then(() => { |
||||
wallet.decryptAccount('one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7', '12345').then((value) => { |
||||
console.log(value); |
||||
}) |
||||
}); |
||||
``` |
||||
*
|
||||
* @packageDocumentation |
||||
* @module harmony-account |
||||
*/ |
||||
|
||||
/**@ignore */ |
||||
export interface README {} |
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-account |
||||
* @module harmony-account |
||||
* @ignore |
||||
*/ |
||||
|
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-account |
||||
* @module harmony-account |
||||
* @hidden |
||||
*/ |
||||
|
@ -0,0 +1,14 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module harmony-account |
||||
* @hidden |
||||
*/ |
||||
|
||||
import { HttpProvider, Messenger } from '@harmony-js/network'; |
||||
import { ChainType, ChainID } from '@harmony-js/utils'; |
||||
|
||||
export const defaultMessenger = new Messenger( |
||||
new HttpProvider('http://localhost:9500'), |
||||
ChainType.Harmony, |
||||
ChainID.HmyLocal, |
||||
); |
@ -1,29 +1,18 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-account |
||||
* @module harmony-account |
||||
* @ignore |
||||
*/ |
||||
|
||||
import fetch from 'jest-fetch-mock'; |
||||
import { Account } from '../src/account'; |
||||
import { HttpProvider, Messenger } from '@woop-js/network'; |
||||
import { ChainType, ChainID } from '@woop-js/utils'; |
||||
import { HttpProvider, Messenger } from '@harmony-js/network'; |
||||
import { ChainType, ChainID } from '@harmony-js/utils'; |
||||
|
||||
const provider = new HttpProvider('http://localhost:9500'); |
||||
const messenger = new Messenger(provider, ChainType.Woop, ChainID.WikiLocal); |
||||
const messenger = new Messenger(provider, ChainType.Harmony, ChainID.HmyLocal); |
||||
|
||||
describe('test account', () => { |
||||
it('test Account.getBalance returns object that implements Balance interface', () => { |
||||
fetch.mockResponses( |
||||
[ |
||||
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x166c690f33421e"}), |
||||
{ status: 200 } |
||||
], |
||||
[ |
||||
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x106"}), |
||||
{ status: 200 } |
||||
] |
||||
); |
||||
const acc = Account.new(); |
||||
acc.setMessenger(messenger); |
||||
acc.getBalance().then((res) => { |
@ -1,12 +1,12 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
* @hidden |
||||
*/ |
||||
|
||||
import { AbiCoder as ABICoder, ParamType, toUtf8Bytes } from './abiCoder'; |
||||
import { isObject, isArray } from '@woop-js/utils'; |
||||
import { keccak256, Arrayish } from '@woop-js/crypto'; |
||||
import { isObject, isArray } from '@harmony-js/utils'; |
||||
import { keccak256, Arrayish } from '@harmony-js/crypto'; |
||||
import { jsonInterfaceMethodToString, bnToString } from './utils'; |
||||
|
||||
export class AbiCoderClass { |
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
* @hidden |
||||
*/ |
||||
|
@ -1,11 +1,11 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
* @hidden |
||||
*/ |
||||
|
||||
import { isObject, isArray } from '@woop-js/utils'; |
||||
import { BN } from '@woop-js/crypto'; |
||||
import { isObject, isArray } from '@harmony-js/utils'; |
||||
import { BN } from '@harmony-js/crypto'; |
||||
|
||||
export const jsonInterfaceMethodToString = (json: any): string => { |
||||
if (isObject(json) && json.name && json.name.includes('(')) { |
@ -1,9 +1,9 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
*/ |
||||
|
||||
import { Wallet } from '@woop-js/account'; |
||||
import { Wallet } from '@harmony-js/account'; |
||||
import { Contract } from './contract'; |
||||
import { ContractOptions } from './utils/options'; |
||||
|
@ -1,9 +1,9 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
*/ |
||||
|
||||
import { LogSub } from '@woop-js/network'; |
||||
import { LogSub } from '@harmony-js/network'; |
||||
import { AbiItemModel } from '../models/types'; |
||||
import { Contract } from '../contract'; |
||||
import { decode as eventLogDecoder } from '../utils/decoder'; |
@ -1,10 +1,10 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
* @hidden |
||||
*/ |
||||
|
||||
import { isArray } from '@woop-js/utils'; |
||||
import { isArray } from '@harmony-js/utils'; |
||||
import { AbiCoderClass } from '../abi/api'; |
||||
import { AbiModel, AbiItemModel } from '../models/types'; |
||||
import { Contract } from '../contract'; |
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
* @hidden |
||||
*/ |
||||
|
@ -1,10 +1,10 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
* @hidden |
||||
*/ |
||||
|
||||
import { isArray } from '@woop-js/utils'; |
||||
import { isArray } from '@harmony-js/utils'; |
||||
import { AbiItemModel, AbiOutput, AbiInput } from './types'; |
||||
|
||||
export class AbiItem { |
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
* @hidden |
||||
*/ |
||||
|
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
* @hidden |
||||
*/ |
||||
|
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
* @hidden |
||||
*/ |
||||
|
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-contract |
||||
* @module harmony-contract |
||||
* @hidden |
||||
*/ |
||||
|
@ -1,11 +1,11 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-core |
||||
* @module harmony-core |
||||
* @hidden |
||||
*/ |
||||
|
||||
export * from './woop'; |
||||
export * from './harmony'; |
||||
export * from './blockchain'; |
||||
export * from './truffleProvider'; |
||||
export * from './woopExtension'; |
||||
export * from './harmonyExtension'; |
||||
export * from './types'; |
@ -0,0 +1,31 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module harmony-core |
||||
* @hidden |
||||
*/ |
||||
|
||||
import { HttpProvider, Messenger } from '@harmony-js/network'; |
||||
import { TransactionFactory, Transaction } from '@harmony-js/transaction'; |
||||
import { Wallet, Account } from '@harmony-js/account'; |
||||
import { ChainType, ChainID } from '@harmony-js/utils'; |
||||
import { Blockchain } from './blockchain'; |
||||
|
||||
export interface HarmonyModule { |
||||
HttpProvider: HttpProvider; |
||||
Messenger: Messenger; |
||||
Blockchain: Blockchain; |
||||
TransactionFactory: TransactionFactory; |
||||
Wallet: Wallet; |
||||
Transaction: Transaction; |
||||
Account: Account; |
||||
} |
||||
|
||||
export const enum UrlType { |
||||
http, |
||||
ws, |
||||
} |
||||
|
||||
export interface HarmonySetting<T extends ChainType, I extends ChainID> { |
||||
type: T; |
||||
id: I; |
||||
} |
@ -0,0 +1,16 @@ |
||||
{ |
||||
"extends": "../../tsconfig.base.json", |
||||
"compilerOptions": { |
||||
"rootDir": "src", |
||||
"outDir": "dist" |
||||
}, |
||||
"include": ["src", "../../typings/**/*.d.ts", "../harmony-utils/src/core.ts"], |
||||
"references": [ |
||||
{"path": "../harmony-account"}, |
||||
{"path": "../harmony-crypto"}, |
||||
{"path": "../harmony-utils"}, |
||||
{"path": "../harmony-network"}, |
||||
{"path": "../harmony-transaction"}, |
||||
{"path": "../harmony-contract"} |
||||
] |
||||
} |
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-crypto |
||||
* @module harmony-crypto |
||||
* @hidden |
||||
*/ |
||||
|
@ -1,82 +1,68 @@ |
||||
/** |
||||
# @woop-js/crypto |
||||
|
||||
This package provides a collection of apis related to address management, kestore, encoding, and encrypt/decrypt. |
||||
|
||||
## Installation |
||||
|
||||
``` |
||||
npm install @woop-js/crypto |
||||
``` |
||||
|
||||
## Usage |
||||
|
||||
```javascript
|
||||
* const { |
||||
* encode, |
||||
* decode, |
||||
* randomBytes, |
||||
* toBech32, |
||||
* fromBech32, |
||||
* WoopAddress, |
||||
* generatePrivateKey, |
||||
* getPubkeyFromPrivateKey, |
||||
* getAddressFromPublicKey, |
||||
* getAddressFromPrivateKey, |
||||
* encryptPhrase, |
||||
* decryptPhrase |
||||
* } = require('@woop-js/crypto'); |
||||
* const { isPrivateKey, isAddress, isPublicKey } = require('@woop-js/utils'); |
||||
``` |
||||
|
||||
Address apis |
||||
```javascript
|
||||
const bytes = randomBytes(20); |
||||
const addr = new WoopAddress(bytes); |
||||
|
||||
console.log(addr.checksum); |
||||
console.log(addr.bech32); |
||||
|
||||
console.log(WoopAddress.isValidBech32(addr.bech32)); |
||||
``` |
||||
|
||||
RLP apis |
||||
```javascript
|
||||
const encoded = '0x89010101010101010101'; |
||||
const decoded = '0x010101010101010101'; |
||||
console.log(encode(decoded)); |
||||
console.log(decode(encoded)); |
||||
``` |
||||
|
||||
Keystore apis |
||||
```javascript
|
||||
const prv = generatePrivateKey(); |
||||
const pub = getPubkeyFromPrivateKey(prv); |
||||
const addr = getAddressFromPublicKey(pub); |
||||
const addrPrv = getAddressFromPrivateKey(prv); |
||||
console.log(isPrivateKey(prv)); |
||||
console.log(isPublicKey(pub)); |
||||
console.log(isAddress(addr)); |
||||
console.log(isAddress(addrPrv)); |
||||
``` |
||||
|
||||
Encrypt/decrypt apis |
||||
```javascript
|
||||
* const { Wallet } = require('@woop-js/account'); |
||||
|
||||
* const myPhrase = new Wallet().newMnemonic(); |
||||
* console.log(myPhrase); |
||||
* const pwd = '1234'; |
||||
* encryptPhrase(myPhrase, pwd).then((value) => { |
||||
* console.log(value); |
||||
* decryptPhrase(JSON.parse(value), pwd).then(value => { |
||||
* console.log(value); |
||||
* }); |
||||
* }); |
||||
``` |
||||
* ## About this package |
||||
* |
||||
* `@harmony-js/crypot` provides a series of functions to deal with keys |
||||
* |
||||
* ## How to use this package |
||||
* |
||||
* ### Create a Harmony Instance |
||||
* ```javascript
|
||||
* const { Harmony } = require('@harmony-js/core'); |
||||
* const { ChainID, ChainType } = require('@harmony-js/utils'); |
||||
* |
||||
* const hmy = new Harmony( |
||||
* 'http://localhost:9500', |
||||
* { |
||||
* chainType: ChainType.Harmony, |
||||
* chainId: ChainID.HmyLocal, |
||||
* }, |
||||
* ); |
||||
* ``` |
||||
* |
||||
* ### Some examples |
||||
* |
||||
* ```javascript
|
||||
* // randomBytes
|
||||
* const bytes = hmy.crypto.randomBytes(20); |
||||
* console.log(bytes) |
||||
* |
||||
* // encryptPhrase
|
||||
* const myPhrase = hmy.wallet.newMnemonic(); |
||||
* const pwd = '1234'; |
||||
* hmy.crypto.encryptPhrase(myPhrase, pwd).then((value) => { |
||||
* console.log(value); |
||||
* }) |
||||
* |
||||
* // decryptThePhrase
|
||||
* hmy.crypto.encryptPhrase(myPhrase, pwd).then((keystore) => { |
||||
* hmy.crypto.decryptPhrase(JSON.parse(keystore), pwd).then((value) => { |
||||
* console.log(value); |
||||
* }) |
||||
* }) |
||||
* |
||||
* // generatePrivateKey
|
||||
* const privateKey = hmy.crypto.generatePrivateKey(); |
||||
* console.log(privateKey) |
||||
* |
||||
* // getPubkeyFromPrivateKey
|
||||
* const publicKey = hmy.crypto.getPubkeyFromPrivateKey(privateKey); |
||||
* console.log(publicKey); |
||||
* |
||||
* // getAddressFromPrivateKey
|
||||
* const address = hmy.crypto.getAddressFromPrivateKey(privateKey); |
||||
* console.log(address); |
||||
* |
||||
* // getAddressFromPublicKey
|
||||
* const address = hmy.crypto.getAddressFromPublicKey(publicKey); |
||||
* console.log(address); |
||||
* |
||||
* // toChecksumAddress
|
||||
* const checksumAddr = hmy.crypto.toChecksumAddress(address); |
||||
* console.log(checksumAddr); |
||||
* ``` |
||||
* |
||||
* @packageDocumentation |
||||
* @module woop-crypto |
||||
* @module harmony-crypto |
||||
*/ |
||||
|
||||
// This file is ported from ether.js/src.ts/errors.ts
|
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-crypto |
||||
* @module harmony-crypto |
||||
* @ignore |
||||
*/ |
||||
|
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-crypto |
||||
* @module harmony-crypto |
||||
* @ignore |
||||
*/ |
||||
|
@ -1,13 +1,14 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-crypto |
||||
* @module harmony-crypto |
||||
*/ |
||||
|
||||
import aes from 'aes-js'; |
||||
import scrypt from 'scrypt.js'; |
||||
// import scrypt from 'scrypt.js';
|
||||
import scrypt from 'scrypt-shim'; |
||||
import { pbkdf2Sync } from 'pbkdf2'; |
||||
import uuid from 'uuid'; |
||||
import { isPrivateKey } from '@woop-js/utils'; |
||||
import { isPrivateKey } from '@harmony-js/utils'; |
||||
import { randomBytes } from './random'; |
||||
import { getAddressFromPrivateKey } from './keyTool'; |
||||
import { concat, hexToIntArray } from './bytes'; |
@ -1,6 +1,6 @@ |
||||
/** |
||||
* @packageDocumentation |
||||
* @module woop-crypto |
||||
* @module harmony-crypto |
||||
*/ |
||||
|
||||
/** |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue