-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from joeymeere/fix/legacy-message-handling
fix(tx): handle generic messages as input
- Loading branch information
Showing
6 changed files
with
80 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,66 @@ | ||
import * as bs58 from "bs58"; | ||
import { VersionedMessage } from "@solana/web3.js"; | ||
import { | ||
Message, | ||
MessageAccountKeys, | ||
MessageV0, | ||
PublicKey, | ||
Transaction, | ||
TransactionMessage, | ||
VersionedMessage, | ||
VersionedTransaction, | ||
} from "@solana/web3.js"; | ||
|
||
export function decodeAndDeserialize(tx: string): { | ||
message: VersionedMessage; | ||
interface DeserializedTransaction { | ||
message: TransactionMessage; | ||
version: number | "legacy"; | ||
} { | ||
accountKeys: PublicKey[]; | ||
} | ||
|
||
/** | ||
* Decodes a base58 encoded transaction and deserializes it into a TransactionMessage | ||
* @param tx - Base58 encoded transaction string | ||
* @returns Object containing the deserialized message, version, and account keys | ||
* @throws Error if deserialization fails | ||
*/ | ||
export function decodeAndDeserialize(tx: string): DeserializedTransaction { | ||
if (!tx) { | ||
throw new Error("Transaction string is required"); | ||
} | ||
|
||
try { | ||
const messageBytes = bs58.default.decode(tx); | ||
const version = VersionedMessage.deserializeMessageVersion(messageBytes); | ||
const message = VersionedMessage.deserialize(messageBytes); | ||
let message: TransactionMessage; | ||
let accountKeys: PublicKey[]; | ||
|
||
if (version === "legacy") { | ||
const legacyMessage = Message.from(messageBytes); | ||
accountKeys = legacyMessage.accountKeys; | ||
|
||
const intermediate = VersionedMessage.deserialize( | ||
new MessageV0(legacyMessage).serialize() | ||
); | ||
message = TransactionMessage.decompile(intermediate, { | ||
addressLookupTableAccounts: [], | ||
}); | ||
} else { | ||
const versionedMessage = VersionedMessage.deserialize(messageBytes); | ||
accountKeys = versionedMessage.staticAccountKeys; | ||
|
||
message = TransactionMessage.decompile(versionedMessage, { | ||
addressLookupTableAccounts: [], | ||
}); | ||
} | ||
|
||
return { version, message }; | ||
return { | ||
version, | ||
message, | ||
accountKeys, | ||
}; | ||
} catch (error) { | ||
console.error(error); | ||
throw new Error("Failed to decode transaction."); | ||
if (error instanceof Error) { | ||
throw new Error(`Failed to decode transaction: ${error.message}`); | ||
} | ||
throw new Error("Failed to decode transaction: Unknown error"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters