@ledgerhq/domain-service
Version:
Ledger service responsible for domain handling
74 lines • 2.35 kB
JavaScript
import axios from "axios";
import { log } from "@ledgerhq/logs";
import { getRegistries } from "../registries";
import { validateDomain } from "../utils";
/**
* Get an APDU to sign a domain resolution on the nano
*
* @param {string} domain
* @param {SupportedRegistries} registryName
* @param {string} challenge
* @returns {Promise<AddressResolutionResponse[]>}
*/
export const signDomainResolution = async (domain, registryName, challenge) => {
if (!validateDomain(domain)) {
throw new Error(`Domains with more than 255 caracters or with unicode are not supported on the nano. Domain: ${domain}`);
}
const registries = await getRegistries();
const registry = registries.find(r => r.name === registryName);
if (!registry)
return null;
const url = registry.signatures.forward
.replace("{name}", domain)
.replace("{challenge}", challenge);
return axios
.request({
method: "GET",
url,
})
.then(({ data }) => data.payload)
.catch(error => {
/* istanbul ignore next: don't test logs */
if (error.status !== 404) {
log("domain-service", "failed to get APDU for a domain", {
domain,
error,
});
}
return null;
});
};
/**
* Get an APDU to sign an address resolve resolution on the nano
*
* @param {string} address
* @param {SupportedRegistries} registryName
* @param {string} challenge
* @returns {Promise<AddressResolutionResponse[]>}
*/
export const signAddressResolution = async (address, registryName, challenge) => {
const registries = await getRegistries();
const registry = registries.find(r => r.name === registryName);
if (!registry)
return null;
const url = registry.signatures.reverse
.replace("{address}", address)
.replace("{challenge}", challenge);
return axios
.request({
method: "GET",
url,
})
.then(({ data }) => data.payload)
.catch(error => {
/* istanbul ignore next: don't test logs */
if (error.status !== 404) {
log("domain-service", "failed to get APDU for an address", {
address,
error,
});
}
return null;
});
};
//# sourceMappingURL=index.js.map