Skip to content

Commit

Permalink
refactor(core): rename the Core class to CKB class (#407)
Browse files Browse the repository at this point in the history
refactor(core): rename the Core class to CKB class
  • Loading branch information
Keith-CY committed Dec 9, 2019
2 parents fb22b74 + 7c1f40c commit 3f9bad5
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 121 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ All the modules above are integrated into the core module. You can find `rpc` an
To use the core module, you need to import it in your project and instantiate it with a node object. For now, the node object only contains one field named `url`, the URI of the blockchain node your are going to communicate with.

```javascript
const CKBCore = require('@nervosnetwork/ckb-sdk-core').default
const CKB = require('@nervosnetwork/ckb-sdk-core').default

const nodeUrl = 'http://localhost:8114'

const core = new CKBCore(nodeUrl)
const ckb = new CKB(nodeUrl)
```

After that you can use the `core` object to generate addresses, send requests, etc.
After that you can use the `ckb` object to generate addresses, send requests, etc.

# RPC

Expand All @@ -149,12 +149,12 @@ If the SDK is running in Node.js, the following steps make the persistent connec
// HTTP Agent
const http = require('http')
const httpAgent = new http.Agent({ keepAlive: true })
core.rpc.setNode({ httpAgent })
ckb.rpc.setNode({ httpAgent })

// HTTPS Agent
const https = require('https')
const httpsAgent = new https.Agent({ keepAlive: true })
core.rpc.setNode({ httpsAgent })
ckb.rpc.setNode({ httpsAgent })
```

# Errors
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { default: generateRawTransaction } = require('../../lib/generateRawTransaction')
const { default: Core } = require('../../lib')
const { default: CKB } = require('../../lib')
const rpc = require('../../__mocks__/rpc')

const fixtures = require('./fixtures.json')

describe('generate raw transaction', () => {
const core = new Core('http://localhost:8114')
core.rpc = rpc
const ckb = new CKB('http://localhost:8114')
ckb.rpc = rpc

const fixtureTable = Object.entries(fixtures).map(([title, { params, expected, exception }]) => [
title,
Expand Down
76 changes: 38 additions & 38 deletions packages/ckb-sdk-core/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
const fixtures = require('./fixtures.json')
const rpc = require('../__mocks__/rpc')

const { default: Core } = require('../lib')
const { default: CKB } = require('../lib')

describe('ckb-core', () => {
describe('ckb', () => {
const url = 'http://localhost:8114'
let core
let ckb
beforeEach(() => {
core = new Core(url)
ckb = new CKB(url)
})

describe('instantiate with default configuration', () => {
it('default url is http://localhost:8114', () => {
const c = new Core()
const c = new CKB()
expect(c.node.url).toBe('http://localhost:8114')
})
})

describe('load data', () => {
it('load the secp256k1 dep', async () => {
core.rpc = rpc
ckb.rpc = rpc
jest.setTimeout(50000)
const fixture = fixtures.loadSecp256k1Dep
expect(core.config.loadSecp256k1Dep).toEqual(undefined)
expect(ckb.config.loadSecp256k1Dep).toEqual(undefined)

const secp256k1Dep = await core.loadSecp256k1Dep()
const secp256k1Dep = await ckb.loadSecp256k1Dep()
expect(secp256k1Dep).toEqual(fixture.target)
})

it('load the dao dep', async () => {
core.rpc = rpc
ckb.rpc = rpc
jest.setTimeout(50000)
const fixture = fixtures.loadDaoDep
expect(core.config.loadDaoDep).toEqual(undefined)
const daoDep = await core.loadDaoDep()
expect(ckb.config.loadDaoDep).toEqual(undefined)
const daoDep = await ckb.loadDaoDep()
expect(daoDep).toEqual(fixture.target)
})

it('load cells', async () => {
core.rpc = rpc
ckb.rpc = rpc
const lockHash = '0xe831b2179a00307607d254b6fae904047b1fb7f2c76968f305ec27841201739a'
const cells = await core.loadCells({
const cells = await ckb.loadCells({
lockHash,
end: BigInt(100),
step: BigInt(100),
save: true,
})
expect(cells).toHaveLength(100)
expect(core.cells.size).toBe(1)
expect(core.cells.get(lockHash)).toHaveLength(100)
expect(ckb.cells.size).toBe(1)
expect(ckb.cells.get(lockHash)).toHaveLength(100)
})
})

describe('set node', () => {
const newURL = 'http://localhost:8080'
it('has url set by instantication', () => {
expect(core.node.url).toBe(url)
expect(core.rpc.node.url).toBe(url)
expect(ckb.node.url).toBe(url)
expect(ckb.rpc.node.url).toBe(url)
})
it('set node with url', () => {
core.setNode(newURL)
expect(core.node.url).toBe(newURL)
expect(core.rpc.node.url).toBe(newURL)
ckb.setNode(newURL)
expect(ckb.node.url).toBe(newURL)
expect(ckb.rpc.node.url).toBe(newURL)
})
it('set node with node object', () => {
core.setNode({
ckb.setNode({
url,
})
expect(core.node.url).toBe(url)
expect(core.node.url).toBe(url)
expect(ckb.node.url).toBe(url)
expect(ckb.node.url).toBe(url)
})
})

Expand All @@ -81,12 +81,12 @@ describe('ckb-core', () => {
},
}
it('generate lock hash', () => {
const lockHash = core.generateLockHash(params.publicKeyHash, params.deps)
const lockHash = ckb.generateLockHash(params.publicKeyHash, params.deps)
expect(lockHash).toBe('0x0fec94c611533c9588c8ddfed557b9024f4431a65ace4b1e7106388ddd5dd87b')
})

it('lack fo deps should throw an error', () => {
expect(() => core.generateLockHash(params.publicKeyHash)).toThrowError('deps is required')
expect(() => ckb.generateLockHash(params.publicKeyHash)).toThrowError('deps is required')
})
})

Expand All @@ -99,11 +99,11 @@ describe('ckb-core', () => {
])
test.each(fixtureTable)('%s', async (_title, script, expected, exception) => {
if (undefined !== exception) {
const computedHash = await core.rpc.computeScriptHash(script)
const computedHash = await ckb.rpc.computeScriptHash(script)
expect(computedHash).toBe(expected)
}
if (undefined !== exception) {
expect(core.rpc.computeScriptHash(script)).reject.toThrowError(exception)
expect(ckb.rpc.computeScriptHash(script)).reject.toThrowError(exception)
}
})
})
Expand All @@ -120,11 +120,11 @@ describe('ckb-core', () => {
)
test.each(fixtureTable)('%s', (_title, privateKey, transaction, expected, exception) => {
if (undefined !== expected) {
const signedTransactionWithPrivateKey = core.signTransaction(privateKey)(transaction)
const signedTransactionWithPrivateKey = ckb.signTransaction(privateKey)(transaction)
expect(signedTransactionWithPrivateKey).toEqual(expected)
}
if (undefined !== exception) {
expect(() => core.signTransaction(privateKey)(transaction)).toThrowError(exception)
expect(() => ckb.signTransaction(privateKey)(transaction)).toThrowError(exception)
}
})
})
Expand All @@ -136,39 +136,39 @@ describe('ckb-core', () => {

test.each(fixtureTable)('%s', (_title, params, expected, exception) => {
if (undefined === exception) {
const rawTransaction = core.generateRawTransaction({
const rawTransaction = ckb.generateRawTransaction({
...params,
capacity: BigInt(params.capacity),
fee: BigInt(params.fee || 0),
})
expect(rawTransaction).toEqual(expected)
} else {
expect(() => core.generateRawTransaction(params)).toThrowError(exception)
expect(() => ckb.generateRawTransaction(params)).toThrowError(exception)
}
})
})

describe('nervos dao', () => {
it('generate deposit transaction', async () => {
core.rpc = rpc
ckb.rpc = rpc
const { params, expected } = fixtures.generateDaoDepositTransaction
const p = {
fromAddress: params.fromAddress,
capacity: BigInt(params.capacity),
fee: BigInt(params.fee),
cells: params.cells,
}
expect(() => core.generateDaoDepositTransaction(p)).toThrowError('Secp256k1 dep is required')
await core.loadSecp256k1Dep()
expect(() => core.generateDaoDepositTransaction(p)).toThrowError('Dao dep is required')
await core.loadDaoDep()
const tx = await core.generateDaoDepositTransaction(p)
expect(() => ckb.generateDaoDepositTransaction(p)).toThrowError('Secp256k1 dep is required')
await ckb.loadSecp256k1Dep()
expect(() => ckb.generateDaoDepositTransaction(p)).toThrowError('Dao dep is required')
await ckb.loadDaoDep()
const tx = await ckb.generateDaoDepositTransaction(p)
expect(tx).toEqual(expected)
})

it.skip('generate start withdraw transaction', async () => {
const { params, expected } = fixtures.generateDaoWithdrawStartTransaction
const tx = await core.generateDaoWithdrawStartTransaction({
const tx = await ckb.generateDaoWithdrawStartTransaction({
outPoint: params.outPoint,
fee: BigInt(params.fee),
cells: params.cells,
Expand Down
52 changes: 26 additions & 26 deletions packages/ckb-sdk-core/examples/nervosDAO.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/* eslint-disable */
const Core = require('../lib').default
const CKB = require('../lib').default
const nodeUrl = process.env.NODE_URL || 'http://localhost:8114' // example node url

const core = new Core(nodeUrl)
const ckb = new CKB(nodeUrl)

const sk = process.env.PRIV_KEY || '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' // example private key
const pk = core.utils.privateKeyToPublicKey(sk)
const pk = ckb.utils.privateKeyToPublicKey(sk)

const pkh = `0x${core.utils.blake160(pk, 'hex')}`
const addr = core.utils.privateKeyToAddress(sk)
const pkh = `0x${ckb.utils.blake160(pk, 'hex')}`
const addr = ckb.utils.privateKeyToAddress(sk)

const loadCells = async () => {
await core.loadSecp256k1Dep()
const lockHash = core.generateLockHash(
await ckb.loadSecp256k1Dep()
const lockHash = ckb.generateLockHash(
pkh
)
await core.loadCells({
await ckb.loadCells({
lockHash,
start: BigInt(0),
end: BigInt(1000),
Expand All @@ -25,14 +25,14 @@ const loadCells = async () => {

const deposit = async () => {
await loadCells()
const depositTx = await core.generateDaoDepositTransaction({
const depositTx = await ckb.generateDaoDepositTransaction({
fromAddress: addr,
capacity: BigInt(10200000000),
fee: BigInt(100000)
})
const signed = core.signTransaction(sk)(depositTx)
const signed = ckb.signTransaction(sk)(depositTx)

const txHash = await core.rpc.sendTransaction(signed)
const txHash = await ckb.rpc.sendTransaction(signed)
const depositOutPoint = {
txHash,
index: '0x0'
Expand All @@ -46,11 +46,11 @@ const depositOutPoint = {
}

const logDepositEpoch = async () => {
const tx = await core.rpc.getTransaction(depositOutPoint.txHash)
const tx = await ckb.rpc.getTransaction(depositOutPoint.txHash)
if (tx.txStatus.blockHash) {
const b = await core.rpc.getBlock(tx.txStatus.blockHash)
const b = await ckb.rpc.getBlock(tx.txStatus.blockHash)
const epoch = b.header.epoch
console.log(`const depositEpoch = ${JSON.stringify(core.utils.parseEpoch(epoch), null, 2)}`)
console.log(`const depositEpoch = ${JSON.stringify(ckb.utils.parseEpoch(epoch), null, 2)}`)
} else {
console.log('not committed')
}
Expand All @@ -64,12 +64,12 @@ const depositEpoch = {

const starWithdrawing = async () => {
await loadCells()
const tx = await core.generateDaoWithdrawStartTransaction({
const tx = await ckb.generateDaoWithdrawStartTransaction({
outPoint: depositOutPoint,
fee: 10000
})
const signed = core.signTransaction(sk)(tx)
const txHash = await core.rpc.sendTransaction(signed)
const signed = ckb.signTransaction(sk)(tx)
const txHash = await ckb.rpc.sendTransaction(signed)
const outPoint = {
txHash,
index: '0x0'
Expand All @@ -83,11 +83,11 @@ const startWithDrawOutPoint = {
}

const logStartWithdrawingEpoch = async () => {
const tx = await core.rpc.getTransaction(startWithDrawOutPoint.txHash)
const tx = await ckb.rpc.getTransaction(startWithDrawOutPoint.txHash)
if (tx.txStatus.blockHash) {
const b = await core.rpc.getBlock(tx.txStatus.blockHash)
const b = await ckb.rpc.getBlock(tx.txStatus.blockHash)
const epoch = b.header.epoch
console.log(`const startWithdrawingEpoch = ${JSON.stringify(core.utils.parseEpoch(epoch), null, 2)}`)
console.log(`const startWithdrawingEpoch = ${JSON.stringify(ckb.utils.parseEpoch(epoch), null, 2)}`)
} else {
console.log('not committed')
}
Expand All @@ -100,20 +100,20 @@ const startWithdrawingEpoch = {
}

const logCurrentEpoch = async () => {
core.rpc.getTipHeader().then(h => console.log(core.utils.parseEpoch(h.epoch)))
ckb.rpc.getTipHeader().then(h => console.log(ckb.utils.parseEpoch(h.epoch)))
}

const withdraw = async () => {
await core.loadDaoDep()
await core.loadSecp256k1Dep()
await ckb.loadDaoDep()
await ckb.loadSecp256k1Dep()
await loadCells()
const tx = await core.generateDaoWithdrawTransaction({
const tx = await ckb.generateDaoWithdrawTransaction({
depositOutPoint,
withdrawOutPoint: startWithDrawOutPoint,
fee: BigInt(100000)
})
const signed = core.signTransaction(sk)(tx)
const txHash = await core.rpc.sendTransaction(signed)
const signed = ckb.signTransaction(sk)(tx)
const txHash = await ckb.rpc.sendTransaction(signed)
const outPoint = {
txHash,
index: '0x0'
Expand Down
Loading

0 comments on commit 3f9bad5

Please sign in to comment.