Skip to content

Commit

Permalink
Merge pull request #187 from nervina-labs/develop
Browse files Browse the repository at this point in the history
Release v0.7.1
  • Loading branch information
duanyytop authored Nov 3, 2022
2 parents 1d91a9d + 75744c9 commit 89a25db
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 89 deletions.
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.3",
"@nervina-labs/cota-sdk": "0.6.6",
"@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].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==
"@nervina-labs/[email protected].6":
version "0.6.6"
resolved "https://registry.yarnpkg.com/@nervina-labs/cota-sdk/-/cota-sdk-0.6.6.tgz#cf9b164ec49472bed7dbfec1b0e52472f1d71149"
integrity sha512-H+UXxKx/H5qYbjT0osCgRrjOuSQyoXeeJkJw6IeIPx3F0P0kBR+CHuP8Hk6sC0Zan+NLvKRwnrWedBkcyTNOGw==
dependencies:
"@nervosnetwork/ckb-sdk-core" "^0.103.0"
"@nervosnetwork/ckb-sdk-utils" "^0.103.0"
Expand Down
53 changes: 53 additions & 0 deletions example/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { addressToScript } from '@nervosnetwork/ckb-sdk-utils'
import { Collector } from '../src/collector'
import { Aggregator } from '../src/aggregator'
import { Service } from '../src'
import { generateAddExtensionTx } from '../src/service/cota/extension'

const TEST_PRIVATE_KEY = '0xee56672e70cec79941adc0637c1edb1546a0c39c72eff8c41bc4f1e03bf663b3'
const TEST_ADDRESS = 'ckt1qyq897k5m53wxzup078jwkucvvsu8kzv55rqqm6glm'

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

let rawTx = await generateAddExtensionTx(service, extensionLock, BigInt(6000), 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(`Add cota extension tx has been sent with tx hash ${txHash}`)
}

run()
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.3"
"@nervina-labs/cota-sdk": "0.6.6"
}
}
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].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==
"@nervina-labs/[email protected].6":
version "0.6.6"
resolved "https://registry.yarnpkg.com/@nervina-labs/cota-sdk/-/cota-sdk-0.6.6.tgz#cf9b164ec49472bed7dbfec1b0e52472f1d71149"
integrity sha512-H+UXxKx/H5qYbjT0osCgRrjOuSQyoXeeJkJw6IeIPx3F0P0kBR+CHuP8Hk6sC0Zan+NLvKRwnrWedBkcyTNOGw==
dependencies:
"@nervosnetwork/ckb-sdk-core" "^0.103.0"
"@nervosnetwork/ckb-sdk-utils" "^0.103.0"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nervina-labs/cota-sdk",
"version": "0.6.6",
"version": "0.7.1",
"description": "The SDK of CoTA",
"repository": "[email protected]:nervina-labs/cota-sdk-js.git",
"author": "duanyytop <[email protected]>",
Expand Down
6 changes: 6 additions & 0 deletions src/aggregator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
GetDefineInfoReq,
GetIssuerInfoReq,
GetCotaCountReq,
ExtensionReq,
} from '../types/request'
import {
ClaimResp,
Expand All @@ -37,6 +38,7 @@ import {
GetDefineInfoResp,
GetIssuerInfoResp,
GetCotaCountResp,
ExtensionResp,
} from '../types/response'
import { Byte32 } from '../types/common'

Expand Down Expand Up @@ -159,6 +161,10 @@ export class Aggregator {
async getCotaCount(req: GetCotaCountReq): Promise<GetCotaCountResp> {
return (await this.baseRPC('get_cota_count', req)) as Promise<GetCotaCountResp>
}

async generateExtensionSmt(extension: ExtensionReq): Promise<ExtensionResp> {
return (await this.baseRPC('generate_extension_smt', extension)) as Promise<ExtensionResp>
}
}

const convert = (req: GetCotaReq) => ({
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: '0xad4b1985dde9f13cd6c3a8d875640b8e9a476542bc5d87c66220730ac694a350', index: '0x0' },
outPoint: { txHash: '0xd8c7396f955348bd74a8ed4398d896dad931977b7c1e3f117649765cd3d75b86', index: '0x0' },
depType: 'depGroup',
} as CKBComponents.CellDep,

Expand Down
4 changes: 3 additions & 1 deletion src/service/cota/claim-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export const generateClaimUpdateCotaTx = async (
nfts,
}

const { smtRootHash, claimUpdateSmtEntry, withdrawBlockHash } = await service.aggregator.generateClaimUpdateCotaSmt(claimUpdateReq)
const { smtRootHash, claimUpdateSmtEntry, withdrawBlockHash } = await service.aggregator.generateClaimUpdateCotaSmt(
claimUpdateReq,
)
const outputsData = [`0x02${smtRootHash}`]
const cellDeps = [getCotaCellDep(isMainnet)]
const headerDeps = [`0x${withdrawBlockHash}`]
Expand Down
138 changes: 69 additions & 69 deletions src/service/cota/cota-meta.ts
Original file line number Diff line number Diff line change
@@ -1,69 +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
}
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
}
76 changes: 76 additions & 0 deletions src/service/cota/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { serializeScript } from '@nervosnetwork/ckb-sdk-utils'
import { Service } from '../..'
import { FEE, getCotaTypeScript, getCotaCellDep } from '../../constants'
import { ExtensionReq } from '../../types'


enum Action {
Add,
Update
}

const generateExtensionTx = async (
service: Service,
cotaLock: CKBComponents.Script,
fee = FEE,
isMainnet = false,
action: Action,
) => {
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 extensionReq: ExtensionReq = {
lockScript: serializeScript(cotaLock),
}

const { smtRootHash, extensionSmtEntry } = await service.aggregator.generateExtensionSmt(extensionReq)
const cotaCellData = `0x02${smtRootHash}`

const outputsData = [cotaCellData]
const cellDeps = [getCotaCellDep(isMainnet)]

const rawTx = {
version: '0x0',
cellDeps,
headerDeps: [],
inputs,
outputs,
outputsData,
witnesses: [],
} as any

const prefix = action == Action.Add ? '0xF0' : '0xF1'
rawTx.witnesses = rawTx.inputs.map((_, i) =>
i > 0 ? '0x' : { lock: '', inputType: `${prefix}${extensionSmtEntry}`, outputType: '' },
)
return rawTx
}


export const generateAddExtensionTx = async (
service: Service,
cotaLock: CKBComponents.Script,
fee = FEE,
isMainnet = false
) => await generateExtensionTx(service, cotaLock, fee, isMainnet, Action.Add)


export const generateUpdateExtensionTx = async (
service: Service,
cotaLock: CKBComponents.Script,
fee = FEE,
isMainnet = false
) => await generateExtensionTx(service, cotaLock, fee, isMainnet, Action.Update)
5 changes: 2 additions & 3 deletions src/service/cota/transfer-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ export const generateTransferUpdateCotaTx = async (
transferOutPoint: append0x(serializeOutPoint(cotaCell.outPoint).slice(26)),
transfers,
}
const { smtRootHash, transferUpdateSmtEntry, withdrawBlockHash } = await service.aggregator.generateTransferUpdateCotaSmt(
transferUpdateReq,
)
const { smtRootHash, transferUpdateSmtEntry, withdrawBlockHash } =
await service.aggregator.generateTransferUpdateCotaSmt(transferUpdateReq)
const outputsData = [`0x02${smtRootHash}`]

const cellDeps = [getCotaCellDep(isMainnet)]
Expand Down
Loading

0 comments on commit 89a25db

Please sign in to comment.