Skip to content

Commit

Permalink
remove brightid sponsorship logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Apr 24, 2024
1 parent 7d31cd8 commit 45fe06d
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 406 deletions.
8 changes: 1 addition & 7 deletions contracts/deploy-config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
"OptimisticRecipientRegistry": {
"challengePeriodSeconds": 9007199254740990,
"deposit": "0.001"
},
"BrightIdUserRegistry": {
"deploy": false,
"context": "clrfund-arbitrum-goerli",
"verifier": "0xdbf0b2ee9887fe11934789644096028ed3febe9c"
}
},
"arbitrum-sepolia": {
Expand All @@ -43,8 +38,7 @@
},
"BrightIdUserRegistry": {
"context": "clrfund-arbitrum-goerli",
"verifier": "0xdbf0b2ee9887fe11934789644096028ed3febe9c",
"sponsor": "0xC7c81634Dac2de4E7f2Ba407B638ff003ce4534C"
"verifier": "0xdbf0b2ee9887fe11934789644096028ed3febe9c"
}
}
}
62 changes: 0 additions & 62 deletions contracts/tasks/subtasks/user/03-brightidSponsor.ts

This file was deleted.

14 changes: 5 additions & 9 deletions contracts/tasks/subtasks/user/04-brightidUserRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { EContracts } from '../../../utils/types'
const subtask = Subtask.getInstance()
const storage = ContractStorage.getInstance()

// @TODO remove this on the next major release when the sponsor contract is removed
// Hardcode with a dummy address for now
const BRIGHTID_SPONSOR = '0xC7c81634Dac2de4E7f2Ba407B638ff003ce4534C'

/**
* Deploy step registration and task itself
*/
Expand Down Expand Up @@ -58,15 +62,7 @@ subtask
'verifier'
)

let sponsor = subtask.tryGetConfigField<string>(
EContracts.BrightIdUserRegistry,
'sponsor'
)
if (!sponsor) {
sponsor = storage.mustGetAddress(EContracts.BrightIdSponsor, network)
}

const args = [encodeBytes32String(context), verifier, sponsor]
const args = [encodeBytes32String(context), verifier, BRIGHTID_SPONSOR]
const brightidUserRegistryContract = await subtask.deployContract(
EContracts.BrightIdUserRegistry,
{ signer: deployer, args }
Expand Down
9 changes: 0 additions & 9 deletions vue-app/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,11 @@ VITE_USER_REGISTRY_TYPE=simple
VITE_BRIGHTID_CONTEXT=clrfund-goerli


# These are for interacting with the BrightID api. When the SPONSOR_API_URL and one of the
# SPONSOR_KEY is set, a sponsor request will be sent to the BrightID node when the QR code
# to link users wallet address to BrightID is displayed. SPONSOR_KEY is used to sign the
# sponsor request. The SPONSOR_KEY_FOR_NETLIFY will trigger the netlify serverless function
# to send the sponsor request. The SPONSOR_KEY alone will send the request directly from
# the web app without using the netlify function.
# VITE_BRIGHTID_NODE_URL is the BrightID node used to query BrightID status. It needs to
# match the BRIGHTID_VERIFIER_ADDR defined in the contract .env file. This address is used
# to verify the signature returned from the BrightID verification status for user registration.
# The BRIGHTID_VERIFIER_ADDR value is the ethSigningAddress from the node url,
# e.g. https://brightid.clr.fund
#VITE_BRIGHTID_SPONSOR_KEY_FOR_NETLIFY=
#VITE_BRIGHTID_SPONSOR_KEY=
#VITE_BRIGHTID_SPONSOR_API_URL=https://brightid.clr.fund/brightid/v6/operations

# BrightId node one url, default to clrfund node at https://brightid.clr.fund/brightid/v6
#VITE_BRIGHTID_NODE_URL=https://app.brightid.org/node/v6
Expand Down
177 changes: 1 addition & 176 deletions vue-app/src/api/bright-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Contract, encodeBytes32String, toUtf8Bytes, decodeBase64, encodeBase64
import type { TransactionResponse, Signer } from 'ethers'

import { BrightIdUserRegistry } from './abi'
import { brightIdSponsorKey, brightIdNodeUrl } from './core'
import { brightIdNodeUrl } from './core'
import nacl from 'tweetnacl'

const BRIGHTID_APP_URL = 'https://app.brightid.org'
Expand Down Expand Up @@ -44,55 +44,6 @@ export interface Verification {
app: string
}

export interface Sponsorship {
timestamp: number
app: string
appHasAuthorized: boolean
spendRequested: boolean
}

type AppData = {
id: string
name: string
context?: string
verification: string
verifications?: string[]
verificationsUrl: string
logo?: string
url?: string
assignedSponsorships?: number
unusedSponsorships?: number
testing?: boolean
idAsHex?: boolean
usingBlindSig?: boolean
verificationExpirationLength?: number
sponsorPublicKey?: string
nodeUrl?: string
soulbound: boolean
callbackUrl?: string
}

type SponsorOperation = {
name: string
app: string
appUserId: string
timestamp: number
v: number
sig?: string
}

type SponsorData = {
hash?: string
error?: string
}

export async function selfSponsor(registryAddress: string, signer: Signer): Promise<TransactionResponse> {
const registry = new Contract(registryAddress, BrightIdUserRegistry, signer)
const userAddress = await signer.getAddress()
const transaction = await registry.sponsor(userAddress)
return transaction
}

// This link is for generating QR code
export function getBrightIdLink(userAddress: string): string {
const deepLink = `brightid://link-verification/${CONTEXT}/${userAddress}`
Expand Down Expand Up @@ -166,129 +117,3 @@ export async function getBrightId(contextId: string): Promise<BrightId> {
}
return brightId
}

/**
* Get the unused sponsorship amount
* @param context - the context to retrieve unused sponsorships for
*
* @returns Returns the number of sponsorships available to the specified `context`
*/
async function unusedSponsorships(context: string): Promise<number> {
const endpoint = `${NODE_URL}/apps/${context}`
const response = await fetch(endpoint)
const json = await response.json()

if (json['errorMessage']) {
throw new Error(JSON.stringify(json))
}

const data = json['data'] as AppData
return data.unusedSponsorships || 0
}

/**
* Call the BrightID sponsor operation endpoint to put a sponsorship request for the user
* @param userAddress user wallet address
* @returns sponsporship result or error
*/
export async function brightIdSponsor(userAddress: string): Promise<SponsorData> {
const endpoint = `${NODE_URL}/operations`

if (!brightIdSponsorKey) {
return { error: 'BrightId sponsor key not set' }
}

const sponsorships = await unusedSponsorships(CONTEXT)
if (typeof sponsorships === 'number' && sponsorships < 1) {
return { error: 'BrightID sponsorships not available' }
}

if (typeof sponsorships !== 'number') {
return { error: 'Invalid BrightID sponsorship' }
}

const timestamp = Date.now()

// these fields must be in alphabetical because
// BrightID nodes use 'fast-json-stable-stringify' that sorts fields
const op: SponsorOperation = {
app: CONTEXT,
appUserId: userAddress,
name: 'Sponsor',
timestamp,
v: 6,
}

const message = JSON.stringify(op)
const arrayedMessage = toUtf8Bytes(message)
const arrayedKey = decodeBase64(brightIdSponsorKey)
const signature = nacl.sign.detached(arrayedMessage, arrayedKey)
op.sig = encodeBase64(signature)

const res = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(op),
})
const json = await res.json()

if (json['error']) {
if (canIgnoreError(json.errorNum)) {
// sponsorship already sent recently, ignore this error
return { hash: '0x0' }
}
return { error: json['errorMessage'] }
} else {
return json['data']
}
}

/**
* Call the netlify function to invoke the BrightId sponsor api
* @param userAddress user wallet address
* @returns sponsorship data or error
*/
async function netlifySponsor(userAddress: string): Promise<SponsorData> {
const res = await fetch('/.netlify/functions/sponsor', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ userAddress }),
})

const json = await res.json()
if (res.status === 200) {
return json
}

if (res.status === 400 && canIgnoreError(json.errorNum)) {
return { hash: '0x0' }
}

// return the error
return json
}

/**
* Sponsor a BrightID user using the sponsorship api
* @param userAddress user wallet address
* @returns sponsporship result or error
*/
export async function sponsorUser(userAddress: string): Promise<SponsorData> {
if (brightIdSponsorKey) {
return brightIdSponsor(userAddress)
}

try {
return await netlifySponsor(userAddress)
} catch (err) {
if (err instanceof Error) {
return { error: (err as Error).message }
} else {
return { error: 'Unknown sponsorhip error' }
}
}
}
4 changes: 1 addition & 3 deletions vue-app/src/api/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ export const maxDecimals = Number(import.meta.env.VUE_APP_MAX_DECIMAL || 1)
// the number of records per batch in the `pending submissions` export file
export const exportBatchSize = Number(import.meta.env.VITE_EXPORT_BATCH_SIZE) || 60

// BrightId sponsorhip stuff, set these parameters to automatically sponsor user using the brightId URL
export const brightIdSponsorKey = import.meta.env.VITE_BRIGHTID_SPONSOR_KEY
// BrightId url for querying user verification
export const brightIdNodeUrl = import.meta.env.VITE_BRIGHTID_NODE_URL || 'https://brightid.clr.fund/brightid/v6'
export const brightIdSponsorUrl = import.meta.env.VITE_BRIGHTID_SPONSOR_API_URL

// wait for data to sync with the subgraph
export const MAX_WAIT_DEPTH = Number(import.meta.env.VITE_MAX_WAIT_DEPTH) || 15
Expand Down
Loading

0 comments on commit 45fe06d

Please sign in to comment.