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

Signature #220

Merged
merged 2 commits into from
Jun 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const Curve25519: KeyPairProvider = (
getPublicKey: () => toHex(ed25519.getPublicKey(privateKeyHex)),
sign: (messageHex: string): Result<string, Error> => {
try {
return ok(toHex(ed25519.sign(privateKeyHex, messageHex)))
return ok(toHex(ed25519.sign(messageHex, privateKeyHex)))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really should stop using Strings everywhere. I've brought this up in the past.

Best to create a PrivateKey type early.

And a Exactly32bytes type early as well, and use those, not strings.

} catch (error) {
return err(error as Error)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, expect, it } from 'vitest'
import { createSignatureMessage } from './create-signature-message'
import { ed25519 } from '@noble/curves/ed25519'
import base64url from 'base64url'

describe('signature', () => {
it('should verify signature', async () => {
const { signature, request, identity, origin, dAppDefinitionAddress } = {
request:
'eyJpdGVtcyI6eyJkaXNjcmltaW5hdG9yIjoiYXV0aG9yaXplZFJlcXVlc3QiLCJhdXRoIjp7ImRpc2NyaW1pbmF0b3IiOiJsb2dpbldpdGhDaGFsbGVuZ2UiLCJjaGFsbGVuZ2UiOiI3ODA3YzI5MDRmZDE4Njc3Mzg2MzljOGU0MDJmZTU1MzIwZGEyYzYyNzUxODllZjA1MTNkZTM2ZGRhMjBmN2VlIn0sInJlc2V0Ijp7ImFjY291bnRzIjpmYWxzZSwicGVyc29uYURhdGEiOmZhbHNlfX0sImludGVyYWN0aW9uSWQiOiJiMjVjODU4Ny1kMDhhLTQ4YmYtYmViNi1hODBhZTk4YzUxMDYiLCJtZXRhZGF0YSI6eyJ2ZXJzaW9uIjoyLCJkQXBwRGVmaW5pdGlvbkFkZHJlc3MiOiJhY2NvdW50X3RkeF8yXzEyeWY5Z2Q1M3lmZXA3YTY2OWZ2MnQzd203bno5emVlendkMDRuMDJhNDMza2VyOHZ6YTZyaGUiLCJuZXR3b3JrSWQiOjIsIm9yaWdpbiI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTE3MyJ9fQ',
signature:
'509dd1a493f2e04830543be490b04770f9bf36cf57dd4ed8ac52951eb850daa0110ba320a32122ee87594999a430cfde7d34a5e6009608839cd8c4a009f6dc07',
identity:
'faf57eae029aaf4312a11c433bd2dee645212a5b8cdaf3af3d4b423fc57472b7',
origin: 'http://localhost:5173',
dAppDefinitionAddress:
'account_tdx_2_12yf9gd53yfep7a669fv2t3wm7nz9zeezwd04n02a433ker8vza6rhe',
}

const decoded = base64url.decode(request)
const { interactionId } = JSON.parse(decoded)

const privateKey =
'a18e4e44d66f428302494fa34296efdc8db4b32488c853fbe31aab0002c128a5'

const message = createSignatureMessage({
interactionId,
dAppDefinitionAddress,
origin,
})._unsafeUnwrap()

const expectedSignatureHex = Buffer.from(
ed25519.sign(message, privateKey),
).toString('hex')

expect(signature).toBe(expectedSignatureHex)

const expectedPublicKey = Buffer.from(
ed25519.getPublicKey(privateKey),
).toString('hex')

expect(identity).toBe(expectedPublicKey)

const signatureValidation = ed25519.verify(signature, message, identity)

expect(signatureValidation).toBe(true)
})
})
Loading