60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
![]() |
import { Keypair, Transaction, VersionedTransaction } from "@solana/web3.js"
|
||
|
|
||
|
import bs58 from "bs58"
|
||
|
|
||
|
export class BaseWallet extends Keypair {
|
||
|
constructor(privateKey) {
|
||
|
super()
|
||
|
try {
|
||
|
const keypair =
|
||
|
typeof privateKey === "string"
|
||
|
? Keypair.fromSecretKey(bs58.decode(privateKey))
|
||
|
: privateKey
|
||
|
|
||
|
Object.assign(this, keypair)
|
||
|
} catch (error) {
|
||
|
throw new Error(
|
||
|
`Failed to initialize wallet: ${
|
||
|
error instanceof Error ? error.message : "Unknown error"
|
||
|
}`
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async signTransaction(transaction) {
|
||
|
try {
|
||
|
if (transaction instanceof Transaction) {
|
||
|
transaction.partialSign(this)
|
||
|
} else if (transaction instanceof VersionedTransaction) {
|
||
|
transaction.sign([this])
|
||
|
}
|
||
|
return transaction
|
||
|
} catch (error) {
|
||
|
throw new Error(
|
||
|
`Failed to sign transaction: ${
|
||
|
error instanceof Error ? error.message : "Unknown error"
|
||
|
}`
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async signAllTransactions(transactions) {
|
||
|
try {
|
||
|
return transactions.map(tx => {
|
||
|
if (tx instanceof Transaction) {
|
||
|
tx.partialSign(this)
|
||
|
} else if (tx instanceof VersionedTransaction) {
|
||
|
tx.sign([this])
|
||
|
}
|
||
|
return tx
|
||
|
})
|
||
|
} catch (error) {
|
||
|
throw new Error(
|
||
|
`Failed to sign transactions: ${
|
||
|
error instanceof Error ? error.message : "Unknown error"
|
||
|
}`
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
}
|