Skip to content

Commit

Permalink
SDK: Add coinbase data to the SPV proof builder (#773)
Browse files Browse the repository at this point in the history
Refs:
GHSA-wg2x-rv86-mmpx

The SPV proof verifier used by the `Bridge` contract has been changed.
It now expects the coinbase's preimage and merkle proof to be submitted
as part of the proven transaction's SPV proof. Here we adhere to this
requirement by adjusting the SDK's SPV proof builder accordingly.

Note: Core changes presented in this PR are equivalent to
keep-network/keep-core#3762
  • Loading branch information
tomaszslabon authored Jan 17, 2024
2 parents 0e6f4a5 + e8ca5b8 commit b84d56a
Show file tree
Hide file tree
Showing 19 changed files with 760 additions and 68 deletions.
6 changes: 3 additions & 3 deletions typescript/api-reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ Bitcoin transaction along with the inclusion proof.

#### Defined in

[src/lib/bitcoin/spv.ts:64](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L64)
[src/lib/bitcoin/spv.ts:75](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L75)

___

Expand Down Expand Up @@ -686,7 +686,7 @@ Electrum script hash as a hex string.

#### Defined in

[src/lib/electrum/client.ts:649](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/electrum/client.ts#L649)
[src/lib/electrum/client.ts:667](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/electrum/client.ts#L667)

___

Expand Down Expand Up @@ -957,7 +957,7 @@ The function should be used within a try-catch block.

#### Defined in

[src/lib/bitcoin/spv.ts:145](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L145)
[src/lib/bitcoin/spv.ts:180](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L180)

___

Expand Down
27 changes: 27 additions & 0 deletions typescript/api-reference/classes/ElectrumClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Electrum-based implementation of the Bitcoin client.

- [broadcast](ElectrumClient.md#broadcast)
- [findAllUnspentTransactionOutputs](ElectrumClient.md#findallunspenttransactionoutputs)
- [getCoinbaseTxHash](ElectrumClient.md#getcoinbasetxhash)
- [getHeadersChain](ElectrumClient.md#getheaderschain)
- [getNetwork](ElectrumClient.md#getnetwork)
- [getRawTransaction](ElectrumClient.md#getrawtransaction)
Expand Down Expand Up @@ -167,6 +168,32 @@ ___

___

### getCoinbaseTxHash

**getCoinbaseTxHash**(`blockHeight`): `Promise`\<[`BitcoinTxHash`](BitcoinTxHash.md)\>

#### Parameters

| Name | Type |
| :------ | :------ |
| `blockHeight` | `number` |

#### Returns

`Promise`\<[`BitcoinTxHash`](BitcoinTxHash.md)\>

**`See`**

#### Implementation of

[BitcoinClient](../interfaces/BitcoinClient.md).[getCoinbaseTxHash](../interfaces/BitcoinClient.md#getcoinbasetxhash)

#### Defined in

[src/lib/electrum/client.ts:646](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/electrum/client.ts#L646)

___

### getHeadersChain

**getHeadersChain**(`blockHeight`, `chainLength`): `Promise`\<[`Hex`](Hex.md)\>
Expand Down
28 changes: 14 additions & 14 deletions typescript/api-reference/classes/EthereumBridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ EthersContractHandle.\_totalRetryAttempts

#### Defined in

[src/lib/ethereum/bridge.ts:494](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L494)
[src/lib/ethereum/bridge.ts:498](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L498)

___

Expand Down Expand Up @@ -177,7 +177,7 @@ Builds the UTXO hash based on the UTXO components. UTXO hash is computed as

#### Defined in

[src/lib/ethereum/bridge.ts:618](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L618)
[src/lib/ethereum/bridge.ts:622](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L622)

___

Expand All @@ -204,7 +204,7 @@ ___

#### Defined in

[src/lib/ethereum/bridge.ts:429](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L429)
[src/lib/ethereum/bridge.ts:433](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L433)

___

Expand Down Expand Up @@ -334,7 +334,7 @@ Bridge.getNewWalletRegisteredEvents

#### Defined in

[src/lib/ethereum/bridge.ts:530](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L530)
[src/lib/ethereum/bridge.ts:534](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L534)

___

Expand All @@ -361,7 +361,7 @@ Bridge.getRedemptionRequestedEvents

#### Defined in

[src/lib/ethereum/bridge.ts:635](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L635)
[src/lib/ethereum/bridge.ts:639](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L639)

___

Expand All @@ -381,7 +381,7 @@ ___

#### Defined in

[src/lib/ethereum/bridge.ts:515](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L515)
[src/lib/ethereum/bridge.ts:519](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L519)

___

Expand All @@ -405,7 +405,7 @@ Parsed deposit request.

#### Defined in

[src/lib/ethereum/bridge.ts:474](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L474)
[src/lib/ethereum/bridge.ts:478](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L478)

___

Expand Down Expand Up @@ -454,7 +454,7 @@ Parsed wallet data.

#### Defined in

[src/lib/ethereum/bridge.ts:589](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L589)
[src/lib/ethereum/bridge.ts:593](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L593)

___

Expand Down Expand Up @@ -510,7 +510,7 @@ ___

#### Defined in

[src/lib/ethereum/bridge.ts:336](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L336)
[src/lib/ethereum/bridge.ts:338](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L338)

___

Expand Down Expand Up @@ -597,7 +597,7 @@ ___

#### Defined in

[src/lib/ethereum/bridge.ts:380](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L380)
[src/lib/ethereum/bridge.ts:382](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L382)

___

Expand Down Expand Up @@ -644,7 +644,7 @@ ___

#### Defined in

[src/lib/ethereum/bridge.ts:322](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L322)
[src/lib/ethereum/bridge.ts:324](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L324)

___

Expand All @@ -664,7 +664,7 @@ ___

#### Defined in

[src/lib/ethereum/bridge.ts:555](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L555)
[src/lib/ethereum/bridge.ts:559](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L559)

___

Expand All @@ -690,7 +690,7 @@ ___

#### Defined in

[src/lib/ethereum/bridge.ts:572](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L572)
[src/lib/ethereum/bridge.ts:576](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L576)

___

Expand All @@ -715,7 +715,7 @@ Deposit key.

#### Defined in

[src/lib/ethereum/bridge.ts:455](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L455)
[src/lib/ethereum/bridge.ts:459](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/bridge.ts#L459)

___

Expand Down
23 changes: 23 additions & 0 deletions typescript/api-reference/interfaces/BitcoinClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Represents a Bitcoin client.

- [broadcast](BitcoinClient.md#broadcast)
- [findAllUnspentTransactionOutputs](BitcoinClient.md#findallunspenttransactionoutputs)
- [getCoinbaseTxHash](BitcoinClient.md#getcoinbasetxhash)
- [getHeadersChain](BitcoinClient.md#getheaderschain)
- [getNetwork](BitcoinClient.md#getnetwork)
- [getRawTransaction](BitcoinClient.md#getrawtransaction)
Expand Down Expand Up @@ -72,6 +73,28 @@ List of UTXOs.

___

### getCoinbaseTxHash

**getCoinbaseTxHash**(`blockHeight`): `Promise`\<[`BitcoinTxHash`](../classes/BitcoinTxHash.md)\>

Gets the hash of the coinbase transaction for the given block height.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `blockHeight` | `number` | Height of the block. |

#### Returns

`Promise`\<[`BitcoinTxHash`](../classes/BitcoinTxHash.md)\>

#### Defined in

[src/lib/bitcoin/client.ts:109](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/client.ts#L109)

___

### getHeadersChain

**getHeadersChain**(`blockHeight`, `chainLength`): `Promise`\<[`Hex`](../classes/Hex.md)\>
Expand Down
27 changes: 27 additions & 0 deletions typescript/api-reference/interfaces/BitcoinSpvProof.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ the Bitcoin blockchain.
### Properties

- [bitcoinHeaders](BitcoinSpvProof.md#bitcoinheaders)
- [coinbasePreimage](BitcoinSpvProof.md#coinbasepreimage)
- [coinbaseProof](BitcoinSpvProof.md#coinbaseproof)
- [merkleProof](BitcoinSpvProof.md#merkleproof)
- [txIndexInBlock](BitcoinSpvProof.md#txindexinblock)

Expand All @@ -26,6 +28,31 @@ Concatenated block headers in hexadecimal format. Each block header is

___

### coinbasePreimage

**coinbasePreimage**: [`Hex`](../classes/Hex.md)

The sha256 preimage of the coinbase transaction hash i.e.,
the sha256 hash of the coinbase transaction.

#### Defined in

[src/lib/bitcoin/spv.ts:37](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L37)

___

### coinbaseProof

**coinbaseProof**: [`Hex`](../classes/Hex.md)

Merkle proof of coinbase transaction inclusion in a block.

#### Defined in

[src/lib/bitcoin/spv.ts:42](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L42)

___

### merkleProof

**merkleProof**: [`Hex`](../classes/Hex.md)
Expand Down
6 changes: 3 additions & 3 deletions typescript/api-reference/interfaces/BitcoinTxMerkleBranch.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The height of the block the transaction was confirmed in.

#### Defined in

[src/lib/bitcoin/spv.ts:41](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L41)
[src/lib/bitcoin/spv.ts:52](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L52)

___

Expand All @@ -34,7 +34,7 @@ the deepest pairing first. Each hash is an unprefixed hex string.

#### Defined in

[src/lib/bitcoin/spv.ts:48](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L48)
[src/lib/bitcoin/spv.ts:59](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L59)

___

Expand All @@ -46,4 +46,4 @@ The 0-based index of the transaction's position in the block.

#### Defined in

[src/lib/bitcoin/spv.ts:53](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L53)
[src/lib/bitcoin/spv.ts:64](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L64)
6 changes: 6 additions & 0 deletions typescript/src/lib/bitcoin/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,10 @@ export interface BitcoinClient {
* @param transaction - Transaction to broadcast.
*/
broadcast(transaction: BitcoinRawTx): Promise<void>

/**
* Gets the hash of the coinbase transaction for the given block height.
* @param blockHeight - Height of the block.
*/
getCoinbaseTxHash(blockHeight: number): Promise<BitcoinTxHash>
}
Loading

0 comments on commit b84d56a

Please sign in to comment.