Skip to content

Commit

Permalink
Merge pull request #176 from nervina-labs/develop
Browse files Browse the repository at this point in the history
Release v0.6.5
  • Loading branch information
duanyytop authored Sep 29, 2022
2 parents b1a717b + 3aec81f commit 4c21a6c
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3.4.1
- uses: actions/setup-node@v3.5.0
with:
node-version: '14'
- name: Install dependency
Expand Down
4 changes: 2 additions & 2 deletions example/aggregator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"build": "tsc"
},
"dependencies": {
"@nervina-labs/cota-sdk": "0.6.2",
"@nervina-labs/cota-sdk": "0.6.3",
"@types/node": "17.0.21",
"ts-node": "10.9.1",
"tslib": "2.4.0",
"typescript": "4.7.4"
"typescript": "4.8.3"
}
}
16 changes: 8 additions & 8 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].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==
"@nervina-labs/[email protected].3":
version "0.6.3"
resolved "https://registry.yarnpkg.com/@nervina-labs/cota-sdk/-/cota-sdk-0.6.3.tgz#f8606f6d43f3425a24c536a3dd38a71b66e35457"
integrity sha512-Y1gT5VjfU6UwrL+ByCqZeOvYFnJqG1ezZBWDQpay0SwMk+r4d0qL5oDJZ0DSjTxfFzW5yhtwT8fasmblirF0xQ==
dependencies:
"@nervosnetwork/ckb-sdk-core" "^0.103.0"
"@nervosnetwork/ckb-sdk-utils" "^0.103.0"
Expand Down Expand Up @@ -346,10 +346,10 @@ type-fest@^1.2.1:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-1.4.0.tgz#e9fb813fe3bf1744ec359d55d1affefa76f14be1"
integrity sha512-yGSza74xk0UG8k+pLh5oeoYirvIiWo5t0/o3zHHAO2tRDiZcxWP7fywNlXhqb6/r6sWvwi+RsyQMWhVLe4BVuA==

typescript@4.7.4:
version "4.7.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.4.tgz#1a88596d1cf47d59507a1bcdfb5b9dfe4d488235"
integrity sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==
typescript@4.8.3:
version "4.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.3.tgz#d59344522c4bc464a65a730ac695007fdb66dd88"
integrity sha512-goMHfm00nWPa8UvR/CPSvykqf6dVV8x/dp0c5mFTMTIu0u0FlGWRioyy7Nn0PGAdHxpJZnuO/ut+PpQ8UiHAig==

v8-compile-cache-lib@^3.0.1:
version "3.0.1"
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.2"
"@nervina-labs/cota-sdk": "0.6.3"
}
}
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].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==
"@nervina-labs/[email protected].3":
version "0.6.3"
resolved "https://registry.yarnpkg.com/@nervina-labs/cota-sdk/-/cota-sdk-0.6.3.tgz#f8606f6d43f3425a24c536a3dd38a71b66e35457"
integrity sha512-Y1gT5VjfU6UwrL+ByCqZeOvYFnJqG1ezZBWDQpay0SwMk+r4d0qL5oDJZ0DSjTxfFzW5yhtwT8fasmblirF0xQ==
dependencies:
"@nervosnetwork/ckb-sdk-core" "^0.103.0"
"@nervosnetwork/ckb-sdk-utils" "^0.103.0"
Expand Down
51 changes: 51 additions & 0 deletions example/update-cota-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { addressToScript } from '@nervosnetwork/ckb-sdk-utils'
import { Collector } from '../src/collector'
import { Aggregator } from '../src/aggregator'
import { Service, FEE, CotaInfo } from '../src'
import { generateCotaMetadataTx } from '../src/service/cota/cota-meta'

const TEST_PRIVATE_KEY = '0xc5bd09c9b954559c70a77d68bde95369e2ce910556ddc20f739080cde3b62ef2'
const TEST_ADDRESS = 'ckt1qyq0scej4vn0uka238m63azcel7cmcme7f2sxj5ska'

const secp256k1CellDep = (isMainnet: boolean): CKBComponents.CellDep => {
if (isMainnet) {
return { outPoint: {
txHash: "0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c",
index: "0x0",
}, depType: 'depGroup' }
}
return { outPoint: {
txHash: "0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37",
index: "0x0",
}, depType: 'depGroup' }
}

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()
const cotaLock = addressToScript(TEST_ADDRESS)

const cotaInfo: CotaInfo = {
name: 'Update First Step',
description:
'First step to Blockchain mass adoption. NFT platform launch memento.\n\n-- Nervina Labs & Lay2 Tech, 4/30/2021.',
image: 'https://i.loli.net/2021/04/29/qyJNSE4iHAas7GL.png',
}
const cotaId = '0x36cd7eb40416a61b1f90631b4ddfedd95aea6820'

let rawTx = await generateCotaMetadataTx(service, cotaLock, cotaId, cotaInfo, FEE, isMainnet)
rawTx.cellDeps.push(secp256k1CellDep(isMainnet))

const signedTx = ckb.signTransaction(TEST_PRIVATE_KEY)(rawTx)
console.log(JSON.stringify(signedTx))
let txHash = await ckb.rpc.sendTransaction(signedTx, 'passthrough')
console.info(`Update cota metadata information tx has been sent with tx hash ${txHash}`)
}

