-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c07fe54
commit 6a3239f
Showing
2 changed files
with
50 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { keccak256 } from 'ethers/lib/utils'; | ||
import * as circomlib from 'circomlibjs'; | ||
import * as crypto from 'crypto'; | ||
import { ethers } from 'ethers'; | ||
|
||
export async function createEddsaPrivateKey( | ||
ecdsaPublicKeyOwnerEthereumAccount: string, | ||
signer: ethers.Wallet, | ||
): Promise<string> { | ||
const message = keccak256(ecdsaPublicKeyOwnerEthereumAccount); | ||
const eddsaPrivateKey = await signer.signMessage(message); | ||
|
||
return eddsaPrivateKey; | ||
} | ||
|
||
export async function createEddsaPublicKey( | ||
eddsaPrivateKey: string, | ||
): Promise<string> { | ||
const eddsa = await circomlib.buildEddsa(); | ||
const babyJub = await circomlib.buildBabyjub(); | ||
|
||
const privateKeyBytes = Buffer.from(eddsaPrivateKey, 'hex'); | ||
const publicKeyPoints = eddsa.prv2pub(privateKeyBytes); | ||
const eddsaPublicKey = Buffer.from( | ||
babyJub.packPoint(publicKeyPoints), | ||
).toString('hex'); | ||
|
||
return eddsaPublicKey; | ||
} | ||
|
||
export async function createEddsaSignature( | ||
payload: any, | ||
eddsaPrivateKey: string, | ||
): Promise<string> { | ||
const eddsa = await circomlib.buildEddsa(); | ||
const hashedPayload = crypto | ||
.createHash('sha256') | ||
.update(JSON.stringify(payload)) | ||
.digest(); | ||
|
||
const eddsaSignature = eddsa.signPedersen(eddsaPrivateKey, hashedPayload); | ||
const packedSignature = eddsa.packSignature(eddsaSignature); | ||
const signature = Buffer.from(packedSignature).toString('hex'); | ||
return signature; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters