Skip to content

Instantly share code, notes, and snippets.

@raress96
Created April 4, 2022 11:43
Show Gist options
  • Save raress96/9a9805073d03bf3b954c9cd096bfa788 to your computer and use it in GitHub Desktop.
Save raress96/9a9805073d03bf3b954c9cd096bfa788 to your computer and use it in GitHub Desktop.
Elrond smart contract verify signature
fn verify_signature(
&self,
caller: &ManagedAddress,
url: &ManagedBuffer,
signature: &Signature<Self::Api>,
) {
let mut data = ManagedBuffer::new();
data.append(caller.as_managed_buffer());
data.append(url);
let signer: ManagedAddress = self.signer().get();
let valid_signature = self.crypto().verify_ed25519_managed::<MAX_SIGNATURE_DATA_LEN>(
signer.as_managed_byte_array(),
&data,
signature,
);
require!(valid_signature, "Invalid signature");
}
const privateKey = UserSecretKey.fromPem(this.apiConfigService.getSignerPemFile());
const publicKey = privateKey.generatePublicKey();
console.log('public key hex', publicKey.hex());
// 6d795f616464726573735F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F is `address:my_address` in Mandos
const signature = privateKey.sign(
Buffer.concat([
Buffer.from('6d795f616464726573735F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F', 'hex'), // or use publicKey.hex()
Buffer.from('nft-create-uri'),
])
);
const signatureHex = signature.toString('hex');
console.log('signature ', signatureHex);
console.log(
'verifying signature',
publicKey.verify(
Buffer.concat([
Buffer.from('6d795f616464726573735F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F', 'hex'),
Buffer.from('nft-create-uri'),
]),
Buffer.from(signatureHex, 'hex')
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment