Implement Badge Actions > Airdrop by Key

This commit is contained in:
Serkan Reis
2023-02-21 20:39:42 +03:00
parent e1adca8ddf
commit acd182dc60
5 changed files with 232 additions and 27 deletions
+17
View File
@@ -2,7 +2,24 @@
import { Word32Array } from 'jscrypto'
import { SHA256 } from 'jscrypto/SHA256'
import * as secp256k1 from 'secp256k1'
export function sha256(data: Buffer): Buffer {
return Buffer.from(SHA256.hash(new Word32Array(data)).toUint8Array())
}
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 ''
}
}