Skip to content

Commit

Permalink
feat: SRC47 new URI format support in web request
Browse files Browse the repository at this point in the history
  • Loading branch information
ohager committed Aug 31, 2023
1 parent 7124271 commit 94cef88
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 58 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
"@noble/secp256k1": "1.7.0",
"@popperjs/core": "2.4.4",
"@serh11p/jest-webextension-mock": "4.0.0",
"@signumjs/core": "^1.0.0-rc.65",
"@signumjs/crypto": "^1.0.0-rc.65",
"@signumjs/http": "^1.0.0-rc.65",
"@signumjs/standards": "^1.0.0-rc.65",
"@signumjs/util": "^1.0.0-rc.65",
"@signumjs/core": "^1.0.0-rc.72",
"@signumjs/crypto": "^1.0.0-rc.72",
"@signumjs/http": "^1.0.0-rc.72",
"@signumjs/standards": "^1.0.0-rc.72",
"@signumjs/util": "^1.0.0-rc.72",
"async-retry": "1.3.1",
"bignumber.js": "9.0.1",
"buffer": "5.6.0",
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Signum XT Wallet",
"version": "__VERSION__",
"omnibox": { "keyword": "signum" },
"omnibox": { "keyword": "siggi" },

"icons": {
"16": "misc/signum-icon-16.png",
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Signum XT Wallet",
"version": "__VERSION__",
"omnibox": { "keyword": "signum" },
"omnibox": { "keyword": "siggi" },

"icons": {
"16": "misc/signum-icon-16.png",
Expand Down
57 changes: 39 additions & 18 deletions src/back/src47Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import browser, { WebNavigation } from 'webextension-polyfill';

import { getCurrentNetworkHost } from './dapp';

const stripTrailingSlash = (url: string) => {
return url.endsWith('/') ? url.substring(0, url.length - 1) : url;
};
function extractSRC47URI(url: URL): string {
const parse = (str: string) => {
try {
str = str.endsWith('/') ? str.substring(0, str.length - 1) : str;
if (!str.startsWith('https://') && !str.startsWith('http://')) {
str = `https://${str}`;
}
URIResolver.parseURI(str);
return str;
} catch (e: any) {
Expand All @@ -16,19 +21,14 @@ function extractSRC47URI(url: URL): string {
}
};

if (url.origin === 'https://signum' || url.origin === 'http://signum') {
const alias = url.username;
const tld = url.password || 'signum';
return parse(`signum://${alias}:${tld}`);
}

const uri = parse(url.origin);
let uri = parse(url.href);
if (!uri) {
// check search string
const search = url.searchParams;
for (let [, value] of search) {
console.log('resolver', value);
if (value.endsWith('@signum')) {
return parse(`signum://${value.replace('@signum', '')}`);
uri = parse(value);
if (uri) {
break;
}
}
}
Expand All @@ -38,8 +38,12 @@ function extractSRC47URI(url: URL): string {
// Service Workers can only use fetch api.... SignumJS legder uses Axios and does not work here
const SWHackyLedger = (nodeHost: string) => ({
alias: {
getAliasByName: async (aliasName: string) => {
const response = await fetch(`${nodeHost}/api?requestType=getAlias&aliasName=${aliasName}`);
getAliasByName: async (aliasName: string, tld?: string) => {
let url = `${nodeHost}/api?requestType=getAlias&aliasName=${aliasName}`;
if (tld) {
url += `&tld=${tld}`;
}
const response = await fetch(url);
const result = await response.json();
if (result.errorCode) {
// @ts-ignore
Expand All @@ -50,24 +54,41 @@ const SWHackyLedger = (nodeHost: string) => ({
}
});

let lastResolved = '';
async function handleBeforeNavigate(details: WebNavigation.OnBeforeNavigateDetailsType) {
// Handles SRC47 Links: https://github.com/signum-network/SIPs/blob/master/SIP/sip-47.md
// url in format: https://<subdomain>.<domain>@<namespace>
// url in format: https://<subdomain>.<domain>.<namespace>
if (details.frameId > 0) return;
try {
// url in format: https://<alias>:<namespace>@signum
const uri = extractSRC47URI(new URL(details.url));
if (!uri) return;
const link = stripTrailingSlash(details.url);
console.debug('[Signum SRC47 Resolver] - incoming link: ', link);
if (link === lastResolved) {
console.debug('[Signum SRC47 Resolver] - already resolved - skipping', lastResolved);
return;
}
lastResolved = '';
const uri = extractSRC47URI(new URL(link));

if (!uri) {
console.debug('[Signum SRC47 Resolver] - Not a Signum URI', link);
return;
}
console.debug('[Signum SRC47 Resolver] - found Signum URI', uri);
const { rpcBaseURL: nodeHost } = await getCurrentNetworkHost();
// @ts-ignore
const resolver = new URIResolver(SWHackyLedger(nodeHost));
console.debug('[Signum SRC47 Resolver] - resolving', uri, 'using', nodeHost);
const resolved = await resolver.resolve(uri);
if (typeof resolved !== 'string') return;
new URL(resolved); // throws on invalid URL
const url = sanitizeUrl(resolved);
console.debug('SRC47 URI found and resolved to:', url);
console.debug('[Signum SRC47 Resolver] - resolved to', url);
lastResolved = url;
await browser.tabs.update({ url });
} catch (e: any) {
// no op
console.debug(e.message);
console.debug('[Signum SRC47 Resolver] - error', e.message);
}
}

Expand Down
66 changes: 33 additions & 33 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1508,28 +1508,28 @@
resolved "https://registry.yarnpkg.com/@serh11p/jest-webextension-mock/-/jest-webextension-mock-4.0.0.tgz#c3b2a00e8c758e156a4a922718b35183e9023af9"
integrity sha512-SQDFFOJGwQpaPopzYkFgzdDdi8tL8US/XM2UjBu4rmLLztTI/wOSecwXj/i3lhOUwOs3bHO2Zz9eRu2rPy5p9A==

"@signumjs/contracts@^1.0.0-rc.65":
version "1.0.0-rc.65"
resolved "https://registry.yarnpkg.com/@signumjs/contracts/-/contracts-1.0.0-rc.65.tgz#b43c751c2c26bbb38c3211f8f0c020b7fa421d16"
integrity sha512-5vG/mo+Cuu/D9mtlwGsUv+KWyJK2kpND1Qwm16aiUBm5WU5ze8CF6I5hyLxKbaAPlrhI4wZp8kdrEhFIuRI0zg==
dependencies:
"@signumjs/util" "^1.0.0-rc.65"

"@signumjs/core@^1.0.0-rc.65":
version "1.0.0-rc.65"
resolved "https://registry.yarnpkg.com/@signumjs/core/-/core-1.0.0-rc.65.tgz#3f1a6e1a6901b43a3fc83e01abc0d43e00e76b00"
integrity sha512-Uj4o+2c/+7YJhZ0TYo1VF7iBa0ugQCDNtFokndNQEdj0KqFYXDkl6ge2RS/mM2KmPs0wlmQtpwp6qjnEQgzYGA==
dependencies:
"@signumjs/contracts" "^1.0.0-rc.65"
"@signumjs/crypto" "^1.0.0-rc.65"
"@signumjs/http" "^1.0.0-rc.65"
"@signumjs/util" "^1.0.0-rc.65"
"@signumjs/contracts@^1.0.0-rc.72":
version "1.0.0-rc.72"
resolved "https://registry.yarnpkg.com/@signumjs/contracts/-/contracts-1.0.0-rc.72.tgz#d18a05076d98b608b9c14b095989ec8758446f43"
integrity sha512-/mUvgfeT0URxoS4cTuUu0EAFWRCjp2TUNtE8XCVgrP5QNjsZHGrFaF7DI+vk1aYnrXOAHXG4M03DXjAEKHsRLw==
dependencies:
"@signumjs/util" "^1.0.0-rc.72"

"@signumjs/core@^1.0.0-rc.72":
version "1.0.0-rc.72"
resolved "https://registry.yarnpkg.com/@signumjs/core/-/core-1.0.0-rc.72.tgz#d9e5f91518e15003be11dd492527ad738cd94635"
integrity sha512-cUtxjfjE7T2N0Tow5Fdbi7qDNDdE/+17lOosE95c6GZpjUkBRxULwsqr9ZtYzwJle50ZJodQXwR+aBpieeV9OQ==
dependencies:
"@signumjs/contracts" "^1.0.0-rc.72"
"@signumjs/crypto" "^1.0.0-rc.72"
"@signumjs/http" "^1.0.0-rc.72"
"@signumjs/util" "^1.0.0-rc.72"
bignumber.js "^9.0.0"

"@signumjs/crypto@^1.0.0-rc.65":
version "1.0.0-rc.65"
resolved "https://registry.yarnpkg.com/@signumjs/crypto/-/crypto-1.0.0-rc.65.tgz#22045b168d43279b3dde24733a3873386405e57e"
integrity sha512-4n00pFpEB6Lf7IZGvsAGebPQjNGHprFAWqrb7jfJ8qd/AEprWDywgA0Gs7sUovyFRFvZJv7g9t8kHJQcp+tuUQ==
"@signumjs/crypto@^1.0.0-rc.72":
version "1.0.0-rc.72"
resolved "https://registry.yarnpkg.com/@signumjs/crypto/-/crypto-1.0.0-rc.72.tgz#1e20f9b19dcaac983000b4269132dbbecdd13d61"
integrity sha512-GarEa6e1R2awmfUN0Ynqj55MqMFbnR6A2Up0IOk0kayLl9siliS7nn2jh21YokuWIl3wlcDVMnPxPhCDHm2rzQ==
dependencies:
"@types/crypto-js" "^3.1.43"
"@types/node" "^12.0.0"
Expand All @@ -1539,25 +1539,25 @@
pako "^1.0.10"
seedrandom "^3.0.5"

"@signumjs/http@^1.0.0-rc.65":
version "1.0.0-rc.65"
resolved "https://registry.yarnpkg.com/@signumjs/http/-/http-1.0.0-rc.65.tgz#cb7f2afbcb098e941449f405c35495d23e4d0d4e"
integrity sha512-LyRxKG8ez+T23OvicHy4T3pnVX6j6pBTRq4dGJVunEc5IsqkBSvY4CGBJqPRXoRP8i7QwX2KWjacmVdDC3yfhw==
"@signumjs/http@^1.0.0-rc.72":
version "1.0.0-rc.72"
resolved "https://registry.yarnpkg.com/@signumjs/http/-/http-1.0.0-rc.72.tgz#140d2c700f0aa0af56c0ef586614743ac1b7a7b8"
integrity sha512-3mNZUhpfBP/De+LOcFSvl5OJ6YT1HNAo/NqGvAVDbfteHt0ip5WKVqe3wR43+1BwG0pjY6PaczVyGy47vKxENQ==
dependencies:
axios "^0.21.4"

"@signumjs/standards@^1.0.0-rc.65":
version "1.0.0-rc.65"
resolved "https://registry.yarnpkg.com/@signumjs/standards/-/standards-1.0.0-rc.65.tgz#3ca60f0835620ef50d0026dc0ab036b5614ef6b4"
integrity sha512-NG+dQeR3yqb6xy3xmU6ndGXwe8OlhFzYKOe8eMlv+X0VTV3PehOWbGwzw8Lxyk+yNDO5AWDLBNVaXbjPnFN75Q==
"@signumjs/standards@^1.0.0-rc.72":
version "1.0.0-rc.72"
resolved "https://registry.yarnpkg.com/@signumjs/standards/-/standards-1.0.0-rc.72.tgz#315ea13f2009588e69b6286e818cbae0f4706053"
integrity sha512-QAEc8eer5NAsG1YfY2x3hmErkKbZxgXQbx4j0a8Cbfoz8/c8BMC5KvasF8YVfY3k/M+X3nt4IhweoWg0R1+SYQ==
dependencies:
"@braintree/sanitize-url" "^6.0.1"
"@signumjs/core" "^1.0.0-rc.65"
"@signumjs/core" "^1.0.0-rc.72"

"@signumjs/util@^1.0.0-rc.65":
version "1.0.0-rc.65"
resolved "https://registry.yarnpkg.com/@signumjs/util/-/util-1.0.0-rc.65.tgz#2b0e85bd5516cb673cdc1f503998c1da609d9463"
integrity sha512-+aD+u9QAnshML/kgQHwoxH4DHSknpwVYDK1wqa/4m7WGFhwtXI3MZWVYSNw7nEaM5X6aG9FgcaH/Lt30qHYvIQ==
"@signumjs/util@^1.0.0-rc.72":
version "1.0.0-rc.72"
resolved "https://registry.yarnpkg.com/@signumjs/util/-/util-1.0.0-rc.72.tgz#5b8f2fa36385b7b8fd85e8042a56e61f4a8aee6c"
integrity sha512-UEVFXjQ5XlGJ87uQyp776wFgpktqEQUPBNAMihbSOkhszwW5LG0uFy5wirfQ8b+GDgGSuQvWc9VHWbfgO18jKw==
dependencies:
bignumber.js "^9.0.0"
js-base64 "^3.6.0"
Expand Down

0 comments on commit 94cef88

Please sign in to comment.