Skip to content
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
19 changes: 19 additions & 0 deletions src/wallets/solana/solanaClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest'

import { UserInputValidationError } from '../../errors'
import { SolanaClient } from './solanaClient'

describe('SolanaClient.importAccount', () => {
it('rejects odd-length hex private keys before encryption', async () => {
const client = new SolanaClient()

await expect(
client.importAccount({ privateKey: `0x${'a'.repeat(63)}` }),
).rejects.toThrow(UserInputValidationError)
await expect(
client.importAccount({ privateKey: `0x${'a'.repeat(63)}` }),
).rejects.toThrow(
'Private key hex string must contain an even number of characters',
)
})
})
10 changes: 10 additions & 0 deletions src/wallets/solana/solanaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,19 @@ export class SolanaClient {
if (!/^[0-9a-fA-F]+$/.test(hex)) {
throw new UserInputValidationError('Invalid hex string')
}
if (hex.length % 2 !== 0) {
throw new UserInputValidationError(
'Private key hex string must contain an even number of characters',
)
}
privateKeyBytes = Uint8Array.from(Buffer.from(hex, 'hex'))
} else if (/^[0-9a-fA-F]+$/.test(options.privateKey)) {
// Hex format without prefix
if (options.privateKey.length % 2 !== 0) {
throw new UserInputValidationError(
'Private key hex string must contain an even number of characters',
)
}
privateKeyBytes = Uint8Array.from(Buffer.from(options.privateKey, 'hex'))
} else {
// Assume base58 format (standard Solana format)
Expand Down