Skip to content

Commit

Permalink
Merge pull request #26 from friedger/feat/compact-only
Browse files Browse the repository at this point in the history
feat: remove non-compact version
  • Loading branch information
MarvinJanssen authored Dec 3, 2023
2 parents 79c765b + d1b6c95 commit 8f49307
Show file tree
Hide file tree
Showing 10 changed files with 437 additions and 205 deletions.
5 changes: 0 additions & 5 deletions Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ path = 'contracts/tests/clarity-bitcoin-helper_test.clar'
clarity_version = 2
epoch = 2.1

[contracts.send-to-first-input]
path = 'contracts/examples/send-to-first-input.clar'
clarity_version = 2
epoch = 2.1

[contracts.send-to-first-input-compact]
path = 'contracts/examples/send-to-first-input-compact.clar'
clarity_version = 2
Expand Down
13 changes: 2 additions & 11 deletions contracts/clarity-bitcoin.clar
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
;; @contract stateless contract to verify bitcoin transaction
;; @version 5

;; version 5 adds support for txid generation and improves security

;; Error codes
(define-constant ERR-OUT-OF-BOUNDS u1)
(define-constant ERR-TOO-MANY-TXINS u2)
Expand Down Expand Up @@ -490,17 +492,6 @@
(let ((block (unwrap! (parse-block-header header) (err ERR-BAD-HEADER))))
(was-tx-mined-internal height tx header (get merkle-root block) proof)))

;; Determine whether or not a Bitcoin transaction was mined in a prior Bitcoin block.
;; with the given header object and merkle proof.
;; Returns (ok txid) if tx was mined else
;; returns (err u1) if the header is invalid or
;; returns (err u2) if the proof is invalid.
(define-read-only (was-tx-mined (height uint) (tx (buff 4096))
(header { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4) })
(proof { tx-index: uint, hashes: (list 14 (buff 32)), tree-depth: uint}))
(was-tx-mined-internal height tx (contract-call? .clarity-bitcoin-helper concat-header header) (reverse-buff32 (get merkle-root header)) proof))


;; Private function to verify block header and merkle proof.
;; This function must only be called with the merkle root of the provided header.
;; Use was-tx-mined-compact with header as a buffer or
Expand Down
31 changes: 0 additions & 31 deletions contracts/examples/send-to-first-input.clar

This file was deleted.

3 changes: 0 additions & 3 deletions contracts/helper.clar
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
(define-public (verify-mp (reverse-tx-id (buff 32)) (merkle-root (buff 32)) (proof { tx-index: uint, hashes: (list 14 (buff 32)), tree-depth: uint}))
(contract-call? .clarity-bitcoin verify-merkle-proof reverse-tx-id merkle-root proof))

(define-public (was-tx-mined (height uint) (tx (buff 1024)) (header { version: (buff 4), parent: (buff 32), merkle-root: (buff 32), timestamp: (buff 4), nbits: (buff 4), nonce: (buff 4) }) (proof { tx-index: uint, hashes: (list 14 (buff 32)), tree-depth: uint}))
(contract-call? .clarity-bitcoin was-tx-mined height tx header proof))

(define-public (was-tx-mined-compact (height uint) (tx (buff 1024)) (header (buff 80)) (proof { tx-index: uint, hashes: (list 14 (buff 32)), tree-depth: uint}))
(contract-call? .clarity-bitcoin was-tx-mined-compact height tx header proof))

Expand Down
13 changes: 13 additions & 0 deletions contracts/tests/clarity-bitcoin-helper_test.clar
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@
(asserts! (is-eq result 0x1122334401112233445566778899aabbccddeeff00112233445566778899aabbccddeeff000011223380112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff001122334401001122334455667780112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff0044332211) (err result))
(ok true)))

;; @name concat header
;; block id: 000000000000000606f86a5bc8fb6e38b16050fb4676dea26cba5222583c4d86
(define-public (test-concat-header)
(let ((result (contract-call? .clarity-bitcoin-helper concat-header
{merkle-root: 0x9160ba7ae5f29f9632dc0cd89f466ee64e2dddfde737a40808ddc147cd82406f,
version: 0x0000a020,
nbits: 0x88a12719,
nonce: 0x9842cec7,
timestamp: 0x18b84864,
parent: 0x65bc9201b5b5a1d695a18e4d5efe5d52d8ccc4129a2499141d00000000000000
})))
(asserts! (is-eq result 0x0000a02065bc9201b5b5a1d695a18e4d5efe5d52d8ccc4129a2499141d000000000000009160ba7ae5f29f9632dc0cd89f466ee64e2dddfde737a40808ddc147cd82406f18b8486488a127199842cec7) (err result))
(ok true)))
40 changes: 38 additions & 2 deletions contracts/tests/clarity-bitcoin_segwit_test.clar
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,45 @@
)
)

