Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rgbpp-ckb): build btc transfer virtual ckb tx #13

Merged
merged 22 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:

- name: Install dependencies
run: pnpm i

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test workflow doesn't seem to have any significant changes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding an empty line will be more clear and I don't think it's a bad thing. Do you think so ?

If you think it's necessary to remove the empty line, I will do it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with you. I think breaking steps with an empty line is clearer than collapsing them together. However, I noticed that the only change in the test.yaml file in the PR is the addition of more spaces on line 45, rather than adding more line breaks as you suggested.

- name: Run tests for packages
run: pnpm run test:packages
env:
Expand Down
6 changes: 4 additions & 2 deletions packages/ckb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
"lib"
],
"dependencies": {
"@ckb-lumos/base": "0.21.1",
"@ckb-lumos/codec": "0.0.0-canary-3f37c5b-20240228111419",
"@nervosnetwork/ckb-sdk-core": "^0.109.0",
"@nervosnetwork/ckb-sdk-utils": "^0.109.0",
"@nervosnetwork/ckb-types": "^0.109.0",
"camelcase-keys": "^7.0.2",
"axios": "^1.6.7",
"bignumber.js": "^9.1.1",
"axios": "^1.6.7"
"camelcase-keys": "^7.0.2",
"js-sha256": "^0.11.0"
},
"devDependencies": {
"@ckb-lumos/molecule": "0.0.0-canary-3f37c5b-20240228111419",
Expand Down
35 changes: 33 additions & 2 deletions packages/ckb/src/collector/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import axios from 'axios';
import CKB from '@nervosnetwork/ckb-sdk-core';
import { toCamelcase } from '../utils/case-parser';
import { CollectResult, IndexerCell } from '../types/collector';
import { CollectResult, CollectUdtResult, IndexerCell } from '../types/collector';
import { MIN_CAPACITY } from '../constants';
import { CapacityNotEnoughError, IndexerError } from '../error';
import { CapacityNotEnoughError, IndexerError, UdtAmountNotEnoughError } from '../error';
import { leToU128 } from '../utils';

const parseScript = (script: CKBComponents.Script) => ({
code_hash: script.codeHash,
Expand Down Expand Up @@ -111,4 +112,34 @@ export class Collector {
}
return { inputs, capacity: sum };
}

collectUdtInputs(liveCells: IndexerCell[], needAmount: bigint): CollectUdtResult {
let inputs: CKBComponents.CellInput[] = [];
let sumCapacity = BigInt(0);
let sumAmount = BigInt(0);
for (let cell of liveCells) {
inputs.push({
previousOutput: {
txHash: cell.outPoint.txHash,
index: cell.outPoint.index,
},
since: '0x0',
});
sumCapacity = sumCapacity + BigInt(cell.output.capacity);
sumAmount += leToU128(cell.outputData);
if (sumAmount >= needAmount) {
break;
}
}
if (sumAmount < needAmount) {
throw new UdtAmountNotEnoughError('Insufficient UDT balance');
}
return { inputs, capacity: sumCapacity, amount: sumAmount };
Copy link
Contributor

@Flouse Flouse Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated ec07a01

}

async getLiveCell(outPoint: CKBComponents.OutPoint): Promise<CKBComponents.LiveCell> {
const ckb = new CKB(this.ckbNodeUrl);
const { cell } = await ckb.rpc.getLiveCell(outPoint, true);
return cell;
}
}
61 changes: 60 additions & 1 deletion packages/ckb/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const CKB_UNIT = BigInt(10000_0000);
export const MAX_FEE = BigInt(2000_0000);
export const MIN_CAPACITY = BigInt(63) * BigInt(10000_0000);
export const MIN_CAPACITY = BigInt(61) * BigInt(10000_0000);
export const SECP256K1_WITNESS_LOCK_LEN = 65;

const TestnetInfo = {
Secp256k1LockDep: {
Expand All @@ -10,6 +11,31 @@ const TestnetInfo = {
},
depType: 'depGroup',
} as CKBComponents.CellDep,

RgbppLockScript: {
codeHash: '0xd23761b364210735c19c60561d213fb3beae2fd6172743719eff6920e020baac',
hashType: 'type',
args: '',
} as CKBComponents.Script,

RgbppLockDep: {
outPoint: { txHash: '0x437d4343c1eb5901c74ba34f6e9b1a1a25d72b441659d73bb1b40e9924bda6fb', index: '0x0' },
depType: 'depGroup',
} as CKBComponents.CellDep,

XUDTTypeScript: {
codeHash: '0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb',
hashType: 'type',
args: '',
} as CKBComponents.Script,

XUDTTypeDep: {
outPoint: {
txHash: '0xbf6fb538763efec2a70a6a3dcb7242787087e1030c4e7d86585bc63a9d337f5f',
index: '0x0',
},
depType: 'code',
} as CKBComponents.CellDep,
};

const MainnetInfo = {
Expand All @@ -20,7 +46,40 @@ const MainnetInfo = {
},
depType: 'depGroup',
} as CKBComponents.CellDep,

RgbppLockScript: {
codeHash: '0xd23761b364210735c19c60561d213fb3beae2fd6172743719eff6920e020baac',
hashType: 'type',
args: '',
} as CKBComponents.Script,

RgbppLockDep: {
outPoint: { txHash: '0x437d4343c1eb5901c74ba34f6e9b1a1a25d72b441659d73bb1b40e9924bda6fb', index: '0x0' },
depType: 'depGroup',
} as CKBComponents.CellDep,

XUDTTypeScript: {
codeHash: '0x50bd8d6680b8b9cf98b73f3c08faf8b2a21914311954118ad6609be6e78a1b95',
hashType: 'data1',
args: '',
} as CKBComponents.Script,

XUDTTypeDep: {
outPoint: {
txHash: '0xc07844ce21b38e4b071dd0e1ee3b0e27afd8d7532491327f39b786343f558ab7',
index: '0x0',
},
depType: 'code',
} as CKBComponents.CellDep,
};

