Skip to content

Commit

Permalink
feat(coinjoin): coinjoin backend identities
Browse files Browse the repository at this point in the history
(cherry picked from commit 6238877)
  • Loading branch information
marekrjpolak authored and matejkriz committed Nov 8, 2022
1 parent ff6d3f3 commit 826c3fd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
24 changes: 18 additions & 6 deletions packages/coinjoin/src/backend/CoinjoinBackendClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,28 @@ type CoinjoinBackendClientSettings = CoinjoinBackendSettings & {

export class CoinjoinBackendClient extends EventEmitter {
protected readonly wabisabiUrl;
protected readonly blockbookUrl;
protected readonly blockbookUrls;

private readonly identityWabisabi = 'Satoshi';
private readonly identitiesBlockbook = [
'Blockbook_1',
'Blockbook_2',
'Blockbook_3',
'Blockbook_4',
];

constructor(settings: CoinjoinBackendClientSettings) {
super();
this.wabisabiUrl = `${settings.wabisabiBackendUrl}api/v4/btc`;
this.blockbookUrl =
settings.blockbookUrls[Math.floor(Math.random() * settings.blockbookUrls.length)];
this.blockbookUrls = settings.blockbookUrls;
}

getIdentityForBlock(height: number | undefined) {
return this.identitiesBlockbook[(height ?? 0) & 0x3]; // Works only when identities.length === 4
}

fetchBlock(height: number, options?: RequestOptions): Promise<BlockbookBlock> {
return this.blockbook(options)
return this.blockbook({ identity: this.getIdentityForBlock(height), ...options })
.get(`block/${height}`)
.then(this.handleBlockbookResponse.bind(this));
}
Expand Down Expand Up @@ -113,11 +124,12 @@ export class CoinjoinBackendClient extends EventEmitter {
}

protected wabisabi(options?: RequestOptions) {
return this.request(this.wabisabiUrl, options);
return this.request(this.wabisabiUrl, { identity: this.identityWabisabi, ...options });
}

protected blockbook(options?: RequestOptions) {
return this.request(this.blockbookUrl, {
const url = this.blockbookUrls[Math.floor(Math.random() * this.blockbookUrls.length)];
return this.request(url, {
...options,
userAgent: '', // blockbook api requires user-agent to be sent, see ./utils/http.ts
});
Expand Down
6 changes: 5 additions & 1 deletion packages/coinjoin/src/backend/CoinjoinMempoolController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export class CoinjoinMempoolController implements MempoolController {
const txids = await this.client.fetchMempoolTxids();
const entries = await Promise.allSettled(
txids.map(async txid => {
const tx = this.mempool[txid] ?? (await this.client.fetchTransaction(txid));
const tx =
this.mempool[txid] ??
(await this.client.fetchTransaction(txid, {
identity: this.client.getIdentityForBlock(undefined),
}));
return [txid, tx] as const;
}),
)
Expand Down
4 changes: 3 additions & 1 deletion packages/coinjoin/src/backend/backendUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export const deriveAddresses = (
export const fixTx = (transactions: Transaction[], client: CoinjoinBackendClient) =>
Promise.all(
transactions.map(async tx => {
const fetched = await client.fetchTransaction(tx.txid);
const fetched = await client.fetchTransaction(tx.txid, {
identity: client.getIdentityForBlock(tx.blockHeight),
});
// tx.vsize missing in transactions from /block endpoint
tx.feeRate = fetched.vsize
? new BigNumber(tx.fee).div(fetched.vsize).decimalPlaces(2).toString()
Expand Down
5 changes: 4 additions & 1 deletion packages/coinjoin/src/types/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ export interface FilterController {

export type FilterClient = Pick<CoinjoinBackendClient, 'fetchFilters'>;

export type MempoolClient = Pick<CoinjoinBackendClient, 'fetchMempoolTxids' | 'fetchTransaction'>;
export type MempoolClient = Pick<
CoinjoinBackendClient,
'fetchMempoolTxids' | 'fetchTransaction' | 'getIdentityForBlock'
>;

export type AccountInfo = AccountInfoBase & {
utxo: Utxo[];
Expand Down
4 changes: 4 additions & 0 deletions packages/coinjoin/tests/mocks/MockMempoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export class MockMempoolClient implements MempoolClient {
this.fetched = [];
}

getIdentityForBlock() {
return 'default';
}

fetchMempoolTxids() {
return Promise.resolve(this.transactions.map(tx => tx.txid));
}
Expand Down

0 comments on commit 826c3fd

Please sign in to comment.