Skip to content

Commit

Permalink
Merge pull request #167 from nervina-labs/develop
Browse files Browse the repository at this point in the history
Release v0.6.3
  • Loading branch information
duanyytop authored Sep 15, 2022
2 parents 9319a05 + 0c10721 commit b1a717b
Show file tree
Hide file tree
Showing 13 changed files with 567 additions and 365 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,25 @@ CoTA SDK needs to run the services as blow:
### Public ckb node url and ckb indexer url as blow can be used to develop and test

```
mainnet:
# mainnet:
https://mainnet.ckbapp.dev/rpc ---> ckb mainnet node rpc
https://mainnet.ckbapp.dev/indexer ---> ckb mainnet indexer rpc
testnet:
# testnet:
https://testnet.ckbapp.dev/rpc ---> ckb testnet node rpc
https://testnet.ckbapp.dev/indexer ---> ckb testnet indexer rpc
```

### Public aggregator rpc url as blow can be used to develop and test

```
testnet:
https://cota.nervina.dev/aggregator ---> cota aggregator rpc
https://cota.nervina.dev/registry-aggregator ---> cota registry aggregator rpc
# mainnet:
https://cota.nervina.dev/mainnet-aggregator ---> cota aggregator rpc
https://cota.nervina.dev/mainnet-registry-aggregator ---> cota registry aggregator rpc
# testnet:
https://cota.nervina.dev/aggregator ---> cota aggregator rpc
https://cota.nervina.dev/registry-aggregator ---> cota registry aggregator rpc
```

## CoTA NFT Flow
Expand Down
2 changes: 1 addition & 1 deletion example/aggregator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "tsc"
},
"dependencies": {
"@nervina-labs/cota-sdk": "0.6.1",
"@nervina-labs/cota-sdk": "0.6.2",
"@types/node": "17.0.21",
"ts-node": "10.9.1",
"tslib": "2.4.0",
Expand Down
8 changes: 4 additions & 4 deletions example/aggregator/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@nervina-labs/[email protected].1":
version "0.6.1"
resolved "https://registry.yarnpkg.com/@nervina-labs/cota-sdk/-/cota-sdk-0.6.1.tgz#acd00d9529d36c1a9d4ff310864194f824dd160f"
integrity sha512-rKI7R4tIiO19n0sQS6z2SjiIP5cw1RUDQL1l66ETI1db0zOd8vq7qs/LXe7ycT7un4ayS2oNJllf3SrWCiNcGw==
"@nervina-labs/[email protected].2":
version "0.6.2"
resolved "https://registry.yarnpkg.com/@nervina-labs/cota-sdk/-/cota-sdk-0.6.2.tgz#1e111f51545c9d5037966c10df40c784b2aa88e3"
integrity sha512-87+3TKr8rvDlpAnIMnwHNkyHskgfyuABJJBv/wvwoaXYKGRSodkVSDXkakNzIx2Gg1x0iLYwmX86Xl3d6+UwcQ==
dependencies:
"@nervosnetwork/ckb-sdk-core" "^0.103.0"
"@nervosnetwork/ckb-sdk-utils" "^0.103.0"
Expand Down
2 changes: 1 addition & 1 deletion example/mainnet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"description": "The examples for mainnet",
"license": "MIT",
"dependencies": {
"@nervina-labs/cota-sdk": "0.6.1"
"@nervina-labs/cota-sdk": "0.6.2"
}
}
8 changes: 4 additions & 4 deletions example/mainnet/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@nervina-labs/[email protected].1":
version "0.6.1"
resolved "https://registry.yarnpkg.com/@nervina-labs/cota-sdk/-/cota-sdk-0.6.1.tgz#acd00d9529d36c1a9d4ff310864194f824dd160f"
integrity sha512-rKI7R4tIiO19n0sQS6z2SjiIP5cw1RUDQL1l66ETI1db0zOd8vq7qs/LXe7ycT7un4ayS2oNJllf3SrWCiNcGw==
"@nervina-labs/[email protected].2":
version "0.6.2"
resolved "https://registry.yarnpkg.com/@nervina-labs/cota-sdk/-/cota-sdk-0.6.2.tgz#1e111f51545c9d5037966c10df40c784b2aa88e3"
integrity sha512-87+3TKr8rvDlpAnIMnwHNkyHskgfyuABJJBv/wvwoaXYKGRSodkVSDXkakNzIx2Gg1x0iLYwmX86Xl3d6+UwcQ==
dependencies:
"@nervosnetwork/ckb-sdk-core" "^0.103.0"
"@nervosnetwork/ckb-sdk-utils" "^0.103.0"
Expand Down
49 changes: 49 additions & 0 deletions example/registry-ccids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { rawTransactionToHash, scriptToHash, serializeWitnessArgs } from '@nervosnetwork/ckb-sdk-utils'
import { Collector } from '../src/collector'
import { Aggregator } from '../src/aggregator'
import { getAlwaysSuccessLock } from '../src/constants'
import { generateUpdateCcidsTx } from '../src/service/registry'
import { Service } from '../src'
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses'


const run = async () => {
// True for mainnet and false for testnet
const isMainnet = false

const service: Service = {
collector: new Collector({ ckbNodeUrl: 'https://testnet.ckb.dev/rpc', ckbIndexerUrl: 'https://testnet.ckb.dev/indexer' }),
aggregator: new Aggregator({ registryUrl: 'http://localhost:3050', cotaUrl: 'http://localhost:3030' }),
}
const ckb = service.collector.getCkb()

let rawTx = await generateUpdateCcidsTx(service, isMainnet)

const registryLock = getAlwaysSuccessLock(isMainnet)

let keyMap = new Map<string, string>()
keyMap.set(scriptToHash(registryLock), '')

const cells = rawTx.inputs.map((input, _) => ({
outPoint: input.previousOutput,
lock: registryLock,
}))

const transactionHash = rawTransactionToHash(rawTx)

const signedWitnesses = signWitnesses(keyMap)({
transactionHash,
witnesses: rawTx.witnesses,
inputCells: cells,
skipMissingKeys: true,
})
const signedTx = {
...rawTx,
witnesses: signedWitnesses.map(witness => (typeof witness === 'string' ? witness : serializeWitnessArgs(witness))),
}
console.log(JSON.stringify(signedTx))
let txHash = await ckb.rpc.sendTransaction(signedTx, 'passthrough')
console.log(`Update registered ccids tx has been sent with tx hash ${txHash}`)
}

