-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from lollerfirst/sign-mint-quotes
NUT-20 Signed Mint Payloads
- Loading branch information
Showing
9 changed files
with
272 additions
and
15 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
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
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,41 @@ | ||
import { schnorr } from '@noble/curves/secp256k1'; | ||
import { SerializedBlindedMessage } from '../model/types'; | ||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils'; | ||
import { sha256 } from '@noble/hashes/sha256'; | ||
|
||
function constructMessage( | ||
quote: string, | ||
blindedMessages: Array<SerializedBlindedMessage> | ||
): Uint8Array { | ||
let message = quote; | ||
for (const blindedMessage of blindedMessages) { | ||
message += blindedMessage.B_; | ||
} | ||
const msgbytes = new TextEncoder().encode(message); | ||
return sha256(msgbytes); | ||
} | ||
|
||
export function signMintQuote( | ||
privkey: string, | ||
quote: string, | ||
blindedMessages: Array<SerializedBlindedMessage> | ||
): string { | ||
const message = constructMessage(quote, blindedMessages); | ||
const privkeyBytes = hexToBytes(privkey); | ||
const signature = schnorr.sign(message, privkeyBytes); | ||
return bytesToHex(signature); | ||
} | ||
|
||
export function verifyMintQuoteSignature( | ||
pubkey: string, | ||
quote: string, | ||
blindedMessages: Array<SerializedBlindedMessage>, | ||
signature: string | ||
): boolean { | ||
const sigbytes = hexToBytes(signature); | ||
let pubkeyBytes = hexToBytes(pubkey); | ||
if (pubkeyBytes.length !== 33) return false; | ||
pubkeyBytes = pubkeyBytes.slice(1); | ||
const message = constructMessage(quote, blindedMessages); | ||
return schnorr.verify(sigbytes, message, pubkeyBytes); | ||
} |
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
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
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
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
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
Oops, something went wrong.