Skip to content

Commit

Permalink
feat: add did contrace and switch on dynamic celldep loading
Browse files Browse the repository at this point in the history
  • Loading branch information
liyukun committed Jul 4, 2024
1 parent bae7e6e commit c0307bf
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
31 changes: 28 additions & 3 deletions packages/core/src/api/joints/spore/injectLiveSporeCell.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { BIish } from '@ckb-lumos/bi';
import { PackedSince } from '@ckb-lumos/base';
import { BI, Cell, helpers, HexString } from '@ckb-lumos/lumos';
import { BI, Cell, helpers, HexString, Indexer, Script } from '@ckb-lumos/lumos';
import { addCellDep, parseAddress } from '@ckb-lumos/lumos/helpers';
import { decodeContentType, isContentTypeValid, setAbsoluteCapacityMargin, setupCell } from '../../../helpers';
import {
decodeContentType,
getCellByType,
isContentTypeValid,
setAbsoluteCapacityMargin,
setupCell,
} from '../../../helpers';
import { getSporeConfig, getSporeScript, SporeConfig } from '../../../config';
import { unpackToRawSporeData } from '../../../codec';
import { getMutantById } from '../mutant/getMutant';
Expand Down Expand Up @@ -68,7 +74,26 @@ export async function injectLiveSporeCell(props: {
}

// Add Spore script as cellDep
txSkeleton = addCellDep(txSkeleton, sporeScript.cellDep);
let sporeCelldep = sporeScript.cellDep;
if (sporeScript.behaviors?.dynamicCeldep) {
const args = sporeScript.behaviors?.dynamicCeldep;
const typeIdScript: Script = {
codeHash: '0x00000000000000000000000000000000000000000000000000545950455f4944',
hashType: 'type',
args,
};
const scriptCell = await getCellByType({
type: typeIdScript,
indexer: new Indexer(config.ckbIndexerUrl, config.ckbNodeUrl),
});
if (scriptCell) {
sporeCelldep = {
outPoint: scriptCell.outPoint!,
depType: 'code',
};
}
}
txSkeleton = addCellDep(txSkeleton, sporeCelldep);

// Validate SporeData.contentType
const sporeData = unpackToRawSporeData(sporeCell.data);
Expand Down
25 changes: 22 additions & 3 deletions packages/core/src/api/joints/spore/injectNewSporeOutput.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BIish } from '@ckb-lumos/bi';
import { Script } from '@ckb-lumos/base';
import { bytes, BytesLike } from '@ckb-lumos/codec';
import { Address, BI, Cell, Hash, helpers, HexString, PackedSince } from '@ckb-lumos/lumos';
import { Address, BI, Cell, Hash, helpers, HexString, Indexer, PackedSince } from '@ckb-lumos/lumos';
import { addCellDep } from '@ckb-lumos/lumos/helpers';
import { packRawSporeData } from '../../../codec';
import { getSporeConfig, getSporeScript, SporeConfig } from '../../../config';
import { EncodableContentType, setContentTypeParameters } from '../../../helpers';
import { EncodableContentType, getCellByType, setContentTypeParameters } from '../../../helpers';
import { correctCellMinimalCapacity, setAbsoluteCapacityMargin } from '../../../helpers';
import { composeInputLocks, composeOutputLocks, decodeContentType, isContentTypeValid } from '../../../helpers';
import { getClusterById } from '../cluster/getCluster';
Expand Down Expand Up @@ -223,7 +223,26 @@ export async function injectNewSporeOutput(props: {
}

// Add Spore relevant cellDeps
txSkeleton = addCellDep(txSkeleton, sporeScript.cellDep);
let sporeCelldep = sporeScript.cellDep;
if (sporeScript.behaviors?.dynamicCeldep) {
const args = sporeScript.behaviors?.dynamicCeldep;
const typeIdScript: Script = {
codeHash: '0x00000000000000000000000000000000000000000000000000545950455f4944',
hashType: 'type',
args,
};
const scriptCell = await getCellByType({
type: typeIdScript,
indexer: new Indexer(config.ckbIndexerUrl, config.ckbNodeUrl),
});
if (scriptCell) {
sporeCelldep = {
outPoint: scriptCell.outPoint!,
depType: 'code',
};
}
}
txSkeleton = addCellDep(txSkeleton, sporeCelldep);

return {
txSkeleton,
Expand Down
38 changes: 38 additions & 0 deletions packages/core/src/config/predefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ const TESTNET_SPORE_CONFIG: SporeConfig<PredefinedTestnetSporeScriptName> = {
scripts: {
Spore: {
versions: [
// see: https://github.com/dotbitHQ/did-contracts/blob/docs/docs/Deployed-Scripts.md#testnet
{
tags: ['v2', 'did'],
script: {
codeHash: '0x0b1f412fbae26853ff7d082d422c2bdd9e2ff94ee8aaec11240a5b34cc6e890f',
hashType: 'type',
},
cellDep: {
outPoint: {
txHash: '0x29d0123dda05cb92452a46df609e9bd08e066d62f0c3953fe34935dc10edfdcc',
index: '0x0',
},
depType: 'code',
},
behaviors: {
lockProxy: true,
dynamicCeldep: '0x80f0d4bf6b3951911aa6b98cc609d477a8a10b903b35cfd528d098e95c36f680',
},
},
{
tags: ['v2', 'preview'],
script: {
Expand Down Expand Up @@ -180,6 +199,25 @@ const MAINNET_SPORE_CONFIG: SporeConfig<PredefinedMainnetSporeScriptName> = {
scripts: {
Spore: {
versions: [
// see: https://github.com/dotbitHQ/did-contracts/blob/docs/docs/Deployed-Scripts.md#mainnet
{
tags: ['v2', 'did'],
script: {
codeHash: '0xcfba73b58b6f30e70caed8a999748781b164ef9a1e218424a6fb55ebf641cb33',
hashType: 'type',
},
cellDep: {
outPoint: {
txHash: '0x18dda0f02036305b423b85cce276a40417faed044b2ee9220284215f38734daa',
index: '0x0',
},
depType: 'code',
},
behaviors: {
lockProxy: true,
dynamicCeldep: '0x62312cd846659e188b05da11dc3f080b083c27371ea701d6026e11e713e0e3de',
},
},
{
tags: ['v2', 'latest'],
script: {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Config } from '@ckb-lumos/config-manager';
import { CellDep } from '@ckb-lumos/base';
import { ScriptId } from '../types';
import { ClusterDataVersion } from '../codec';
import { HexString } from '@ckb-lumos/lumos';

export interface SporeConfig<T extends string = string> {
lumos: Config;
Expand Down Expand Up @@ -35,4 +36,5 @@ export interface SporeScriptBehaviors {
lockProxy?: boolean;
cobuild?: boolean;
clusterDataVersion?: ClusterDataVersion;
dynamicCeldep?: HexString;
}

0 comments on commit c0307bf

Please sign in to comment.