export const getSecp256k1CellDep = (isMainnet = false) =>
isMainnet ? MainnetInfo.Secp256k1LockDep : TestnetInfo.Secp256k1LockDep;

export const getXudtTypeScript = (isMainnet = false) =>
isMainnet ? MainnetInfo.XUDTTypeScript : TestnetInfo.XUDTTypeScript;
export const getXudtDep = (isMainnet = false) => (isMainnet ? MainnetInfo.XUDTTypeDep : TestnetInfo.XUDTTypeDep);

export const getRgbppLockScript = (isMainnet = false) =>
isMainnet ? MainnetInfo.RgbppLockScript : TestnetInfo.RgbppLockScript;
export const getRgbppLockDep = (isMainnet = false) => (isMainnet ? MainnetInfo.RgbppLockDep : TestnetInfo.RgbppLockDep);
18 changes: 18 additions & 0 deletions packages/ckb/src/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ export class NoLiveCellError extends Error {
super(message);
}
}

export class NoRgbppLiveCellError extends Error {
constructor(message: string) {
super(message);
}
}

export class UdtAmountNotEnoughError extends Error {
constructor(message: string) {
super(message);
}
}

export class InputsCapacityNotEnoughError extends Error {
constructor(message: string) {
super(message);
}
}
1 change: 1 addition & 0 deletions packages/ckb/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './collector';
export * from './error';
export * from './paymaster';
export * from './types';
export * from './rgbpp';
7 changes: 3 additions & 4 deletions packages/ckb/src/paymaster/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { AddressPrefix, addressToScript, getTransactionSize, privateKeyToAddress } from '@nervosnetwork/ckb-sdk-utils';
import { ConstructParams } from '../types/transfer';
import { ConstructPaymasterParams } from '../types/rgbpp';
import { NoLiveCellError } from '../error';
import { CKB_UNIT, MAX_FEE, getSecp256k1CellDep } from '../constants';
import { CKB_UNIT, MAX_FEE, SECP256K1_WITNESS_LOCK_LEN, getSecp256k1CellDep } from '../constants';
import { append0x, calculateTransactionFee } from '../utils';