run()
8 changes: 4 additions & 4 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.3",
"version": "0.6.5",
"description": "The SDK of CoTA",
"repository": "[email protected]:nervina-labs/cota-sdk-js.git",
"author": "duanyytop <[email protected]>",
Expand Down Expand Up @@ -34,17 +34,17 @@
"devDependencies": {
"@types/crypto-js": "4.1.1",
"@types/node": "17.0.31",
"@typescript-eslint/parser": "5.33.0",
"@typescript-eslint/parser": "5.38.1",
"babel-eslint": "10.1.0",
"eslint": "8.21.0",
"eslint": "8.24.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-prettier": "4.2.1",
"husky": "8.0.1",
"prettier": "2.7.1",
"ts-node": "10.9.1",
"tslib": "2.4.0",
"typescript": "4.7.4"
"typescript": "4.8.3"
},
"homepage": "https://github.com/nervina-labs/cota-sdk-js#readme"
}
69 changes: 69 additions & 0 deletions src/service/cota/cota-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Service } from '../..'
import { FEE, getCotaTypeScript, getCotaCellDep } from '../../constants'
import { CotaInfo, Hex } from '../../types'
import { append0x, utf8ToHex, toSnakeCase } from '../../utils'

const generateCotaMetadata = (cotaInfo: CotaInfo, cotaId: Hex): Hex => {
const cotaInfoTemp = {
cotaId,
...cotaInfo,
}
const cotaMeta = {
id: 'CTMeta',
ver: '1.0',
metadata: {
target: 'output#0',
type: 'cota',
data: {
version: '0',
...cotaInfoTemp,
},
},
}
return append0x(utf8ToHex(JSON.stringify(toSnakeCase(cotaMeta))))
}

export const generateCotaMetadataTx = async (
service: Service,
cotaLock: CKBComponents.Script,
cotaId: Hex,
cotaInfo: CotaInfo,
fee = FEE,
isMainnet = false,
) => {
const cotaType = getCotaTypeScript(isMainnet)
const cotaCells = await service.collector.getCells(cotaLock, cotaType)
if (!cotaCells || cotaCells.length === 0) {
throw new Error("Cota cell doesn't exist")
}

const cotaCell = cotaCells[0]
const inputs = [
{
previousOutput: cotaCell.outPoint,
since: '0x0',
},
]

const outputs = [cotaCell.output]
outputs[0].capacity = `0x${(BigInt(outputs[0].capacity) - fee).toString(16)}`

const cotaOutput = await service.collector.getLiveCell(cotaCell.outPoint)
const outputsData = [cotaOutput.data?.content]

const cellDeps = [getCotaCellDep(isMainnet)]

const rawTx: any = {
version: '0x0',
cellDeps,
headerDeps: [],
inputs,
outputs,
outputsData,
witnesses: [],
}
rawTx.witnesses = rawTx.inputs.map((_, i) =>
i > 0 ? '0x' : { lock: '', inputType: '', outputType: generateCotaMetadata(cotaInfo, cotaId) },
)
return rawTx
}
10 changes: 6 additions & 4 deletions src/service/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const generateCotaOutputs = async (
inputCapacity: bigint,
cotaLocks: CKBComponents.Script[],
changeLock: CKBComponents.Script,
fee: bigint,
isMainnet: boolean,
): Promise<CKBComponents.CellOutput[]> => {
let outputs: CKBComponents.CellOutput[] = cotaLocks.map(lock => {
Expand All @@ -29,7 +30,7 @@ const generateCotaOutputs = async (
})

const cotaCellsLength = BigInt(cotaLocks.length)
const changeCapacity = inputCapacity - FEE - COTA_CELL_CAPACITY * cotaCellsLength
const changeCapacity = inputCapacity - fee - COTA_CELL_CAPACITY * cotaCellsLength
outputs.push({
capacity: `0x${changeCapacity.toString(16)}`,
lock: changeLock,
Expand Down Expand Up @@ -68,10 +69,10 @@ export const generateRegisterCotaTx = async (
]
inputs = inputs.concat(normalInputs)

let outputs = await generateCotaOutputs(capacity, cotaLocks, lock, isMainnet)
let outputs = await generateCotaOutputs(capacity, cotaLocks, lock, fee, isMainnet)
outputs = [registryCell.output].concat(outputs)
const length = outputs.length
outputs[length - 1].capacity = `0x${(BigInt(outputs[length - 1].capacity) - FEE).toString(16)}`
outputs[length - 1].capacity = `0x${(BigInt(outputs[length - 1].capacity) - fee).toString(16)}`

const lockHashes = cotaLocks.map(lock => scriptToHash(lock))
const { smtRootHash, registrySmtEntry, outputAccountNum } = await service.aggregator.generateRegisterCotaSmt(lockHashes)
Expand Down Expand Up @@ -99,6 +100,7 @@ export const generateRegisterCotaTx = async (

export const generateUpdateCcidsTx = async (
service: Service,
fee = FEE,
isMainnet = false,
): Promise<CKBComponents.RawTransactionToSign> => {
const registryLock = getAlwaysSuccessLock(isMainnet)
Expand All @@ -116,7 +118,7 @@ export const generateUpdateCcidsTx = async (
]

let outputs = [registryCell.output]
outputs[0].capacity = `0x${(BigInt(outputs[0].capacity) - FEE).toString(16)}`
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))}`
Expand Down
Loading

0 comments on commit 4c21a6c

Please sign in to comment.