-
Notifications
You must be signed in to change notification settings - Fork 93
/
cns.ts
47 lines (44 loc) · 2.13 KB
/
cns.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @dev Functions for interacting with the Unstoppable Domains Crypto Name Service (CNS)
*/
import { Point } from '@noble/secp256k1';
import { default as Resolution, NamingServiceName } from '@unstoppabledomains/resolution';
import type { EthersProvider } from '../types';
import { CNS_RESOLVER_ABI } from './constants';
import { Contract } from '../ethers';
export const cnsKeyPathSpending = 'crypto.ETH.umbra.spending_public_key';
export const cnsKeyPathViewing = 'crypto.ETH.umbra.viewing_public_key';
/**
* @notice Computes CNS namehash of the input CNS domain, normalized to CNS compatibility
* @param name CNS domain, e.g. myname.crypto
* @param resolution Resolution instance of @unstoppabledomains/resolution
*/
export function namehash(name: string, resolution: Resolution) {
if (typeof name !== 'string') {
throw new Error('Name must be a string');
}
return resolution.namehash(name, NamingServiceName.UNS);
}
/**
* @notice For a given CNS domain, returns the public keys, or throws if they don't exist
* @param name CNS domain, e.g. myname.crypto
* @param provider ethers provider to use
* @param resolution Resolution instance of @unstoppabledomains/resolution
*/
export async function getPublicKeys(name: string, provider: EthersProvider, resolution: Resolution) {
// Read compressed public keys
const domainNamehash = resolution.namehash(name, NamingServiceName.UNS);
const resolverAddress = await resolution.resolver(name);
const cnsResolver = new Contract(resolverAddress, CNS_RESOLVER_ABI, provider);
const [compressedSpendingPublicKey, compressedViewingPublicKey] = await cnsResolver.getMany(
[cnsKeyPathSpending, cnsKeyPathViewing],
domainNamehash
);
if (!compressedSpendingPublicKey || !compressedViewingPublicKey) {
throw new Error(`Public keys not found for ${name}. User must setup their Umbra account`);
}
// Return uncompressed public keys
const spendingPublicKey = `0x${Point.fromHex(<string>compressedSpendingPublicKey.slice(2)).toHex()}`;
const viewingPublicKey = `0x${Point.fromHex(<string>compressedViewingPublicKey.slice(2)).toHex()}`;
return { spendingPublicKey, viewingPublicKey };
}