Skip to content

Instantly share code, notes, and snippets.

@EclesioMeloJunior
Last active May 19, 2024 03:48
Show Gist options
  • Save EclesioMeloJunior/bb1a65e22aa2a4e27263206b42e34c43 to your computer and use it in GitHub Desktop.
Save EclesioMeloJunior/bb1a65e22aa2a4e27263206b42e34c43 to your computer and use it in GitHub Desktop.
// you must provide the account hex, signature hex and the message wrapped with <Bytes>...</Bytes>
// To get the hex from the account you can do:
// import { u8aToHex, u8aToU8a } from "@polkadot/util";
// const hexPublicKey = u8aToHex(decodeAddress(account.address));
func TestVerifySR25519(t *testing.T) {
const pubhex = "0xf84d048da2ddae2d9d8fd6763f469566e8817a26114f39408de15547f6d47805"
const sighex = "0x48ce2c90e08651adfc8ecef84e916f6d1bb51ebebd16150ee12df247841a5437951ea0f9d632ca165e6ab391532e75e701be6a1caa88c8a6bcca3511f55b4183"
const challenge = "<Bytes>message to sign</Bytes>"
verifySR25519(
t,
pubhex,
sighex,
challenge,
)
}
func verifySR25519(t *testing.T, address, signature, challenge string) {
// decodes the public hex to a schnorrkel.PublicKey
pub, err := schnorrkel.NewPublicKeyFromHex(address)
require.NoError(t, err)
// decodes the signature hex to a schnorrkel.Signature
sig, err := schnorrkel.NewSignatureFromHex(signature)
require.NoError(t, err)
// this is the ctx the polkadot-js uses to create the signature
ctx := []byte("substrate")
// creates the SigningContext using the wrapped message
transcript := schnorrkel.NewSigningContext(ctx, []byte(challenge))
// verify the signature using the public key and the signing context
ok, err := pub.Verify(sig, transcript)
require.NoError(t, err)
require.True(t, ok)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment