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: add issuer names to resolution results #172

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion scripts/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ async function main(args: ReturnType<typeof parseArgs>) {
providerUrl,
federatedAttestationsProxyContractAddress:
network.federatedAttestationsProxyContractAddress,
trustedIssuers: network.trustedIssuers,
// Pass in the account as a trusted issuer since we're likely using it
// for ad hoc testing.
trustedIssuers: { [account]: 'resolve-kit', ...network.trustedIssuers },
authSigner: {
authenticationMethod: OdisUtils.Query.AuthenticationMethod.WALLET_KEY,
contractKit: kit,
Expand Down
22 changes: 15 additions & 7 deletions src/resolve-celo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe('resolve-celo', () => {
lookupAttestations: () => {
return {
call: () => ({
accounts: ['0xf00ba7'],
countsPerIssuer: ['1', '1'],
accounts: ['0xf00ba7', '0xdeadf00d'],
}),
}
},
Expand All @@ -46,17 +47,24 @@ describe('resolve-celo', () => {
const resolver = new ResolveCelo({
providerUrl: 'http://does.not.matter',
federatedAttestationsProxyContractAddress: '0x1',
trustedIssuers: [],
trustedIssuers: {
'0x1': 'unit-test-1',
'0x2': 'unit-test-2',
},
authSigner,
account: '0x3',
serviceContext,
})

const resolutions = await resolver.resolve('+18888888888')
expect(resolutions.resolutions.length).toBe(1)
expect(resolutions.resolutions.length).toBe(2)
expect(resolutions.errors.length).toBe(0)
const resolution = resolutions.resolutions[0]
expect(resolution.address).toBe('0xf00ba7')
expect(resolutions.resolutions).toEqual(
expect.arrayContaining([
{ address: '0xf00ba7', issuerName: 'unit-test-1', kind: 'celo' },
{ address: '0xdeadf00d', issuerName: 'unit-test-2', kind: 'celo' },
]),
)
})

it('returns errors on contract call error', async () => {
Expand Down Expand Up @@ -93,7 +101,7 @@ describe('resolve-celo', () => {
const resolver = new ResolveCelo({
providerUrl: 'http://does.not.matter',
federatedAttestationsProxyContractAddress: '0x1',
trustedIssuers: [],
trustedIssuers: { '0x2': 'unit-test' },
authSigner,
account: '0x3',
serviceContext,
Expand Down Expand Up @@ -130,7 +138,7 @@ describe('resolve-celo', () => {
const resolver = new ResolveCelo({
providerUrl: 'http://does.not.matter',
federatedAttestationsProxyContractAddress: '0x1',
trustedIssuers: [],
trustedIssuers: { '0x2': 'unit-test' },
authSigner,
account: '0x3',
serviceContext,
Expand Down
74 changes: 59 additions & 15 deletions src/resolve-celo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const DEFAULT_NETWORK_TIMEOUT = 5_000

export class ResolveCelo implements NameResolver {
private federatedAttestationsContract: Contract
private trustedIssuers: Address[]
private trustedIssuers: Record<Address, string>
private authSigner: AuthSigner
private account: Address
private serviceContext: ServiceContext
Expand All @@ -26,14 +26,14 @@ export class ResolveCelo implements NameResolver {
static readonly MainnetFederatedAttestationsProxyContractAddress: Address =
'0x0aD5b1d0C25ecF6266Dd951403723B2687d6aff2'

static readonly AlfajoresDefaultTrustedIssuers: Address[] = [
'0xe3475047EF9F9231CD6fAe02B3cBc5148E8eB2c8', // Libera
]
static readonly MainnetDefaultTrustedIssuers: Address[] = [
'0x6549aF2688e07907C1b821cA44d6d65872737f05', // Kaala
'0xff7f2af3f451318aFb0819fDeE8f1d6306C0fbEe', // Node Finance
'0x388612590F8cC6577F19c9b61811475Aa432CB44', // Libera
]
static readonly AlfajoresDefaultTrustedIssuers: Record<Address, string> = {
'0xe3475047EF9F9231CD6fAe02B3cBc5148E8eB2c8': 'Libera',
}
static readonly MainnetDefaultTrustedIssuers: Record<Address, string> = {
'0x6549aF2688e07907C1b821cA44d6d65872737f05': 'Kaala',
'0xff7f2af3f451318aFb0819fDeE8f1d6306C0fbEe': 'Node Finance',
'0x388612590F8cC6577F19c9b61811475Aa432CB44': 'Libera',
}

constructor({
providerUrl,
Expand All @@ -46,7 +46,7 @@ export class ResolveCelo implements NameResolver {
}: {
providerUrl: string
federatedAttestationsProxyContractAddress: Address
trustedIssuers: Address[]
trustedIssuers: Record<string, Address>
authSigner: AuthSigner
account: Address
serviceContext: ServiceContext
Expand Down Expand Up @@ -93,15 +93,59 @@ export class ResolveCelo implements NameResolver {
).obfuscatedIdentifier
clearTimeout(timer)

const issuers = Object.keys(this.trustedIssuers)
const attestations = await this.federatedAttestationsContract.methods
.lookupAttestations(identifier, [this.account, ...this.trustedIssuers])
.lookupAttestations(identifier, issuers)
.call()

let totalCount = 0
const issuerAddresses: { issuerName: string; address: Address }[] = []

// .lookupAttestations returns an object with .accounts and .countsPerIssuer.
// .countsPerIssuer specifies for each issuer, how many addresses it resolved.
//
// E.g., .lookupAttestations(..., ['0x1', '0x2']) ->
// { accounts: ['0xf00', '0xd00'], countsPerIssuer: ['1', '1']}
// 0x1 issuer returns 0xf00 and 0x2 issuer returns 0xd00.
//
// We map issuer names to addresses that issuer resolved in the loop below.
for (
let index = 0;
index < attestations.countsPerIssuer.length;
index++
) {
const issuer = issuers[index]
const count = parseInt(attestations.countsPerIssuer[index], 10)
if (Number.isNaN(count)) {
throw new Error(
`Issuer count parses to NaN: ${attestations.countsPerIssuer[index]}`,
)
}
const addresses = attestations.accounts.slice(
totalCount,
totalCount + count,
)
const issuerName = this.trustedIssuers[issuer]
issuerAddresses.push(
...addresses.map((address: Address) => ({ issuerName, address })),
)
totalCount += count
}

return {
resolutions: attestations.accounts.map((address: Address) => ({
kind: ResolutionKind.Celo,
address,
})),
resolutions: issuerAddresses.map(
({
issuerName,
address,
}: {
issuerName: string
address: Address
}) => ({
kind: ResolutionKind.Celo,
issuerName,
address,
}),
),
errors: [],
}
} catch (error) {
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ export enum ResolutionKind {
}

export interface NameResolution {
// Method of resolution
kind: ResolutionKind
// Address of resolution
address: Address
// Name of entity that created the resolution. For example, 'Kaala' might
// create a resolution on SocialConnect.
issuerName?: string
// The resolve method might perform some normalization on the ID passed in.
// This is the result of that normalization.
name?: string
// TODO: remove?
thumbnailPath?: string
}

Expand Down