const SECP256K1_MIN_CAPACITY = BigInt(61) * CKB_UNIT;
const SECP256K1_WITNESS_LOCK_LEN = 65;

export const splitMultiCellsWithSecp256k1 = async ({
masterPrivateKey,
collector,
receiverAddress,
capacityWithCKB,
cellAmount,
}: ConstructParams) => {
}: ConstructPaymasterParams) => {
const isMainnet = receiverAddress.startsWith('ckb');
const masterAddress = privateKeyToAddress(masterPrivateKey, {
prefix: isMainnet ? AddressPrefix.Mainnet : AddressPrefix.Testnet,
Expand Down
87 changes: 87 additions & 0 deletions packages/ckb/src/rgbpp/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {
getTransactionSize,
rawTransactionToHash,
scriptToHash,
serializeWitnessArgs,
} from '@nervosnetwork/ckb-sdk-utils';
import { AppendPaymasterCellAndSignTxParams, AppendWitnessesParams, SendTxParams } from '../types';
import { SECP256K1_WITNESS_LOCK_LEN, getRgbppLockScript } from '../constants';
import { append0x, calculateTransactionFee } from '../utils';
import { InputsCapacityNotEnoughError } from '../error';
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses';

//TODO: waiting for SPV proof
export const appendWitnesses = ({ ckbRawTx, sumInputsCapacity, needPaymasterCell }: AppendWitnessesParams) => {
if (!needPaymasterCell) {
let rawTx = ckbRawTx;
const partialOutputsCapacity = rawTx.outputs
.slice(0, rawTx.outputs.length - 1)
.map((output) => BigInt(output.capacity))
.reduce((prev, current) => prev + current, BigInt(0));

if (sumInputsCapacity <= partialOutputsCapacity) {
throw new InputsCapacityNotEnoughError('The sum of inputs capacity is not enough');
}

const txSize = getTransactionSize(rawTx) + SECP256K1_WITNESS_LOCK_LEN;
const estimatedTxFee = calculateTransactionFee(txSize);

const changeCapacity = sumInputsCapacity - partialOutputsCapacity - estimatedTxFee;
rawTx.outputs[rawTx.outputs.length - 1].capacity = append0x(changeCapacity.toString(16));
return rawTx;
}
};

export const appendPaymasterCellAndSignTx = async ({
secp256k1PrivateKey,
ckbRawTx,
sumInputsCapacity,
paymasterCell,
}: AppendPaymasterCellAndSignTxParams) => {
let rawTx = ckbRawTx;
const paymasterInput = { previousOutput: paymasterCell.outPoint, since: '0x0' };
rawTx.inputs = [paymasterInput, ...rawTx.inputs];
const inputsCapacity = sumInputsCapacity + BigInt(paymasterCell.output.capacity);

const sumOutputsCapacity = rawTx.outputs
.map((output) => BigInt(output.capacity))
.reduce((prev, current) => prev + current, BigInt(0));

const txSize = getTransactionSize(rawTx) + SECP256K1_WITNESS_LOCK_LEN;
const estimatedTxFee = calculateTransactionFee(txSize);

if (inputsCapacity <= sumOutputsCapacity) {
throw new InputsCapacityNotEnoughError('The sum of inputs capacity is not enough');
}
const changeCapacity = inputsCapacity - sumOutputsCapacity - estimatedTxFee;
rawTx.outputs[rawTx.outputs.length - 1].capacity = append0x(changeCapacity.toString(16));

let keyMap = new Map<string, string>();
keyMap.set(scriptToHash(paymasterCell.output.lock), secp256k1PrivateKey);
// The 0x00 is placeholder for rpbpp cell and it has no effect on transaction signatures
keyMap.set('0x00', '');

const cells = ckbRawTx.inputs.map((input, index) => ({
outPoint: input.previousOutput,
lock: index === 0 ? paymasterCell.output.lock : getRgbppLockScript(false),
}));

const transactionHash = rawTransactionToHash(rawTx);
const signedWitnesses = signWitnesses(keyMap)({
transactionHash,
witnesses: rawTx.witnesses,
inputCells: cells,
skipMissingKeys: true,
});
const emptyWitness = { lock: '', inputType: '', outputType: '' };
const signedTx = {
...rawTx,
witnesses: signedWitnesses.map((witness, index) => (index === 0 ? serializeWitnessArgs(emptyWitness) : witness)),
};
return signedTx;
};

export const sendCkbTx = async ({ collector, signedTx }: SendTxParams) => {
const txHash = await collector.getCkb().rpc.sendTransaction(signedTx, 'passthrough');
return txHash;
};
2 changes: 2 additions & 0 deletions packages/ckb/src/rgbpp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './l1-transfer';
export * from './builder';
83 changes: 83 additions & 0 deletions packages/ckb/src/rgbpp/l1-transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { RgbppL1TransferVirtualParams, RgbppL1TransferVirtualResult, RgbppL1TransferVirtualTx } from '../types/rgbpp';
import { blockchain } from '@ckb-lumos/base';
import { NoRgbppLiveCellError } from '../error';
import { append0x, calculateRgbppCellCapacity, u128ToLe, u32ToLe } from '../utils';
import { calculateCommitment, genRgbppLockScript } from '../utils/rgbpp';
import { IndexerCell } from '../types';
import { getRgbppLockDep, getSecp256k1CellDep, getXudtDep } from '../constants';

export const genTransferL1VirtualTx = async ({
collector,
xudtTypeBytes,
rgbppLockArgsList,
transferAmount,
isMainnet,
}: RgbppL1TransferVirtualParams): Promise<RgbppL1TransferVirtualResult> => {
const xudtType = blockchain.Script.unpack(xudtTypeBytes) as CKBComponents.Script;

const rgbppLocks = rgbppLockArgsList.map((args) => genRgbppLockScript(args, isMainnet));
let rgbppCells: IndexerCell[] = [];
for await (const rgbppLock of rgbppLocks) {
const cells = await collector.getCells({ lock: rgbppLock, type: xudtType });
if (!cells || cells.length === 0) {
throw new NoRgbppLiveCellError('No rgb++ cells found with the xudt type script and the rgbpp lock args');
}
rgbppCells = [...rgbppCells, ...cells];
}

const {
inputs,
capacity: sumInputsCapacity,
amount: sumAmount,
} = collector.collectUdtInputs(rgbppCells, transferAmount);

const rpbppCellCapacity = calculateRgbppCellCapacity(xudtType);
const outputsData = [append0x(u128ToLe(transferAmount))];
const outputs: CKBComponents.CellOutput[] = [
{
lock: genRgbppLockScript(u32ToLe(1)),
type: xudtType,
capacity: append0x(rpbppCellCapacity.toString(16)),
},
];

if (sumAmount > transferAmount) {
outputs.push({
lock: genRgbppLockScript(u32ToLe(2)),
type: xudtType,
capacity: append0x(rpbppCellCapacity.toString(16)),
});
outputsData.push(append0x(u128ToLe(sumAmount - transferAmount)));
}

const cellDeps = [getRgbppLockDep(isMainnet), getXudtDep(isMainnet)];
const needPaymasterCell = inputs.length < outputs.length;
if (needPaymasterCell) {
cellDeps.push(getSecp256k1CellDep(isMainnet));
}
const witnesses = inputs.map((_) => '0x');

const ckbRawTx: CKBComponents.RawTransaction = {
version: '0x0',
cellDeps,
headerDeps: [],
inputs,
outputs,
outputsData,
witnesses,
};

const virtualTx: RgbppL1TransferVirtualTx = {
inputs,
outputs,
outputsData,
};
const commitment = calculateCommitment(virtualTx);

return {
ckbRawTx,
commitment,
needPaymasterCell,
sumInputsCapacity,
};
};
2 changes: 1 addition & 1 deletion packages/ckb/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './common';
export * from './collector';
export * from './transfer';
export * from './rgbpp';
Loading
Loading