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

feat(SPV-1016): Added webhook functions #1079

Merged
merged 7 commits into from
Sep 13, 2024
32 changes: 32 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Txs,
Utxo,
Utxos,
Webhook,
XprivWithSigning,
XPub,
XPubs,
Expand Down Expand Up @@ -444,6 +445,37 @@ export class SpvWalletClient {
return await this.http.adminRequest(`admin/transactions/record`, 'POST', { hex });
}

/**
* Admin only: Subscribe to a webhook with the given URL, token header, and token value.
*
* @param url - The URL to subscribe the webhook to.
* @param tokenHeader - The header name for the authentication token.
* @param tokenValue - The value of the authentication token.
* @returns A Promise that resolves when the webhook subscription is complete.
*/
async AdminSubscribeWebhook(url: string, tokenHeader: string, tokenValue: string): Promise<void> {
return await this.http.adminRequest(`admin/webhooks/subscriptions`, 'POST', { url, tokenHeader, tokenValue });
}

/**
* Admin only: Get a list of all webhook subscriptions.
*
* @returns A Promise that resolves to an array of Webhook objects representing the current webhook subscriptions.
*/
async AdminGetWebhooks(): Promise<Webhook[]> {
return await this.http.adminRequest(`admin/webhooks/subscriptions`, 'GET');
}

/**
* Admin only: Delete a webhook subscription by the given URL.
*
* @param url - The URL of the webhook subscription to delete.
* @returns A Promise that resolves when the webhook subscription is deleted.
*/
async AdminDeleteWebhook(url: string): Promise<void> {
return await this.http.adminRequest(`admin/webhooks/subscriptions`, 'DELETE', { url });
}
chris-4chain marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get information about the xpub from the server of the current user
*
Expand Down
53 changes: 28 additions & 25 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,34 @@ describe('SPVWalletClient admin routing', () => {
const adminSPVWalletClient = new SpvWalletClient(testClient.serverURL, options, { level: 'error' });

it.each`
spvWalletMethod | httpMethod | path | act
${'AdminNewXpub'} | ${'post'} | ${'admin/xpub'} | ${() => adminSPVWalletClient.AdminNewXpub('', {})}
${'AdminGetStatus'} | ${'get'} | ${'admin/status'} | ${() => adminSPVWalletClient.AdminGetStatus()}
${'AdminGetStats'} | ${'get'} | ${'admin/stats'} | ${() => adminSPVWalletClient.AdminGetStats()}
${'AdminGetAccessKeys'} | ${'post'} | ${'admin/access-keys/search'} | ${() => adminSPVWalletClient.AdminGetAccessKeys({}, {}, {})}
${'AdminGetAccessKeysCount'} | ${'post'} | ${'admin/access-keys/count'} | ${() => adminSPVWalletClient.AdminGetAccessKeysCount({}, {})}
${'AdminGetContacts'} | ${'post'} | ${'admin/contact/search'} | ${() => adminSPVWalletClient.AdminGetContacts({}, {}, {})}
${'AdminUpdateContact'} | ${'patch'} | ${'admin/contact/1'} | ${() => adminSPVWalletClient.AdminUpdateContact('1', '', {})}
${'AdminDeleteContact'} | ${'delete'} | ${'admin/contact/1'} | ${() => adminSPVWalletClient.AdminDeleteContact('1')}
${'AdminAcceptContact'} | ${'patch'} | ${'admin/contact/accepted/1'} | ${() => adminSPVWalletClient.AdminAcceptContact('1')}
${'AdminRejectContact'} | ${'patch'} | ${'admin/contact/rejected/1'} | ${() => adminSPVWalletClient.AdminRejectContact('1')}
${'AdminGetDestinations'} | ${'post'} | ${'admin/destinations/search'} | ${() => adminSPVWalletClient.AdminGetDestinations({}, {}, {})}
${'AdminGetDestinationsCount'} | ${'post'} | ${'admin/destinations/count'} | ${() => adminSPVWalletClient.AdminGetDestinationsCount({}, {})}
${'AdminGetPaymail'} | ${'post'} | ${'admin/paymail/get'} | ${() => adminSPVWalletClient.AdminGetPaymail('')}
${'AdminGetPaymails'} | ${'post'} | ${'admin/paymails/search'} | ${() => adminSPVWalletClient.AdminGetPaymails({}, {}, {})}
${'AdminGetPaymailsCount'} | ${'post'} | ${'admin/paymails/count'} | ${() => adminSPVWalletClient.AdminGetPaymailsCount({}, {})}
${'AdminCreatePaymail'} | ${'post'} | ${'admin/paymail/create'} | ${() => adminSPVWalletClient.AdminCreatePaymail('', '', '', '')}
${'AdminDeletePaymail'} | ${'delete'} | ${'admin/paymail/delete'} | ${() => adminSPVWalletClient.AdminDeletePaymail('')}
${'AdminGetTransactions'} | ${'post'} | ${'admin/transactions/search'} | ${() => adminSPVWalletClient.AdminGetTransactions({}, {}, {})}
${'AdminGetTransactionsCount'} | ${'post'} | ${'admin/transactions/count'} | ${() => adminSPVWalletClient.AdminGetTransactionsCount({}, {})}
${'AdminGetUtxos'} | ${'post'} | ${'admin/utxos/search'} | ${() => adminSPVWalletClient.AdminGetUtxos({}, {}, {})}
${'AdminGetUtxosCount'} | ${'post'} | ${'admin/utxos/count'} | ${() => adminSPVWalletClient.AdminGetUtxosCount({}, {})}
${'AdminGetXPubs'} | ${'post'} | ${'admin/xpubs/search'} | ${() => adminSPVWalletClient.AdminGetXPubs({}, {}, {})}
${'AdminGetXPubsCount'} | ${'post'} | ${'admin/xpubs/count'} | ${() => adminSPVWalletClient.AdminGetXPubsCount({}, {})}
${'AdminRecordTransaction'} | ${'post'} | ${'admin/transactions/record'} | ${() => adminSPVWalletClient.AdminRecordTransaction('')}
spvWalletMethod | httpMethod | path | act
${'AdminNewXpub'} | ${'post'} | ${'admin/xpub'} | ${() => adminSPVWalletClient.AdminNewXpub('', {})}
${'AdminGetStatus'} | ${'get'} | ${'admin/status'} | ${() => adminSPVWalletClient.AdminGetStatus()}
${'AdminGetStats'} | ${'get'} | ${'admin/stats'} | ${() => adminSPVWalletClient.AdminGetStats()}
${'AdminGetAccessKeys'} | ${'post'} | ${'admin/access-keys/search'} | ${() => adminSPVWalletClient.AdminGetAccessKeys({}, {}, {})}
${'AdminGetAccessKeysCount'} | ${'post'} | ${'admin/access-keys/count'} | ${() => adminSPVWalletClient.AdminGetAccessKeysCount({}, {})}
${'AdminGetContacts'} | ${'post'} | ${'admin/contact/search'} | ${() => adminSPVWalletClient.AdminGetContacts({}, {}, {})}
${'AdminUpdateContact'} | ${'patch'} | ${'admin/contact/1'} | ${() => adminSPVWalletClient.AdminUpdateContact('1', '', {})}
${'AdminDeleteContact'} | ${'delete'} | ${'admin/contact/1'} | ${() => adminSPVWalletClient.AdminDeleteContact('1')}
${'AdminAcceptContact'} | ${'patch'} | ${'admin/contact/accepted/1'} | ${() => adminSPVWalletClient.AdminAcceptContact('1')}
${'AdminRejectContact'} | ${'patch'} | ${'admin/contact/rejected/1'} | ${() => adminSPVWalletClient.AdminRejectContact('1')}
${'AdminGetDestinations'} | ${'post'} | ${'admin/destinations/search'} | ${() => adminSPVWalletClient.AdminGetDestinations({}, {}, {})}
${'AdminGetDestinationsCount'} | ${'post'} | ${'admin/destinations/count'} | ${() => adminSPVWalletClient.AdminGetDestinationsCount({}, {})}
${'AdminGetPaymail'} | ${'post'} | ${'admin/paymail/get'} | ${() => adminSPVWalletClient.AdminGetPaymail('')}
${'AdminGetPaymails'} | ${'post'} | ${'admin/paymails/search'} | ${() => adminSPVWalletClient.AdminGetPaymails({}, {}, {})}
${'AdminGetPaymailsCount'} | ${'post'} | ${'admin/paymails/count'} | ${() => adminSPVWalletClient.AdminGetPaymailsCount({}, {})}
${'AdminCreatePaymail'} | ${'post'} | ${'admin/paymail/create'} | ${() => adminSPVWalletClient.AdminCreatePaymail('', '', '', '')}
${'AdminDeletePaymail'} | ${'delete'} | ${'admin/paymail/delete'} | ${() => adminSPVWalletClient.AdminDeletePaymail('')}
${'AdminGetTransactions'} | ${'post'} | ${'admin/transactions/search'} | ${() => adminSPVWalletClient.AdminGetTransactions({}, {}, {})}
${'AdminGetTransactionsCount'} | ${'post'} | ${'admin/transactions/count'} | ${() => adminSPVWalletClient.AdminGetTransactionsCount({}, {})}
${'AdminGetUtxos'} | ${'post'} | ${'admin/utxos/search'} | ${() => adminSPVWalletClient.AdminGetUtxos({}, {}, {})}
${'AdminGetUtxosCount'} | ${'post'} | ${'admin/utxos/count'} | ${() => adminSPVWalletClient.AdminGetUtxosCount({}, {})}
${'AdminGetXPubs'} | ${'post'} | ${'admin/xpubs/search'} | ${() => adminSPVWalletClient.AdminGetXPubs({}, {}, {})}
${'AdminGetXPubsCount'} | ${'post'} | ${'admin/xpubs/count'} | ${() => adminSPVWalletClient.AdminGetXPubsCount({}, {})}
${'AdminRecordTransaction'} | ${'post'} | ${'admin/transactions/record'} | ${() => adminSPVWalletClient.AdminRecordTransaction('')}
${'AdminSubscribeWebhook'} | ${'post'} | ${'admin/webhooks/subscriptions'} | ${() => adminSPVWalletClient.AdminSubscribeWebhook('', '', '')}
${'AdminGetWebhooks'} | ${'get'} | ${'admin/webhooks/subscriptions'} | ${() => adminSPVWalletClient.AdminGetWebhooks()}
${'AdminDeleteWebhook'} | ${'delete'} | ${'admin/webhooks/subscriptions'} | ${() => adminSPVWalletClient.AdminDeleteWebhook('')}
`('$spvWalletMethod', async ({ path, httpMethod, act }) => {
// given
setupHttpMock(httpMethod, path);
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,17 @@ export interface AccessKeyWithSigning extends OptionalAdminKey {
accessKey: string;
}

export interface Webhook {
/**
* The URL for the webhook.
*/
url: string;
/**
* Indicates whether the entity is banned or not.
*/
banned: boolean;
}

export type ClientOptions = XpubWithoutSigning | XprivWithSigning | AccessKeyWithSigning | AdminKey;

/**
Expand Down
Loading