Page MenuHomePhabricator

D14191.id41173.diff
No OneTemporary

D14191.id41173.diff

diff --git a/cashtab/src/components/Common/Ticker.js b/cashtab/src/components/Common/Ticker.js
--- a/cashtab/src/components/Common/Ticker.js
+++ b/cashtab/src/components/Common/Ticker.js
@@ -23,7 +23,7 @@
eightByte: 551,
minFee: 550, // dust
},
- aliasServerBaseUrl: 'https://aliasdev.etokens.cash',
+ aliasServerBaseUrl: 'https://alias.etokens.cash',
aliasMaxLength: 21, // max byte length, refer to the Alias spec at https://reviews.bitcoinabc.org/D12972
reservedAliases: [
'avalanche',
diff --git a/cashtab/src/utils/__mocks__/mockAliasServerResponses.js b/cashtab/src/utils/__mocks__/mockAliasServerResponses.js
new file mode 100644
--- /dev/null
+++ b/cashtab/src/utils/__mocks__/mockAliasServerResponses.js
@@ -0,0 +1,7 @@
+export const mockAliasApiRespponse = {
+ alias: 'twelvechar12',
+ address: 'ecash:qpmytrdsakt0axrrlswvaj069nat3p9s7cjctmjasj',
+ txid: '166b21d4631e2a6ec6110061f351c9c3bfb3a8d4e6919684df7e2824b42b0ffe',
+ blockheight: 792419,
+ isRegistered: true,
+};
diff --git a/cashtab/src/utils/__tests__/aliasUtils.test.js b/cashtab/src/utils/__tests__/aliasUtils.test.js
new file mode 100644
--- /dev/null
+++ b/cashtab/src/utils/__tests__/aliasUtils.test.js
@@ -0,0 +1,62 @@
+// Copyright (c) 2023 The Bitcoin developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+'use strict';
+import { currency } from 'components/Common/Ticker';
+import { getAliasDetails } from 'utils/aliasUtils';
+import { mockAliasApiResponse } from '../__mocks__/mockAliasServerResponses';
+import { when } from 'jest-when';
+
+test('getAliasDetails() returns an alias object for a registered alias', async () => {
+ const alias = 'twelvechar12';
+ const fetchUrl = `${currency.aliasSettings.aliasServerBaseUrl}/alias/${alias}`;
+
+ // mock the fetch call to alias-server's '/alias' endpoint
+ global.fetch = jest.fn();
+ when(fetch).calledWith(fetchUrl).mockResolvedValue(mockAliasApiResponse);
+
+ expect(await getAliasDetails(alias)).toEqual(mockAliasApiResponse);
+});
+
+test('getAliasDetails() returns an api error for an invalid alias', async () => {
+ const alias = '@@@@@@@@@@@@';
+ const fetchUrl = `${currency.aliasSettings.aliasServerBaseUrl}/alias/${alias}`;
+ const expectedError = {
+ error: `Error fetching /alias/${alias}: alias param cannot contain non-alphanumeric characters`,
+ };
+
+ // mock the fetch call to alias-server's '/alias' endpoint
+ global.fetch = jest.fn();
+ when(fetch).calledWith(fetchUrl).mockResolvedValue(expectedError);
+
+ expect(await getAliasDetails(alias)).toEqual(expectedError);
+});
+
+test('getAliasDetails() returns an object with isRegistered as false for an unregistered alias', async () => {
+ const alias = 'foobar';
+ const fetchUrl = `${currency.aliasSettings.aliasServerBaseUrl}/alias/${alias}`;
+ const expectedResult = {
+ alias: alias,
+ isRegistered: false,
+ };
+
+ // mock the fetch call to alias-server's '/alias' endpoint
+ global.fetch = jest.fn();
+ when(fetch).calledWith(fetchUrl).mockResolvedValue(expectedResult);
+
+ expect(await getAliasDetails(alias)).toEqual(expectedResult);
+});
+
+test('getAliasDetails() returns an api error for an alias longer than 21 characters', async () => {
+ const alias = 'foobarrrrrrrrrrrrrrrrrrrrrrrrrrr';
+ const fetchUrl = `${currency.aliasSettings.aliasServerBaseUrl}/alias/${alias}`;
+ const expectedError = {
+ error: `Error fetching /alias/${alias}: alias param must be between 1 and 21 characters in length`,
+ };
+
+ // mock the fetch call to alias-server's '/alias' endpoint
+ global.fetch = jest.fn();
+ when(fetch).calledWith(fetchUrl).mockResolvedValue(expectedError);
+
+ expect(await getAliasDetails(alias)).toEqual(expectedError);
+});
diff --git a/cashtab/src/utils/aliasUtils.js b/cashtab/src/utils/aliasUtils.js
--- a/cashtab/src/utils/aliasUtils.js
+++ b/cashtab/src/utils/aliasUtils.js
@@ -27,6 +27,36 @@
return aliasServerRespJson;
};
+/**
+ * Fetches details for an alias via the alias-server
+ *
+ * @param {string} alias the alias being queried
+ * @returns {object} aliasServerResp an alias object
+ * @throws {error} err server fetch errors from alias-server
+ * @response:
+ * {
+ * alias: 'twelvechar12',
+ * address:'ecash:qpmytrdsakt0axrrlswvaj069nat3p9s7cjctmjasj',
+ * txid:'166b21d4631e2a6ec6110061f351c9c3bfb3a8d4e6919684df7e2824b42b0ffe',
+ * blockheight:792419,
+ * isRegistered:true
+ * }
+ */
+export const getAliasDetails = async alias => {
+ try {
+ const aliasServerResp = await fetch(
+ currency.aliasSettings.aliasServerBaseUrl + '/alias/' + alias,
+ );
+ return aliasServerResp;
+ } catch (err) {
+ console.log(
+ `getAliasDetails(): Error retrieving alias details from alias-server`,
+ err,
+ );
+ throw err;
+ }
+};
+
/*
@response:
[

File Metadata

Mime Type
text/plain
Expires
Tue, May 20, 22:40 (4 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5866081
Default Alt Text
D14191.id41173.diff (5 KB)

Event Timeline