Skip to content

Commit

Permalink
✨ Migrate to Omnilock (#13)
Browse files Browse the repository at this point in the history
What's changed:

- Switch to the deployment information of Omnilock
- args: append 00. It is the omni lock flag byte, which indicate there's
  no advanced omni lock features been used.
- `WitnessArgs.lock` size: 65 to 85, this is the size of a
`OmnilockWitnessLock` to only
  save a 65-byte signature.
- witness: instead of storing the 65-byte signature directly into
  `WitnessArgs.lock`, pack it into a `OmnilockWitnessLock`.

Omnilock Contract Error Codes For Reference:

- 8: args format is wrong
- 5: `WitnessArgs.lock` is not a valid `OmnilockWitnessLock`

[An example deposit in
testnet](https://pudge.explorer.nervos.org/transaction/0x7bc9bd9a60852bf85d7142041867fb3d92b44a0d5821835fef9b1978442e84c0)

Omnilock info:

| parameter | value |
| ----------- |
-------------------------------------------------------------------- |
| `code_hash` |
0xf329effd1c475a2978453c8600e1eaf0bc2087ee093c3ee64cc96ec6847752cb |
| `hash_type` | `type` |
| `tx_hash` |
0xff234bf2fb0ad2ab5b356ceda317d3dee3efb2c55b9427ef55d9dcbf6eecbf9f |
| `index` | `0x0` |
| `dep_type` | `code` |

Related PRs:
nervosnetwork/ckb-production-scripts#75

Closes #12
  • Loading branch information
doitian authored Jan 10, 2024
1 parent daa5fa1 commit e0c19cc
Show file tree
Hide file tree
Showing 19 changed files with 107 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.env.*
/migrations/dev
/migrations/joyid
/migrations/ckb_auth
/migrations/omnilock

# CKB
/ckb-miner.toml
Expand Down
2 changes: 1 addition & 1 deletion bin/deploy-to-dev-chain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bin/generate-blocks.sh 4
sleep 1

# try twice in case the indexer has not updated yet
deploy ckb_auth || deploy ckb_auth
deploy omnilock || deploy omnilock
bin/generate-blocks.sh 4

bin/use-env.sh >.env
7 changes: 7 additions & 0 deletions bin/dev-faucet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -e
set -u
[ -n "${DEBUG:-}" ] && set -x || true

ckb-cli wallet transfer --skip-check-to-address --to-address "$1" --capacity 300000 --privkey-path specs/miner.key
3 changes: 1 addition & 2 deletions bin/download-contracts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ download joyid_dep3 0x95ecf9b41701b45d431657a67bbfa3f07ef7ceb53bf87097f3674e1a4a
# download joyid_dep4 0x8f8c79eb6671709633fe6a46de93c0fedc9c1b8a6527a18d3983879542635c9f 3
download joyid_dep5 0x8b3255491f3c4dcc1cfca33d5c6bcaec5409efe4bbda243900f9580c47e0242e 1

download ckb_auth 0xd4f72f0504373ff8effadf44f92c46a0062774fb585ebcacc24eb47b98e2d66a 0
download unisat_lock 0xe842b43df31c92d448fa345d60a6df3e03aaab19ef88921654bf95c673a26872 0
download omnilock 0xff234bf2fb0ad2ab5b356ceda317d3dee3efb2c55b9427ef55d9dcbf6eecbf9f 0
10 changes: 5 additions & 5 deletions bin/use-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ case "${1:-}" in
esac

JOYID_INFO_FILE="$(ls migrations/joyid/*.json | grep -v deployment | head -n 1)"
CKB_AUTH_INFO_FILE="$(ls migrations/ckb_auth/*.json | grep -v deployment | head -n 1)"
OMNILOCK_INFO_FILE="$(ls migrations/omnilock/*.json | grep -v deployment | head -n 1)"

sed -n \
-e 's/,$//' \
Expand All @@ -38,10 +38,10 @@ sed -n \

sed -n \
-e 's/,$//' \
-e 's/^ *"type_id": "/NEXT_PUBLIC_UNISAT_CODE_HASH="/p' \
"$CKB_AUTH_INFO_FILE" | head -1
-e 's/^ *"type_id": "/NEXT_PUBLIC_OMNILOCK_CODE_HASH="/p' \
"$OMNILOCK_INFO_FILE" | head -1

sed -n \
-e 's/,$//' \
-e 's/^ *"tx_hash": /NEXT_PUBLIC_AUTH_TX_HASH=/p' \
"$CKB_AUTH_INFO_FILE" | tail -1
-e 's/^ *"tx_hash": /NEXT_PUBLIC_OMNILOCK_TX_HASH=/p' \
"$OMNILOCK_INFO_FILE" | tail -1
2 changes: 1 addition & 1 deletion docs/dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Connect a wallet and copy the CKB address displayed at the top of the page.
Transfer some CKB tokens from the miner account to the copied address. Replace `ckbt1qz...` with the real address in the following command.

```bash
ckb-cli wallet transfer --skip-check-to-address --to-address ckt1qz... --capacity 300000 --privkey-path specs/miner.key
bin/dev-faucet.sh ckt1qz...
```

Mine some blocks to commit the transfer transaction.
Expand Down
19 changes: 0 additions & 19 deletions migrations/templates/ckb_auth.toml

This file was deleted.

10 changes: 10 additions & 0 deletions migrations/templates/omnilock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[[cells]]
name = "omnilock"
enable_type_id = true
location = { file = "build/release/omnilock" }

# The lock script set to output cells
[lock]
code_hash = "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"
args = "0xc8328aabcd9b9e8e64fbc566c4385c3bdeb219d7"
hash_type = "type"
7 changes: 5 additions & 2 deletions src/lib/cobuild/fee-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ function storeWitnessForFeeEstimation(
// Variable length, but 500 is usually enough.
() => bytes.hexify(new Uint8Array(500)),
);
} else if (script.codeHash === ckbChainConfig.SCRIPTS.UNISAT.CODE_HASH) {
} else if (
script.codeHash === ckbChainConfig.SCRIPTS.OMNILOCK_CUSTOM.CODE_HASH
) {
return generalLockActions.storeWitnessForFeeEstimation(
buildingPacket,
scriptHash,
inputIndices,
() => `0x${"0".repeat(65 * 2)}`,
// 85 = 65 signature in OmnilockWitnessLock
() => `0x${"0".repeat(85 * 2)}`,
);
}

Expand Down
7 changes: 5 additions & 2 deletions src/lib/cobuild/lock-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ function dispatchLockActions(
},
() => `0x${"0".repeat(129 * 2)}`,
);
} else if (script.codeHash === ckbChainConfig.SCRIPTS.UNISAT.CODE_HASH) {
} else if (
script.codeHash === ckbChainConfig.SCRIPTS.OMNILOCK_CUSTOM.CODE_HASH
) {
return generalLockActions.prepareLockActionWithWitnessStore(
buildingPacket,
scriptHash,
Expand All @@ -99,7 +101,8 @@ function dispatchLockActions(
inputIndices,
},
},
() => `0x${"0".repeat(65 * 2)}`,
// 85 = 65 signature in OmnilockWitnessLock
() => `0x${"0".repeat(85 * 2)}`,
);
}

