Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Action Signing and verification and Utilities #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/src/examples/exampleUsage.js.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { signActionPayload } from '../utils/signActionPayload';

const actionPayload = {
actionType: 'transfer',
amount: 500,
destination: 'G7huw9ZxyV6HX...Xnb6Vhvqpk',
};

async function signActionExample() {
const wallet = window.solana;

if (!wallet.isConnected) {
await wallet.connect();
}

try {
const signedAction = await signActionPayload(actionPayload, wallet);
console.log('Signed Action:', signedAction);
} catch (error) {
console.error('Error signing action:', error.message);
}
}

signActionExample();
20 changes: 20 additions & 0 deletions src/utils/verifySignedAction.js.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { PublicKey } from '@solana/web3.js';
import nacl from 'tweetnacl';
import bs58 from 'bs58';

/**
* Verifies the signed action payload.
*
* @param {Object} signedAction - The signed action payload with signature and publicKey.
* @returns {boolean} True if the signature is valid, false otherwise.
*/
export function verifySignedAction(signedAction) {
const { payload, signature, publicKey } = signedAction;
const message = new TextEncoder().encode(JSON.stringify(payload));

return nacl.sign.detached.verify(
message,
bs58.decode(signature),
new PublicKey(publicKey).toBytes()
);
}