run()
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nervina-labs/cota-sdk",
"version": "0.6.2",
"version": "0.6.3",
"description": "The SDK of CoTA",
"repository": "[email protected]:nervina-labs/cota-sdk-js.git",
"author": "duanyytop <[email protected]>",
Expand Down Expand Up @@ -34,9 +34,9 @@
"devDependencies": {
"@types/crypto-js": "4.1.1",
"@types/node": "17.0.31",
"@typescript-eslint/parser": "5.30.7",
"@typescript-eslint/parser": "5.33.0",
"babel-eslint": "10.1.0",
"eslint": "8.14.0",
"eslint": "8.21.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-prettier": "4.2.1",
Expand Down
8 changes: 6 additions & 2 deletions src/aggregator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export class Aggregator {
this.cotaUrl = cotaUrl
}

private async baseRPC(method: string, req: SmtReq, url = this.cotaUrl): Promise<SmtResp | undefined> {
private async baseRPC(method: string, req: SmtReq | undefined, url = this.cotaUrl): Promise<SmtResp | undefined> {
let payload = {
id: payloadId(),
jsonrpc: '2.0',
method,
params: toSnakeCase(req),
params: req ? toSnakeCase(req) : null,
}
const body = JSON.stringify(payload, null, '')
console.log(body)
Expand Down Expand Up @@ -84,6 +84,10 @@ export class Aggregator {
return (await this.baseRPC('register_cota_cells', lockHashes, this.registryUrl)) as Promise<RegistryResp>
}

async generateUpdateCcidsSmt(): Promise<RegistryResp> {
return (await this.baseRPC('update_registered_ccid', undefined, this.registryUrl)) as Promise<RegistryResp>
}

async checkReisteredLockHashes(lockHashes: Byte32[]): Promise<CheckRegisteredResp> {
return (await this.baseRPC(
'check_registered_lock_hashes',
Expand Down
2 changes: 1 addition & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const TestnetInfo = {
} as CKBComponents.Script,

CotaTypeDep: {
outPoint: { txHash: '0xfa683440f605af7cc117755f8bcf6acec70fc4a69265602117810dfa41444159', index: '0x0' },
outPoint: { txHash: '0xec82a5668238bea872bd9beb865fdf33568478b4a12057776bf1cb392a47ecb2', index: '0x0' },
depType: 'depGroup',
} as CKBComponents.CellDep,

Expand Down
48 changes: 45 additions & 3 deletions src/service/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getAlwaysSuccessCellDep,
getCotaCellDep,
} from '../../constants'
import { append0x, remove0x } from '../../utils/hex'
import { append0x, remove0x, u64ToBe } from '../../utils/hex'

const COTA_CELL_CAPACITY = BigInt(150) * BigInt(100000000)

Expand Down Expand Up @@ -74,8 +74,8 @@ export const generateRegisterCotaTx = async (
outputs[length - 1].capacity = `0x${(BigInt(outputs[length - 1].capacity) - FEE).toString(16)}`

const lockHashes = cotaLocks.map(lock => scriptToHash(lock))
const { smtRootHash, registrySmtEntry } = await service.aggregator.generateRegisterCotaSmt(lockHashes)
const registryCellData = `0x00${smtRootHash}`
const { smtRootHash, registrySmtEntry, outputAccountNum } = await service.aggregator.generateRegisterCotaSmt(lockHashes)
const registryCellData = `0x01${smtRootHash}${u64ToBe(BigInt(outputAccountNum))}`

const outputsData = outputs.map((_, i) => (i === 0 ? registryCellData : i !== outputs.length - 1 ? '0x02' : '0x'))

Expand All @@ -95,3 +95,45 @@ export const generateRegisterCotaTx = async (
rawTx.witnesses = rawTx.inputs.map((_, i) => (i === 0 ? registryWitness : i === 1 ? emptyWitness : '0x'))
return rawTx
}


export const generateUpdateCcidsTx = async (
service: Service,
isMainnet = false,
): Promise<CKBComponents.RawTransactionToSign> => {
const registryLock = getAlwaysSuccessLock(isMainnet)
const registryType = getRegistryTypeScript(isMainnet)
const registryCells = await service.collector.getCells(registryLock, registryType)
if (!registryCells || registryCells.length === 0) {
throw new Error("Registry cell doesn't exist")
}
let registryCell = registryCells[0]
let inputs = [
{
previousOutput: registryCell.outPoint,
since: '0x0',
},
]

let outputs = [registryCell.output]
outputs[0].capacity = `0x${(BigInt(outputs[0].capacity) - FEE).toString(16)}`

const { smtRootHash, registrySmtEntry, outputAccountNum } = await service.aggregator.generateUpdateCcidsSmt()
const registryCellData = `0x01${smtRootHash}${u64ToBe(BigInt(outputAccountNum))}`

const outputsData = [registryCellData]

const cellDeps = [getAlwaysSuccessCellDep(isMainnet), getCotaCellDep(isMainnet)]
const registryWitness = serializeWitnessArgs({ lock: '', inputType: append0x(registrySmtEntry), outputType: '' })

let rawTx = {
version: '0x0',
cellDeps,
headerDeps: [],
inputs,
outputs,
outputsData,
witnesses: [registryWitness],
}
return rawTx
}
1 change: 1 addition & 0 deletions src/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface SmtResp {}
export interface RegistryResp {
smtRootHash: Byte32
registrySmtEntry: Bytes
outputAccountNum: bigint
blockNumber: bigint
}

Expand Down
10 changes: 10 additions & 0 deletions src/utils/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ export const u64ToLe = (u64: bigint): string => {
return `${viewLeft}${viewRight}`
}

export const u64ToBe = (u64: bigint): string => {
if (typeof u64 !== 'bigint') {
throw new Error('u64 must be bigint')
}
const val = u64.toString(16).padStart(16, '0')
const viewLeft = u32ToBe(`0x${val.slice(0, 8)}`)
const viewRight = u32ToBe(`0x${val.slice(8)}`)
return `${viewLeft}${viewRight}`
}

export const utf8ToHex = (text: string) => {
let result = text.trim()
if (result.startsWith('0x')) {
Expand Down
Loading

0 comments on commit b1a717b

Please sign in to comment.