Expand Down
10 changes: 10 additions & 0 deletions src/lib/cobuild/react/building-packet-review.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ export function TxSection({

return (
<dl className="divide-y divide-gray-100">
{process.env.DEBUG !== undefined ? (
<div className="x-4 py-3 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0">
<dt className="leading-6 text-gray-900">Hash</dt>
<dd className="text-gray-700 sm:col-span-2 sm:mt-0 break-all">
<pre className="font-mono p-4 bg-slate-800 text-slate-300 rounded overflow-scroll">
{JSON.stringify(buildingPacket, null, 2)}
</pre>
</dd>
</div>
) : null}
<div className="x-4 py-3 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0">
<dt className="leading-6 text-gray-900">Hash</dt>
<dd className="text-gray-700 sm:col-span-2 sm:mt-0 break-all">
Expand Down
30 changes: 12 additions & 18 deletions src/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const CKB_CHAINS_CONFIGS = {
EXPLORER_URL: "https://pudge.explorer.nervos.org",
SCRIPTS: {
...predefined.AGGRON4.SCRIPTS,

JOYID: {
CODE_HASH:
"0xd23761b364210735c19c60561d213fb3beae2fd6172743719eff6920e020baac",
Expand All @@ -15,18 +16,14 @@ const CKB_CHAINS_CONFIGS = {
INDEX: "0x0",
DEP_TYPE: "depGroup",
},
AUTH: {
TX_HASH:
"0xd4f72f0504373ff8effadf44f92c46a0062774fb585ebcacc24eb47b98e2d66a",
INDEX: "0x0",
DEP_TYPE: "code",
},
UNISAT: {
// Disable OMNILOCK bundled in lumos
OMNILOCK: undefined,
OMNILOCK_CUSTOM: {
CODE_HASH:
"0xd7aac16927b2d572b3803c1f68e49d082d3acc2af2614c9be752ff9cec5dc3ea",
HASH_TYPE: "data1",
"0xf329effd1c475a2978453c8600e1eaf0bc2087ee093c3ee64cc96ec6847752cb",
HASH_TYPE: "type",
TX_HASH:
"0xe842b43df31c92d448fa345d60a6df3e03aaab19ef88921654bf95c673a26872",
"0xff234bf2fb0ad2ab5b356ceda317d3dee3efb2c55b9427ef55d9dcbf6eecbf9f",
INDEX: "0x0",
DEP_TYPE: "code",
},
Expand Down Expand Up @@ -62,13 +59,11 @@ function buildCkbChainConfig(ckbChain) {
TX_HASH: presence(process.env.NEXT_PUBLIC_JOYID_TX_HASH),
},
);
const UNISAT = assign(
{ ...template.SCRIPTS.JOYID },
const OMNILOCK_CUSTOM = assign(
{ ...template.SCRIPTS.OMNILOCK_CUSTOM },
{
CODE_HASH: presence(process.env.NEXT_PUBLIC_UNISAT_CODE_HASH),
HASH_TYPE: "type",
TX_HASH: presence(process.env.NEXT_PUBLIC_AUTH_TX_HASH),
DEP_TYPE: "depGroup",
CODE_HASH: presence(process.env.NEXT_PUBLIC_OMNILOCK_CODE_HASH),
TX_HASH: presence(process.env.NEXT_PUBLIC_OMNILOCK_TX_HASH),
},
);

Expand All @@ -84,8 +79,7 @@ function buildCkbChainConfig(ckbChain) {
EXPLORER_URL: null,
SCRIPTS: {
JOYID,
UNISAT,
AUTH: template.SCRIPTS.AUTH,
OMNILOCK_CUSTOM,
DAO: {
...template.SCRIPTS.DAO,
TX_HASH: tx0,
Expand Down
19 changes: 8 additions & 11 deletions src/lib/lumos-adapter/init-lumos-common-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ export function buildJoyidLockInfo(ckbChainConfig) {
return buildLockInfo(ckbChainConfig, ckbChainConfig.SCRIPTS.JOYID, []);
}

export function buildUniSatLockInfo(ckbChainConfig) {
const UNISAT = ckbChainConfig.SCRIPTS.UNISAT;
// Require the secp256k1 data cell
const extraScripts = [ckbChainConfig.SCRIPTS.SECP256K1_BLAKE160];
// if not depGroup, add AUTH as well
if (UNISAT.DEP_TYPE === "code") {
extraScripts.push(ckbChainConfig.SCRIPTS.AUTH);
}

return buildLockInfo(ckbChainConfig, UNISAT, extraScripts);
export function buildOmniLockInfo(ckbChainConfig) {
return buildLockInfo(
ckbChainConfig,
ckbChainConfig.SCRIPTS.OMNILOCK_CUSTOM,
// Require the secp256k1 data cell
[ckbChainConfig.SCRIPTS.SECP256K1_BLAKE160],
);
}

export function buildLockInfo(ckbChainConfig, scriptInfo, extraScripts) {
Expand Down Expand Up @@ -137,7 +134,7 @@ export default function initLumosCommonScripts(ckbChainConfig) {
if (!inited) {
commonScripts.registerCustomLockScriptInfos([
buildJoyidLockInfo(ckbChainConfig),
buildUniSatLockInfo(ckbChainConfig),
buildOmniLockInfo(ckbChainConfig),
]);
inited = true;
}
Expand Down
4 changes: 0 additions & 4 deletions src/lib/lumos-adapter/pay-fee-with-building-packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import { common as commonScripts } from "@ckb-lumos/common-scripts";
import createBuildingPacketFromSkeleton from "./create-building-packet-from-skeleton";
import createSkeletonFromBuildingPacket from "./create-skeleton-from-building-packet";

function outPointEqual(a, b) {
return a.txHash === b.txHash && a.index === b.index;
}

// feePayments: [{address, fee?, feeRate?}]
export default async function payFeeWithBuildingPacket(
buildingPacket,
Expand Down
10 changes: 5 additions & 5 deletions src/lib/wallet/btc-wallet.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { bytes } from "@ckb-lumos/codec";
import * as lumosHelpers from "@ckb-lumos/helpers";
import { bech32 } from "bech32";
import * as bs58 from "bs58";
import * as lumosHelpers from "@ckb-lumos/helpers";
import { bytes } from "@ckb-lumos/codec";

import { urlSafeBase64Decode } from "@/lib/base64";
import { packOmnilockWitnessLock } from "./omni-lock";

export function isNativeSegwit(btcAddress) {
return btcAddress.startsWith("bc1q") || btcAddress.startsWith("tb1q");
Expand Down Expand Up @@ -48,6 +47,7 @@ export function btcAddressToCkbAddress(btcAddress, scriptInfo, ckbChainConfig) {
"Please choose an address type that is NOT Taproot (P2TR).",
);
}
args += "00";

const script = {
codeHash: scriptInfo.CODE_HASH,
Expand All @@ -72,5 +72,5 @@ export function didSign(btcAddress, signature) {
);
}

return bytes.hexify(signature);
return bytes.hexify(packOmnilockWitnessLock(signature));
}
1 change: 1 addition & 0 deletions src/lib/wallet/joyid.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as lumosHelpers from "@ckb-lumos/helpers";
import { urlSafeBase64Decode } from "../base64";

export const title = "Joyid";
export const lockScriptName = "Joyid Lock";

// Connects to the wallet.
export async function connect() {
Expand Down
29 changes: 29 additions & 0 deletions src/lib/wallet/omni-lock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { blockchain } from "@ckb-lumos/base";
import { createFixedBytesCodec, molecule } from "@ckb-lumos/codec";
const { table } = molecule;
const { BytesOpt } = blockchain;

const NullCodec = createFixedBytesCodec({
pack: () => new Uint8Array(0),
unpack: () => null,
byteLength: 0,
});

export const OmnilockWitnessLock = table(
{
signature: BytesOpt,

// Fields not used in PoC
omniIdentity: NullCodec,
preimage: NullCodec,
},
["signature", "omniIdentity", "preimage"],
);

export function packOmnilockWitnessLock(signature) {
return OmnilockWitnessLock.pack({
signature,
omniIdentity: null,
preimage: null,
});
}
4 changes: 4 additions & 0 deletions src/lib/wallet/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export function walletName(slug) {
return providers[slug].title;
}

export function lockScriptName(slug) {
return providers[slug].lockScriptName;
}

export async function connect(slug) {
return await providers[slug].connect();
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/wallet/unisat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { urlSafeBase64Decode } from "@/lib/base64";
import { didConnected, didSign, btcAddressToCkbAddress } from "./btc-wallet";

export const title = "UniSat";
export const lockScriptName = "Omnilock";

// Connects to the wallet.
export async function connect() {
Expand All @@ -18,7 +19,7 @@ export async function connect() {
//
// Calls this function only when wallet is connected.
export function address(btcAddress, ckbChainConfig) {
const scriptInfo = ckbChainConfig.SCRIPTS.UNISAT;
const scriptInfo = ckbChainConfig.SCRIPTS.OMNILOCK_CUSTOM;
return btcAddressToCkbAddress(btcAddress, scriptInfo, ckbChainConfig);
}

Expand Down

0 comments on commit e0c19cc

Please sign in to comment.