Skip to content

Commit

Permalink
Merge branch 'main' into signer-recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-zimnoch authored Nov 6, 2023
2 parents 48bcba8 + 1d21c23 commit 703ad0c
Show file tree
Hide file tree
Showing 11 changed files with 456 additions and 235 deletions.
2 changes: 1 addition & 1 deletion typescript/api-reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ Electrum script hash as a hex string.

#### Defined in

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

___

Expand Down
35 changes: 31 additions & 4 deletions typescript/api-reference/classes/ElectrumClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Electrum-based implementation of the Bitcoin client.
- [getTransactionConfirmations](ElectrumClient.md#gettransactionconfirmations)
- [getTransactionHistory](ElectrumClient.md#gettransactionhistory)
- [getTransactionMerkle](ElectrumClient.md#gettransactionmerkle)
- [getTxHashesForPublicKeyHash](ElectrumClient.md#gettxhashesforpublickeyhash)
- [latestBlockHeight](ElectrumClient.md#latestblockheight)
- [withBackoffRetrier](ElectrumClient.md#withbackoffretrier)
- [withElectrum](ElectrumClient.md#withelectrum)
Expand Down Expand Up @@ -136,7 +137,7 @@ ___

#### Defined in

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

___

Expand Down Expand Up @@ -189,7 +190,7 @@ ___

#### Defined in

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

___

Expand Down Expand Up @@ -341,7 +342,33 @@ ___

#### Defined in

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

___

### getTxHashesForPublicKeyHash

**getTxHashesForPublicKeyHash**(`publicKeyHash`): `Promise`\<[`BitcoinTxHash`](BitcoinTxHash.md)[]\>

#### Parameters

| Name | Type |
| :------ | :------ |
| `publicKeyHash` | [`Hex`](Hex.md) |

#### Returns

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

**`See`**

#### Implementation of

[BitcoinClient](../interfaces/BitcoinClient.md).[getTxHashesForPublicKeyHash](../interfaces/BitcoinClient.md#gettxhashesforpublickeyhash)

#### Defined in

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

___

Expand All @@ -361,7 +388,7 @@ ___

#### Defined in

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

___

Expand Down
9 changes: 2 additions & 7 deletions typescript/api-reference/classes/RedemptionsService.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ Determines the plain-text wallet main UTXO currently registered in the
Bridge on-chain contract. The returned main UTXO can be undefined if the
wallet does not have a main UTXO registered in the Bridge at the moment.

WARNING: THIS FUNCTION CANNOT DETERMINE THE MAIN UTXO IF IT COMES FROM A
BITCOIN TRANSACTION THAT IS NOT ONE OF THE LATEST FIVE TRANSACTIONS
TARGETING THE GIVEN WALLET PUBLIC KEY HASH. HOWEVER, SUCH A CASE IS
VERY UNLIKELY.

#### Parameters

| Name | Type | Description |
Expand All @@ -95,7 +90,7 @@ Promise holding the wallet main UTXO or undefined value.

#### Defined in

[services/redemptions/redemptions-service.ts:221](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/redemptions/redemptions-service.ts#L221)
[services/redemptions/redemptions-service.ts:215](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/redemptions/redemptions-service.ts#L215)

___

Expand Down Expand Up @@ -152,7 +147,7 @@ Throws an error if no redemption request exists for the given

#### Defined in

[services/redemptions/redemptions-service.ts:337](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/redemptions/redemptions-service.ts#L337)
[services/redemptions/redemptions-service.ts:327](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/services/redemptions/redemptions-service.ts#L327)

___

Expand Down
39 changes: 35 additions & 4 deletions typescript/api-reference/interfaces/BitcoinClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Represents a Bitcoin client.
- [getTransactionConfirmations](BitcoinClient.md#gettransactionconfirmations)
- [getTransactionHistory](BitcoinClient.md#gettransactionhistory)
- [getTransactionMerkle](BitcoinClient.md#gettransactionmerkle)
- [getTxHashesForPublicKeyHash](BitcoinClient.md#gettxhashesforpublickeyhash)
- [latestBlockHeight](BitcoinClient.md#latestblockheight)

## Methods
Expand All @@ -41,7 +42,7 @@ Broadcasts the given transaction over the network.

#### Defined in

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

___

Expand Down Expand Up @@ -90,7 +91,7 @@ Concatenation of block headers in a hexadecimal format.

#### Defined in

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

___

Expand Down Expand Up @@ -232,7 +233,37 @@ Merkle branch.

#### Defined in

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

___

### getTxHashesForPublicKeyHash

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

Gets hashes of confirmed transactions that pay the given public key hash
using either a P2PKH or P2WPKH script. The returned transactions hashes are
ordered by block height in the ascending order, i.e. the latest transaction
hash is at the end of the list. The returned list does not contain
unconfirmed transactions hashes living in the mempool at the moment of
request.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `publicKeyHash` | [`Hex`](../classes/Hex.md) | Hash of the public key for which to find corresponding transaction hashes. |

#### Returns

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

Array of confirmed transaction hashes related to the provided
public key hash.

#### Defined in

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

___

Expand All @@ -250,4 +281,4 @@ Height of the last mined block.

#### Defined in

[lib/bitcoin/client.ts:61](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/client.ts#L61)
[lib/bitcoin/client.ts:75](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/client.ts#L75)
14 changes: 14 additions & 0 deletions typescript/src/lib/bitcoin/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ export interface BitcoinClient {
*/
getTransactionConfirmations(transactionHash: BitcoinTxHash): Promise<number>

/**
* Gets hashes of confirmed transactions that pay the given public key hash
* using either a P2PKH or P2WPKH script. The returned transactions hashes are
* ordered by block height in the ascending order, i.e. the latest transaction
* hash is at the end of the list. The returned list does not contain
* unconfirmed transactions hashes living in the mempool at the moment of
* request.
* @param publicKeyHash - Hash of the public key for which to find
* corresponding transaction hashes.
* @returns Array of confirmed transaction hashes related to the provided
* public key hash.
*/
getTxHashesForPublicKeyHash(publicKeyHash: Hex): Promise<BitcoinTxHash[]>

/**
* Gets height of the latest mined block.
* @return Height of the last mined block.
Expand Down
58 changes: 58 additions & 0 deletions typescript/src/lib/electrum/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,64 @@ export class ElectrumClient implements BitcoinClient {
})
}

// eslint-disable-next-line valid-jsdoc
/**
* @see {BitcoinClient#getTxHashesForPublicKeyHash}
*/
getTxHashesForPublicKeyHash(publicKeyHash: Hex): Promise<BitcoinTxHash[]> {
return this.withElectrum<BitcoinTxHash[]>(async (electrum: Electrum) => {
const bitcoinNetwork = await this.getNetwork()

// eslint-disable-next-line camelcase
type HistoryItem = { height: number; tx_hash: string }

const getConfirmedHistory = async (
witnessAddress: boolean
): Promise<HistoryItem[]> => {
const address = BitcoinAddressConverter.publicKeyHashToAddress(
publicKeyHash,
witnessAddress,
bitcoinNetwork
)

const script = BitcoinAddressConverter.addressToOutputScript(
address,
bitcoinNetwork
)

let historyItems: HistoryItem[] = await this.withBackoffRetrier<
HistoryItem[]
>()(async () => {
return await electrum.blockchain_scripthash_getHistory(
computeElectrumScriptHash(script)
)
})

// According to https://electrumx.readthedocs.io/en/latest/protocol-methods.html#blockchain-scripthash-get-history
// unconfirmed items living in the mempool are appended at the end of the
// returned list and their height value is either -1 or 0. That means
// we need to take all items with height >0 to obtain a confirmed txs
// history.
historyItems = historyItems.filter((item) => item.height > 0)

// The list returned from blockchain.scripthash.get_history is sorted by
// the block height in the ascending order though we are sorting it
// again just in case (e.g. API contract changes).
historyItems = historyItems.sort((a, b) => a.height - b.height)

return historyItems
}

const p2pkhItems = await getConfirmedHistory(false)
const p2wpkhItems = await getConfirmedHistory(true)

const items = [...p2pkhItems, ...p2wpkhItems]
items.sort((a, b) => a.height - b.height)

return items.map((item) => BitcoinTxHash.from(item.tx_hash))
})
}

// eslint-disable-next-line valid-jsdoc
/**
* @see {BitcoinClient#latestBlockHeight}
Expand Down
Loading

0 comments on commit 703ad0c

Please sign in to comment.