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: improve Solana rebroadcasting logic for sending transactions #190

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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
100 changes: 100 additions & 0 deletions src/core/Solana/KeypairWalletAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { ed25519 } from '@noble/curves/ed25519'
import type {
SignerWalletAdapter,
WalletName,
} from '@solana/wallet-adapter-base'
import {
BaseSignerWalletAdapter,
WalletConfigError,
WalletNotConnectedError,
WalletReadyState,
isVersionedTransaction,
} from '@solana/wallet-adapter-base'
import type {
Transaction,
TransactionVersion,
VersionedTransaction,
} from '@solana/web3.js'
import { Keypair } from '@solana/web3.js'
import { decode } from 'bs58'

export const KeypairWalletName =
'Keypair Wallet' as WalletName<'Keypair Wallet'>

/**
* This keypair wallet adapter is unsafe to use on the frontend and is only included to provide an easy way for applications to test
* Wallet Adapter without using a third-party wallet.
*/
export class KeypairWalletAdapter
extends BaseSignerWalletAdapter
implements SignerWalletAdapter
{
name = KeypairWalletName
url = 'https://github.com/anza-xyz/wallet-adapter'
icon = ''
supportedTransactionVersions: ReadonlySet<TransactionVersion> = new Set([
'legacy',
0,
])

/**
* Storing a keypair locally like this is not safe because any application using this adapter could retrieve the
* secret key, and because the keypair will be lost any time the wallet is disconnected or the window is refreshed.
*/
private _keypair: Keypair | undefined

constructor(privateKey: string) {
if (!privateKey) {
throw new WalletConfigError()
}
super()
this._keypair = Keypair.fromSecretKey(decode(privateKey))
}

get connecting() {
return false
}

get publicKey() {
return this._keypair?.publicKey || null
}

get readyState() {
return WalletReadyState.Loadable
}

async connect(privateKey?: string): Promise<void> {
if (!privateKey) {
throw new WalletConfigError()
}
this._keypair = Keypair.fromSecretKey(decode(privateKey))
}

async disconnect(): Promise<void> {
this._keypair = undefined
}

async signTransaction<T extends Transaction | VersionedTransaction>(
transaction: T
): Promise<T> {
if (!this._keypair) {
throw new WalletNotConnectedError()
}

if (isVersionedTransaction(transaction)) {
transaction.sign([this._keypair])
} else {
transaction.partialSign(this._keypair)
}

return transaction
}

async signMessage(message: Uint8Array): Promise<Uint8Array> {
if (!this._keypair) {
throw new WalletNotConnectedError()
}

return ed25519.sign(message, this._keypair.secretKey.slice(0, 32))
}
}
Loading
Loading