Skip to content

Commit

Permalink
fix: Solana verify signature
Browse files Browse the repository at this point in the history
  • Loading branch information
gmolki committed Oct 10, 2024
1 parent c3a114a commit c6e7ece
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 12 additions & 4 deletions packages/solana/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class SOLAccount extends Account {

constructor(publicKey: PublicKey, walletOrKeypair: Keypair | MessageSigner) {
super(publicKey.toString())

if (walletOrKeypair instanceof Keypair) {
this.keypair = walletOrKeypair
this.isKeypair = true
Expand All @@ -54,18 +55,25 @@ export class SOLAccount extends Account {
const buffer = message.getVerificationBuffer()

let signature
let publicKey: string // as Base58

if (this.wallet) {
const signed = await this.wallet.signMessage(buffer)
if (signed instanceof Uint8Array) signature = signed
else signature = signed.signature
signature = signed instanceof Uint8Array ? signed : signed.signature
publicKey = this.wallet.publicKey.toBase58()
} else if (this.keypair) {
signature = base58.encode(nacl.sign.detached(buffer, this.keypair.secretKey))
signature = nacl.sign.detached(buffer, this.keypair.secretKey)
publicKey = this.keypair.publicKey.toBase58()
} else {
throw new Error('Cannot sign message')
}

const signatureObj = JSON.stringify({ signature, publicKey: this.address })
signature = base58.encode(signature)

const signatureObj = JSON.stringify({
signature,
publicKey: publicKey,
})

if (verifySolana(buffer, signatureObj)) return signatureObj
throw new Error('Cannot proof the integrity of the signature')
Expand Down
4 changes: 2 additions & 2 deletions packages/solana/src/verify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SignableMessage } from '@aleph-sdk/account'
import bs58 from 'bs58'
import base58 from 'bs58'
import nacl from 'tweetnacl'

/**
Expand All @@ -20,7 +20,7 @@ export function verifySolana(message: Uint8Array | SignableMessage, serializedSi
const { signature, publicKey } = JSON.parse(serializedSignature)

try {
return nacl.sign.detached.verify(message, bs58.decode(signature), bs58.decode(publicKey))
return nacl.sign.detached.verify(message, base58.decode(signature), base58.decode(publicKey))
} catch (e: unknown) {
return false
}
Expand Down

0 comments on commit c6e7ece

Please sign in to comment.