;; @name OP_RETURN is too large. Fails to parse segwit transaction

;; @name verify segwit transaction where the incorrect wproof with zeros only
;; arbitrary segwit transaction
(define-public (test-was-wtx-mined-internal-5)
(let (
(burnchain-block-height u2431567)
;; txid: 2fd0308a0bca2f4ea40fe93a19be976a40b2d5e0df08df1dd991b4df31a563fc
(raw-tx 0x020000000001017d406bb6466e0da97778f55ece77ab6becc415c930dbe618e81e8dae53ba914400000000171600143e8d4581104393d916566886ae01e26be8f8975afeffffff0276410300000000001600143d5f37543d2916547fe9b1242322b22f6e46d6929c9291d8000000001600145b3298860caeb3302f09e58b7cc3cf58d0a6740402463043021f53943d5951726d73a952473b49aa44dde64446f106749806d6478b0bdc053102200863a4f25914f3032afa8549f329962e8ecc7c575a1aefc51102a566ff5ece70012103742fe8a7244ab8384b3d533eb19138e2758c2b7a2e351c9ec2b8912cf4e046c24e1a2500)
;; block id: 00000000096ae97d41a543592c3680477444acdc86c877aeb4832744691cb94b
(raw-block-header 0x000000208af1a70ab2ed062c7ac53eb56b053498db50f0d9c41f0dc8a5efcb1b000000007b64b9e16eb97b1fb32977aa00e2cb7418856b1e794e232be4f3b4b0512cb31256845064ffff001dc3cdbab0)
(witness-merkle-root 0xb2ea7fb39beae6b5a225c85cb7087561909e1d17bba74de3592a3c1da3944983)
(witness-reserved-data 0x0000000000000000000000000000000000000000000000000000000000000000)
;; txid: 84f8a86015b0a95763bb16128ff301a60f668356548405178953ab1e4cbff36e
(raw-coinbase-tx 0x01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff32034f1a2500045684506404c862b40c0c11c14a6400000000000000000a636b706f6f6c0e2f6d696e65642062792072736b2fffffffff0317ea2b00000000001976a914ec2f9ffaba0d68ea6bd7c25cedfe2ae710938e6088ac0000000000000000266a24aa21a9ede2ee16ef0a8c0fd6ccb8c78f297199f0f143629121801c46b1e1487c0123cb4b00000000000000002a6a52534b424c4f434b3acb9b4841f625100fb87055003991835f3183bff7668865e93e1f1118003a492d00000000)
(parsed-block-header (contract-call? .clarity-bitcoin parse-block-header raw-block-header))
(parsed-tx (contract-call? .clarity-bitcoin parse-wtx raw-tx false))
)

(let ((result (contract-call? .clarity-bitcoin was-segwit-tx-mined-compact
burnchain-block-height
raw-tx
raw-block-header
u1
u7
(list 0x0000000000000000000000000000000000000000000000000000000000000000 0x0000000000000000000000000000000000000000000000000000000000000000 0x0000000000000000000000000000000000000000000000000000000000000000 0x0000000000000000000000000000000000000000000000000000000000000000 0x0000000000000000000000000000000000000000000000000000000000000000 0x0000000000000000000000000000000000000000000000000000000000000000 0x0000000000000000000000000000000000000000000000000000000000000000)
witness-merkle-root
witness-reserved-data
raw-coinbase-tx
(list 0xfc63a531dfb491d91ddf08dfe0d5b2406a97be193ae90fa44e2fca0b8a30d02f 0x33274cc92f8b980272688e01114cc2944fb661d1aa3a658c7d29675a46a4d5ad 0x1172bf0943aad7bc580aaab5f5d356b1c172f11c297ccf0077515309906352f2 0x2af4fd00ac79b65c0c508fbf44c22d1cf5acb084770079a94bd72ac816cfceb8 0xed4ca325a1800f0dfb2ab9d63761ecb358c014424e684c820d0d87ace45474a1 0xd3292e0e550420e500f29663dfc8ef632dbcb119c8a1ddf49aa3d32ecad83084 0x6369b65eea600edbd69b56386be9269f9662ca3f384a0ca21922ac03d2936102)
)))
(asserts! (is-eq result (err ERR-WITNESS-TX-NOT-IN-COMMITMENT)) (err result))
(ok true)
)
)
)

;; @name OP_RETURN is too large. Fails to parse segwit transaction
;; arbitrary segwit transaction
(define-public (test-was-wtx-mined-internal-6)
(let (
(burnchain-block-height u2430921)
;; txid: c770364da721e34eeb1a67f09c986fa5e4f13f9819df727e604691f42f5340a1
Expand Down Expand Up @@ -215,7 +251,7 @@

;; @name verify segwit transaction where OP_RETURN is in output[1]
;; arbitrary segwit transaction
(define-public (test-was-wtx-mined-internal-6)
(define-public (test-was-wtx-mined-internal-7)
(let (
(burnchain-block-height u2431087)
;; txid: 3b3a7a31c949048fabf759e670a55ffd5b9472a12e748b684db5d264b6852084
Expand Down
90 changes: 24 additions & 66 deletions contracts/tests/clarity-bitcoin_test.clar
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,30 @@
)
)

;; @name verify transaction with wrong block height
;; arbitrary segwit transaction
(define-public (test-was-tx-mined-internal-8)
(let (
(burnchain-block-height u1)
(txid 0x3b3a7a31c949048fabf759e670a55ffd5b9472a12e748b684db5d264b6852084)
(raw-tx 0x020000000218f905443202116524547142bd55b69335dfc4e4c66ff3afaaaab6267b557c4b030000000000000000e0dbdf1039321ab7a2626ca5458e766c6107690b1a1923e075c4f691cc4928ac0000000000000000000220a10700000000002200208730dbfaa29c49f00312812aa12a62335113909711deb8da5ecedd14688188363c5f26010000000022512036f4ff452cb82e505436e73d0a8b630041b71e037e5997290ba1fe0ae7f4d8d56d182500)
;; block id: 000000000000000606f86a5bc8fb6e38b16050fb4676dea26cba5222583c4d86
(raw-block-header 0x0000a02065bc9201b5b5a1d695a18e4d5efe5d52d8ccc4129a2499141d000000000000009160ba7ae5f29f9632dc0cd89f466ee64e2dddfde737a40808ddc147cd82406f18b8486488a127199842cec7)
(parsed-block-header (contract-call? .clarity-bitcoin parse-block-header raw-block-header))
(parsed-tx (contract-call? .clarity-bitcoin parse-tx raw-tx))
)
(let ((result (contract-call? .clarity-bitcoin was-tx-mined-compact
burnchain-block-height
raw-tx
raw-block-header
{tx-index: u3,
tree-depth: u2,
hashes: (list 0x3313f803502a6f9a89ac09ff9e8f9d8032aa7c35cc6d1679487622e944c8ccb8 0xc4e620f495d8a30d8d919fc148fe55c8873b4aefe43116bc6ef895aa51572215)}
)))
(asserts! (is-eq result (err ERR-HEADER-HEIGHT-MISMATCH)) (err "expected ERR-HEADER-HEIGHT-MISMATCH"))
(ok true))
)
)

;; @name verify segwit transaction with left over data
(define-public (test-parse-tx)
Expand Down Expand Up @@ -238,72 +262,6 @@
)
)

;; @name verify transaction with header object
(define-public (test-was-tx-mined-with-header-object-1)
(let (
(burnchain-block-height u2431087)
(txid 0x3b3a7a31c949048fabf759e670a55ffd5b9472a12e748b684db5d264b6852084)
(raw-tx 0x020000000218f905443202116524547142bd55b69335dfc4e4c66ff3afaaaab6267b557c4b030000000000000000e0dbdf1039321ab7a2626ca5458e766c6107690b1a1923e075c4f691cc4928ac0000000000000000000220a10700000000002200208730dbfaa29c49f00312812aa12a62335113909711deb8da5ecedd14688188363c5f26010000000022512036f4ff452cb82e505436e73d0a8b630041b71e037e5997290ba1fe0ae7f4d8d56d182500)
;; block id: 000000000000000606f86a5bc8fb6e38b16050fb4676dea26cba5222583c4d86
(raw-block-header 0x0000a02065bc9201b5b5a1d695a18e4d5efe5d52d8ccc4129a2499141d000000000000009160ba7ae5f29f9632dc0cd89f466ee64e2dddfde737a40808ddc147cd82406f18b8486488a127199842cec7)
(parsed-block-header (contract-call? .clarity-bitcoin parse-block-header raw-block-header))
(parsed-tx (contract-call? .clarity-bitcoin parse-tx raw-tx))
)
;; prepare

(let ((result (contract-call? .clarity-bitcoin was-tx-mined
burnchain-block-height
raw-tx
{merkle-root: 0x9160ba7ae5f29f9632dc0cd89f466ee64e2dddfde737a40808ddc147cd82406f,
version: 0x0000a020,
nbits: 0x88a12719,
nonce: 0x9842cec7,
timestamp: 0x18b84864,
parent: 0x65bc9201b5b5a1d695a18e4d5efe5d52d8ccc4129a2499141d00000000000000,
}
{tx-index: u3,
tree-depth: u2,
hashes: (list 0x3313f803502a6f9a89ac09ff9e8f9d8032aa7c35cc6d1679487622e944c8ccb8 0xc4e620f495d8a30d8d919fc148fe55c8873b4aefe43116bc6ef895aa51572215)}
)))
(asserts! (is-eq result (ok txid)) (err "expected txid"))
(ok true))
)
)


;; @name verify transaction with header object but wrong version
(define-public (test-was-tx-mined-with-header-object-2)
(let (
(burnchain-block-height u2431087)
(txid 0x3b3a7a31c949048fabf759e670a55ffd5b9472a12e748b684db5d264b6852084)
(raw-tx 0x020000000218f905443202116524547142bd55b69335dfc4e4c66ff3afaaaab6267b557c4b030000000000000000e0dbdf1039321ab7a2626ca5458e766c6107690b1a1923e075c4f691cc4928ac0000000000000000000220a10700000000002200208730dbfaa29c49f00312812aa12a62335113909711deb8da5ecedd14688188363c5f26010000000022512036f4ff452cb82e505436e73d0a8b630041b71e037e5997290ba1fe0ae7f4d8d56d182500)
;; block id: 000000000000000606f86a5bc8fb6e38b16050fb4676dea26cba5222583c4d86
(raw-block-header 0x0000a02065bc9201b5b5a1d695a18e4d5efe5d52d8ccc4129a2499141d000000000000009160ba7ae5f29f9632dc0cd89f466ee64e2dddfde737a40808ddc147cd82406f18b8486488a127199842cec7)
(parsed-block-header (contract-call? .clarity-bitcoin parse-block-header raw-block-header))
(parsed-tx (contract-call? .clarity-bitcoin parse-tx raw-tx))
)
;; prepare

(let ((result (contract-call? .clarity-bitcoin was-tx-mined
burnchain-block-height
raw-tx
{merkle-root: 0x9160ba7ae5f29f9632dc0cd89f466ee64e2dddfde737a40808ddc147cd82406f,
version: 0x00000000,
nbits: 0x88a12719,
nonce: 0x9842cec7,
timestamp: 0x18b84864,
parent: 0x65bc9201b5b5a1d695a18e4d5efe5d52d8ccc4129a2499141d00000000000000,
}
{tx-index: u3,
tree-depth: u2,
hashes: (list 0x3313f803502a6f9a89ac09ff9e8f9d8032aa7c35cc6d1679487622e944c8ccb8 0xc4e620f495d8a30d8d919fc148fe55c8873b4aefe43116bc6ef895aa51572215)}
)))
(asserts! (is-eq result (err ERR-HEADER-HEIGHT-MISMATCH)) (err "expected ERR-HEADER-HEIGHT-MISMATCH"))
(ok true))
)
)


(define-constant ERR-OUT-OF-BOUNDS u1)
(define-constant ERR-TOO-MANY-TXINS u2)
(define-constant ERR-TOO-MANY-TXOUTS u3)
Expand Down
Loading

0 comments on commit 8f49307

Please sign in to comment.