2023-02-21 15:26:10 +00:00
|
|
|
/* eslint-disable eslint-comments/disable-enable-pair */
|
|
|
|
|
|
|
|
import { Word32Array } from 'jscrypto'
|
|
|
|
import { SHA256 } from 'jscrypto/SHA256'
|
2023-02-21 17:39:42 +00:00
|
|
|
import * as secp256k1 from 'secp256k1'
|
2023-02-21 15:26:10 +00:00
|
|
|
|
|
|
|
export function sha256(data: Buffer): Buffer {
|
|
|
|
return Buffer.from(SHA256.hash(new Word32Array(data)).toUint8Array())
|
|
|
|
}
|
2023-02-21 17:39:42 +00:00
|
|
|
|
|
|
|
export function generateSignature(id: number, owner: string, privateKey: string) {
|
|
|
|
try {
|
|
|
|
const message = `claim badge ${id} for user ${owner}`
|
|
|
|
|
|
|
|
const privKey = Buffer.from(privateKey, 'hex')
|
|
|
|
// const pubKey = Buffer.from(secp256k1.publicKeyCreate(privKey, true))
|
|
|
|
const msgBytes = Buffer.from(message, 'utf8')
|
|
|
|
const msgHashBytes = sha256(msgBytes)
|
|
|
|
const signedMessage = secp256k1.ecdsaSign(msgHashBytes, privKey)
|
|
|
|
return Buffer.from(signedMessage.signature).toString('hex')
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|