Signing Data
Overview
Wallets, if supported, can sign some arbitrary data that can be verified via the algosdk.verifyBytes method.
Signing with the default wallet
If you only want to sign data with the default wallet, you can simply call:
- Javascript
- TypeScript
try {
const result = await window.algorand.signBytes({
data: new Uint8Array([...]),
});
console.log(result);
/*
{
id: 'awesome-wallet',
signature: [23, 54, 65 ...],
}
*/
} catch (error) {
// handle error
}
import type { IBaseResult, ISignBytesResult } from '@agoralabs-sh/algorand-provider';
try {
const result: IBaseResult & ISignBytesResult = await window.algorand.signBytes({
data: new Uint8Array([...]),
});
console.log(result);
/*
{
id: 'awesome-wallet',
signature: [23, 54, 65 ...],
}
*/
} catch (error) {
// handle error
}
If this operation is not supported, then a WalletOperationNotSupportedError
will be thrown.
Signing with a specific wallet
If you want to target a specific wallet to sign data, you can simply pass the ID of the wallet to the options object:
- Javascript
- TypeScript
try {
const result = await window.algorand.signBytes({
data: new Uint8Array([...]),
id: 'another-awesome-wallet',
});
console.log(result);
/*
{
id: 'another-awesome-wallet',
signature: [23, 54, 65 ...],
}
*/
} catch (error) {
// handle error
}
import type { IBaseResult, ISignBytesResult } from '@agoralabs-sh/algorand-provider';
try {
const result: IBaseResult & ISignBytesResult = await window.algorand.signBytes({
data: new Uint8Array([...]),
id: 'another-awesome-wallet',
});
console.log(result);
/*
{
id: 'another-awesome-wallet',
signature: [23, 54, 65 ...],
}
*/
} catch (error) {
// handle error
}
If the specified wallet does not exist, then a WalletDoesNotExistError
will be thrown.
Signing with a specific account
If you want to specify which account to use, you can pass the signer in the options object:
- Javascript
- TypeScript
try {
const result = await window.algorand.signBytes({
data: new Uint8Array([...]),
signer: 'P3AIQVDJ2CTH54KSJE63YWB7IZGS4W4JGC53I6GK72BGZ5BXO2B2PS4M4U',
});
console.log(result);
/*
{
id: 'awesome-wallet',
signature: [23, 54, 65 ...],
}
*/
} catch (error) {
// handle error
}
import type { IBaseResult, ISignBytesResult } from '@agoralabs-sh/algorand-provider';
try {
const result: IBaseResult & ISignBytesResult = await window.algorand.signBytes({
data: new Uint8Array([...]),
signer: 'P3AIQVDJ2CTH54KSJE63YWB7IZGS4W4JGC53I6GK72BGZ5BXO2B2PS4M4U',
});
console.log(result);
/*
{
id: 'awesome-wallet',
signature: [23, 54, 65 ...],
}
*/
} catch (error) {
// handle error
}
If the user has not authorized the specified signer or the signer does not exist in the wallet, then a UnauthorizedSignerError
will be thrown.
Verifying the signature
Once the data has been signed, you can use the algosdk.verifyBytes function to verify that the data has been signed using the private key.
- Javascript
- TypeScript
const algosdk = require('algosdk);
const data = new Uint8Array([...]);
const signer = 'P3AIQVDJ2CTH54KSJE63YWB7IZGS4W4JGC53I6GK72BGZ5BXO2B2PS4M4U';
const result = await window.algorand.signBytes({
data,
signer,
});
if (!algosdk.verifyBytes(data, result.signature, signer)) {
throw new Error('invalid signature!!');
}
console.log('data has been verified!');
import type { IBaseResult, ISignBytesResult } from '@agoralabs-sh/algorand-provider';
import { verifyBytes } from 'algosdk';
const data: Uint8Array = new Uint8Array([...]);
const signer: string = 'P3AIQVDJ2CTH54KSJE63YWB7IZGS4W4JGC53I6GK72BGZ5BXO2B2PS4M4U';
const result: IBaseResult & ISignBytesResult = await window.algorand.signBytes({
data,
signer,
});
if (!verifyBytes(data, result.signature, signer)) {
throw new Error('invalid signature!!');
}
console.log('data has been verified!');