Skip to content

Commit

Permalink
Merge pull request #78 from BigWhaleLabs/use-proof
Browse files Browse the repository at this point in the history
use passed proof params
  • Loading branch information
MixailE authored Oct 30, 2023
2 parents 7631826 + 5a6b430 commit 412d8a9
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 20 deletions.
25 changes: 18 additions & 7 deletions src/helpers/createAttestationInput.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import CreateProofParams from 'models/CreateProofParams'
import Signature from 'models/Signature'
import getEddsaPublicKey from 'helpers/getEddsaPublicKey'
import getHashes from 'helpers/getHashes'
import getInput from 'helpers/getInput'
import getProof from 'helpers/getProof'
import unpackSignature from 'helpers/unpackSignature'

async function generateMerkleTreeInputs(
params: CreateProofParams,
hash: string
) {
const { pathIndices, siblings } = await getProof(params.id, hash)

return {
pathElements: siblings,
pathIndices,
}
}

export default async function createAttestationInput(
params: CreateProofParams,
{ message, signature }: Signature
{ message, publicKey, signature }: Signature
) {
const hashes = await getHashes(params.id)
const eddsaPublicKey = await getEddsaPublicKey()
const hash = message[1]
const merkleTreeInputs = await getInput(hash, hashes)
const eddsaPublicKey = publicKey ?? (await getEddsaPublicKey())
const merkleTreeInputs =
params.merkleTreeInputs ??
(await generateMerkleTreeInputs(params, message[1]))
const { R8x, R8y, S } = await unpackSignature(signature)

const inputs = {
Expand Down
25 changes: 13 additions & 12 deletions src/helpers/createPasswordInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ import CreatePasswordProofParams from 'models/CreatePasswordProofParams'
import getBatchOfEntanglementsHashes from 'helpers/getBatchOfEntanglementsHashes'
import getInput from 'helpers/getInput'

export default async function createPasswordInput(
params: CreatePasswordProofParams
) {
const {
attestationHash,
entanglement,
id,
message: attestationMessage,
password,
} = params

async function generateMerkleTreeInputs(params: CreatePasswordProofParams) {
const { attestationHash, entanglement, id } = params
const hash = BigNumber.from(entanglement).toHexString().toLowerCase()
const hashes = await getBatchOfEntanglementsHashes(
id,
hash,
attestationHash,
params.isDev
)
const merkleTreeInputs = await getInput(hash, hashes)

return getInput(hash, hashes)
}

export default async function createPasswordInput(
params: CreatePasswordProofParams
) {
const { message: attestationMessage, password } = params

const merkleTreeInputs =
params.merkleTreeInputs ?? (await generateMerkleTreeInputs(params))

const inputs = {
attestationMessage,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/getHashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default async function getHashes(id: number) {
`${env.VITE_KETL_INVITES_BACKEND}/merkle/hashes?attestationType=${id}`
)

return response.json()
return await response.json()
} catch (e) {
throw new GeneratorError(
`Can't get invitation list, please try again later`,
Expand Down
20 changes: 20 additions & 0 deletions src/helpers/getProof.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import GeneratorError from 'helpers/GeneratorError'
import env from 'helpers/env'

export default async function getProof(id: number, hash: string) {
try {
const response = await fetch(
`${env.VITE_KETL_INVITES_BACKEND}/merkle/proof?attestationType=${id}&hash=${hash}`
)

return (await response.json()) as Promise<{
siblings: string[]
pathIndices: number[]
}>
} catch (e) {
throw new GeneratorError(
`Can't get invitation list, please try again later`,
{ cause: e }
)
}
}
2 changes: 2 additions & 0 deletions src/models/CreatePasswordProofParams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AttestationType from 'models/AttestationType'
import MerkleTreeInputs from 'models/MerkleTreeInputs'

interface CreatePasswordProofParams {
isDev?: boolean
Expand All @@ -7,6 +8,7 @@ interface CreatePasswordProofParams {
password: string
entanglement: string
attestationHash: string
merkleTreeInputs?: MerkleTreeInputs
}

export default CreatePasswordProofParams
2 changes: 2 additions & 0 deletions src/models/CreateProofParams.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import AttestationType from 'models/AttestationType'
import MerkleTreeInputs from 'models/MerkleTreeInputs'

interface CreateProofParams {
password: string
id: AttestationType
merkleTreeInputs?: MerkleTreeInputs
}

export default CreateProofParams
4 changes: 4 additions & 0 deletions src/models/MerkleTreeInputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default interface MerkleTreeInputs {
pathElements: string[]
pathIndices: number[]
}
3 changes: 3 additions & 0 deletions src/models/Signature.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import PublicKey from 'models/PublicKey'

export default interface Signature {
signature: string
message: string[]
publicKey?: PublicKey
}

0 comments on commit 412d8a9

Please sign in to comment.