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

Withdrawals refactor #19

Merged
merged 59 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
af1ce17
contract
Jan 20, 2025
c1a79c6
modify contract
klouskingsley Jan 20, 2025
ccd977f
feat: add changeOutput for contracts
klouskingsley Jan 21, 2025
9aa3a8c
test: add deposit test
klouskingsley Jan 27, 2025
cfbb782
test: withdrawal test
klouskingsley Jan 28, 2025
78695a8
chore: remove some todo
klouskingsley Feb 5, 2025
3ee97e1
fix deposit testnet test
klouskingsley Feb 6, 2025
d780d36
fix testnet test
klouskingsley Feb 7, 2025
ed39ac4
feat: add rpc provider
klouskingsley Feb 7, 2025
f594ecc
fix rpc_importaddress
klouskingsley Feb 7, 2025
b2b2cf9
chore: restore .gitignore
klouskingsley Feb 7, 2025
060cdfb
test: support btc-signet
klouskingsley Feb 9, 2025
7841f7e
fix indexWallet endBlock
klouskingsley Feb 9, 2025
7428ba1
add deposit in operator
klouskingsley Feb 12, 2025
14efcca
import l1 implementation in sandbox
klouskingsley Feb 12, 2025
57966fe
fix running sandbox
klouskingsley Feb 12, 2025
c0cf4da
deposit api
klouskingsley Feb 13, 2025
b4e3d4f
split deps from l1/api.ts
klouskingsley Feb 13, 2025
c0422a0
add CONTARCT_INDEXES constant
klouskingsley Feb 14, 2025
c2e4837
add withdraw api for operator
klouskingsley Feb 14, 2025
7616a1a
fix l1 contract padAmt
klouskingsley Feb 15, 2025
a3ca95c
add api test for l1
klouskingsley Feb 15, 2025
a83640b
fixes
maciejka Feb 17, 2025
4889002
add withdrawal api test
klouskingsley Feb 17, 2025
4a1a5f0
Merge branch 'l1' into mk/l1-fixes
klouskingsley Feb 17, 2025
1376a8a
Merge pull request #7 from Bitcoin-Wildlife-Sanctuary/mk/l1-fixes
klouskingsley Feb 17, 2025
99dbcd3
add l1 transaction for withdrawal
klouskingsley Feb 17, 2025
83736f0
fix error when aggregating 4 deposits
klouskingsley Feb 18, 2025
a76700e
rename function names
klouskingsley Feb 18, 2025
9292464
Move aggregation state into operator (#8)
maciejka Feb 21, 2025
dc0013e
update deposit hashes
maciejka Feb 21, 2025
328d456
save plain state info in output
klouskingsley Feb 20, 2025
409a382
Merge pull request #11 from Bitcoin-Wildlife-Sanctuary/plain-deposit-…
klouskingsley Feb 22, 2025
ee97a03
optimation for frontend
klouskingsley Feb 22, 2025
0c207df
Merge pull request #12 from Bitcoin-Wildlife-Sanctuary/harry/prepare-…
klouskingsley Feb 22, 2025
2bd28a8
prepare frontend
klouskingsley Feb 22, 2025
ea40243
align l2 deposit hashes
maciejka Feb 23, 2025
3bbbbfc
prettier
maciejka Feb 23, 2025
018495e
l2 fmt
maciejka Feb 23, 2025
3426b41
ci
maciejka Feb 23, 2025
3e46e15
ci 2
maciejka Feb 23, 2025
9233661
ci 3
maciejka Feb 23, 2025
4975be0
wip
maciejka Feb 25, 2025
816c5e0
wip 2
maciejka Feb 26, 2025
2593ab6
l1 recipient type extended, id param added to close_batch method
maciejka Feb 26, 2025
aba5a31
update l2 withdrawal hashes
maciejka Feb 26, 2025
db44ce9
Merge remote-tracking branch 'origin/main' into mk/withdrawals-refactor
maciejka Feb 27, 2025
d4fc4d8
Merge remote-tracking branch 'origin/main' into mk/withdrawals-refactor
maciejka Feb 27, 2025
0ed3bae
fix allowance issue
maciejka Feb 27, 2025
ff99512
trying to initiate expansion, wip
maciejka Feb 27, 2025
feb47e3
fmt
maciejka Feb 27, 2025
8ceaa83
fix l2 test
maciejka Feb 27, 2025
5e5dec2
Merge remote-tracking branch 'origin/main' into mk/withdrawals-refactor
maciejka Feb 28, 2025
48706ed
l2 hashs updated
maciejka Feb 28, 2025
2751813
don't expand immediately
maciejka Feb 28, 2025
54064e3
fmt
maciejka Feb 28, 2025
4d64171
expansion from works
maciejka Mar 4, 2025
01568f5
fmt
maciejka Mar 4, 2025
23afb3a
cleanup
maciejka Mar 4, 2025
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
18,357 changes: 18,357 additions & 0 deletions docs/img/covenants.excalidraw

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/img/covenants.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added l1/src/contracts/TxProof.ts
Empty file.
36 changes: 18 additions & 18 deletions l1/src/contracts/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ export class Bridge extends SmartContract {
/**
* Finalize the L1 deposit.
* used when the aggregations are done, and the satoshis of the aggregator utxo should be merged into the bridge utxo.
*
*
* tx: bridgeInput + aggregatorInput + feeInput => stateOutput + bridgeOutput + changeOutput(optional)
*
*
* @param shPreimage - The sighash preimage.
* @param sigOperator - The operator signature.
* @param prevTx - The previous bridge transaction.
Expand Down Expand Up @@ -188,9 +188,9 @@ export class Bridge extends SmartContract {
/**
* Finalize the L2 deposit.
* used when the operator has sent the bitocin to the deposit addresses on l2 for a deposit batch. So the operator notify the bridge contract on L1 that the deposit batch is done.
*
*
* tx: bridgeInput + feeInput => stateOutput + bridgeOutput + changeOutput(optional)
*
*
* @param shPreimage - The sighash preimage.
* @param sigOperator - The operator signature.
* @param prevTx - The previous bridge transaction.
Expand Down Expand Up @@ -261,13 +261,13 @@ export class Bridge extends SmartContract {

/**
* Create a withdrawal transaction.
*
*
* Used when the users want to withdraw from L2 to L1.
* The users request their withdrawals on L2, the operator collects the withdrawals.
* Then call this function to create a withdrawalExpander utxo on L1 to start the L1 withdrawal process.
*
*
* tx: bridgeInput + feeInput => stateOutput + bridgeOutput + expanderOutput + changeOutput(optional)
*
*
* @param shPreimage - The sighash preimage.
* @param sigOperator - The operator signature.
* @param prevTx - The previous bridge transaction.
Expand Down Expand Up @@ -336,19 +336,19 @@ export class Bridge extends SmartContract {

/**
* Get the txid of the bridge transaction.
*
* deployBridgeContract tx:
*
* deployBridgeContract tx:
* feeInput => stateOutput + bridgeOutput + changeOutput(optional)
*
* finalizeL1Deposit tx:
*
* finalizeL1Deposit tx:
* bridgeInput + depositAggregatorInput + feeInput => stateOutput + bridgeOutput + changeOutput(optional)
*
* finalizeL2Deposit tx:
*
* finalizeL2Deposit tx:
* bridgeInput + feeInput => stateOutput + bridgeOutput + changeOutput(optional)
*
* createWithdrawal tx:
*
* createWithdrawal tx:
* bridgeInput + feeInput => stateOutput + bridgeOutput + expanderOutput + changeOutput(optional)
*
*
* @param tx - The bridge transaction.
* @param expanderSPK - The script pubkey of the expander contract output.
* @returns The txid of the bridge transaction.
Expand Down Expand Up @@ -392,7 +392,7 @@ export class Bridge extends SmartContract {

/**
* Get the hash of the finalizeL1Deposit transaction prevouts.
*
*
* @param prevTxId - The previous transaction id.
* @param aggregatorTxId - The aggregator transaction id.
* @param feePrevout - The fee prevout.
Expand All @@ -415,7 +415,7 @@ export class Bridge extends SmartContract {

/**
* Get the hash of the non-finalizeL1Deposit transaction prevouts.
*
*
* @param prevTxId - The previous transaction id.
* @param feePrevout - The fee prevout.
* @returns The hash of the non-finalizeL1Deposit transaction prevouts.
Expand Down
9 changes: 5 additions & 4 deletions l1/src/contracts/depositAggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export class DepositAggregator extends SmartContract {

/**
* Aggregates two aggregator utxos into one.
*
*
* tx: aggregatorInput0 + aggregatorInput1 + feeInput => stateOutput + aggregatorOutput + changeOutput(optional)
*
*
* @param shPreimage - The sighash preimage of the currently executing transaction.
* @param sigOperator - The signature of the bridge operator.
* @param level - The level of the aggregation; zero is the first level, which is the leaf level
Expand All @@ -74,7 +74,8 @@ export class DepositAggregator extends SmartContract {
* @param ancestorTx3 - The previous transaction of second input of prevTx1
* @param feePrevout - The prevout of the fee input
* @param isFirstInput - Indicates whether this method is called from the first or second input.
*
* @param depositData0 - Actual deposit data of the first deposit; used when aggregating leaves.
* @param depositData1 - Actual deposit data of the second deposit; used when aggregating leaves.
*/
@method()
public aggregate(
Expand Down Expand Up @@ -292,7 +293,7 @@ export class DepositAggregator extends SmartContract {
}

/**
* Hash the deposit data.
* Hash the deposit data.
* For depositData, we store the plain data, not hashed.
* @param depositAddress - The L2 address of the deposit.
* @param depositAmt - The amount of the deposit.
Expand Down
4 changes: 2 additions & 2 deletions l1/src/contracts/generalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ export class GeneralUtils extends SmartContractLib {
}

/**
* Get the contract output.
* Get the contract output.
* output=satoshis[8 bytes] + spkLength[1 byte] + spk[34 bytes]
*
*
* @param amt - The amount of the output.
* @param spk - The script pubkey of the output.
* @returns The contract output.
Expand Down
3 changes: 2 additions & 1 deletion l1/src/contracts/sigHashUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export type SHPreimage = {
*/
hashTapLeaf: ByteString
/**
* (1): the key version.
* (1): the key version.
* a constant value 0x00 representing the current version of public keys in the tapscript signature opcode execution.
* ref: https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki#common-signature-message-extension
*/
Expand Down Expand Up @@ -140,6 +140,7 @@ export class SigHashUtils extends SmartContractLib {
assert(e == shPreimage._e + eLastByte, 'invalid value of _e')
const s =
SigHashUtils.Gx + shPreimage._e + int2ByteString(shPreimage.eSuffix + 1n)
//assert(this.checkSig(Sig(s), SigHashUtils.Gx)) TODO (currently done outside)
return Sig(s)
}
}
2 changes: 2 additions & 0 deletions l1/src/covenants/depositAggregatorCovenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ export class DepositAggregatorCovenant extends Covenant<DepositAggregatorState>
): Promise<TracedDepositAggregator> {
const level = utxo.state.level;

console.log("level", level);

const covenant = new DepositAggregatorCovenant(
utxo.operator,
utxo.bridgeSPK,
Expand Down
95 changes: 95 additions & 0 deletions l1/src/features/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,101 @@ export async function createWithdrawalExpander(
}
}

export async function createWithdrawalExpander2(
signer: Signer,
network: SupportedNetwork,
utxoProvider: UtxoProvider,
chainProvider: ChainProvider,
bridgeUtxo: TraceableBridgeUtxo,

withdrawalMerkleRoot: Sha256,
outputWithdrawalState: WithdrawalExpanderState,

feeRate: number
) {
const changeAddress = await signer.getAddress()

// TODO: Does this check makes any sense?
// withdrawals.forEach((withdrawal) =>
// WithdrawalMerkle.checkWithdrawalValid(withdrawal)
// )

const sumAmt = outputWithdrawalState.type === 'LEAF' ?
outputWithdrawalState.withdrawAmt :
outputWithdrawalState.leftAmt + outputWithdrawalState.rightAmt;

console.log("sumAmt > bridgeUtxo.utxo.satoshis", sumAmt, bridgeUtxo.utxo.satoshis)

if (sumAmt > bridgeUtxo.utxo.satoshis) {
throw new Error('withdrawal amt is greater than bridge utxo satoshis')
}

const tracedBridge = await BridgeCovenant.backtrace(bridgeUtxo, chainProvider)
const outputBridgeCovenant = new BridgeCovenant(
tracedBridge.covenant.operator,
tracedBridge.covenant.expanderSPK,
tracedBridge.covenant.state
)

const outputWithdrawalExpanderCovenant = new WithdrawalExpanderCovenant(
tracedBridge.covenant.operator,
outputWithdrawalState
)

const est = estimateCreateWithdrawalTxVSize(
network,
bridgeUtxo,
tracedBridge,
outputBridgeCovenant,
outputWithdrawalExpanderCovenant,
withdrawalMerkleRoot,
sumAmt,
changeAddress,
feeRate
)
const total = feeRate * est.vSize
const utxos = await utxoProvider.getUtxos(changeAddress, {
total: Number(total),
})
if (utxos.length === 0) {
throw new Error(`Insufficient satoshis input amount: no utxos found`)
}
const feeUtxo = pickLargeFeeUtxo(utxos)
if (feeUtxo.satoshis < total) {
throw new Error(
`Insufficient satoshis input amount: fee utxo(${feeUtxo.satoshis}) < total(${total})`
)
}

const createWithdrawalTx = buildCreateWithdrawalTx(
network,
feeUtxo,
bridgeUtxo,
tracedBridge,
outputBridgeCovenant,
outputWithdrawalExpanderCovenant,
withdrawalMerkleRoot,
sumAmt,
changeAddress,
feeRate
)

const signedPsbt = await signer.signPsbt(createWithdrawalTx.toHex(), createWithdrawalTx.psbtOptions())
const txPsbt = createWithdrawalTx.combine(ExtPsbt.fromHex(signedPsbt))
await txPsbt.finalizeAllInputsAsync()
const tx = txPsbt.extractTransaction()
await chainProvider.broadcast(tx.toHex())
markSpent(utxoProvider, tx)
return {
psbt: txPsbt,
txid: tx.getId(),
bridgeState: outputBridgeCovenant.state,
withdrawalState: outputWithdrawalExpanderCovenant.state,
bridgeUtxo: outputToUtxo(tx, CONTRACT_INDEXES.outputIndex.bridge) as UTXO,
withdrawalUtxo: outputToUtxo(tx, CONTRACT_INDEXES.outputIndex.withdrawalExpander.inBridgeTx) as UTXO,
}
}

function estimateCreateWithdrawalTxVSize(
network: SupportedNetwork,
bridgeUtxo: TraceableBridgeUtxo,
Expand Down
Loading
Loading