mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-03 17:30:07 +00:00
43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import { expect } from 'chai'
|
|
import { afterEach, beforeEach, describe, it } from 'mocha'
|
|
import { Buffer } from 'node:buffer'
|
|
import Sinon from 'sinon'
|
|
import nacl from 'tweetnacl'
|
|
import { verifySignature } from '../src/verifySignature.js'
|
|
|
|
describe('VerifySignature', () => {
|
|
let publicKey: Uint8Array
|
|
let secretKey: Uint8Array
|
|
let clock: Sinon.SinonFakeTimers
|
|
|
|
beforeEach(() => {
|
|
clock = Sinon.useFakeTimers();
|
|
|
|
({ publicKey, secretKey } = nacl.sign.keyPair())
|
|
})
|
|
|
|
afterEach(() => {
|
|
Sinon.restore()
|
|
clock.restore()
|
|
})
|
|
|
|
it('reutrn true if signature is verified', () => {
|
|
const timestamp = Date.now().toString()
|
|
const body = 'test body'
|
|
const signature = nacl.sign.detached(
|
|
Buffer.from(timestamp + body),
|
|
secretKey
|
|
)
|
|
|
|
const verifiedSignature = verifySignature({
|
|
publicKey: Buffer.from(publicKey).toString('hex'),
|
|
signature: Buffer.from(signature).toString('hex'),
|
|
timestamp,
|
|
body
|
|
})
|
|
|
|
expect(verifiedSignature.body).equal(body)
|
|
expect(verifiedSignature.isValid).true
|
|
})
|
|
})
|