Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #6967: Use constants provided by core for Solana Instruction Deco…
Browse files Browse the repository at this point in the history
…ding (#7187)

Use constants provided by core for Solana Instruction Decoding
  • Loading branch information
StephenHeaps authored Apr 3, 2023
1 parent 9d19802 commit c36995b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 43 deletions.
16 changes: 8 additions & 8 deletions Sources/BraveWallet/Crypto/Transactions/TransactionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,15 +419,15 @@ enum TransactionParser {
symbol = "SOL"
switch instructionType {
case .transfer, .transferWithSeed:
if let instructionLamports = instruction.decodedData?.paramFor(.lamports)?.value,
if let instructionLamports = instruction.decodedData?.paramFor(BraveWallet.Lamports)?.value,
let instructionLamportsValue = BDouble(instructionLamports),
let fromPubkey = instruction.accountMetas[safe: 0]?.pubkey,
let toPubkey = instruction.accountMetas[safe: 1]?.pubkey,
fromPubkey != toPubkey { // only show lamports as transfered if the amount is going to a different pubKey
valueFromInstructions += instructionLamportsValue
}
case .withdrawNonceAccount:
if let instructionLamports = instruction.decodedData?.paramFor(.lamports)?.value,
if let instructionLamports = instruction.decodedData?.paramFor(BraveWallet.Lamports)?.value,
let instructionLamportsValue = BDouble(instructionLamports) {
if let nonceAccount = instruction.accountMetas[safe: 0]?.pubkey,
nonceAccount == fromAddress {
Expand All @@ -437,15 +437,15 @@ enum TransactionParser {
}
}
case .createAccount, .createAccountWithSeed:
if let instructionLamports = instruction.decodedData?.paramFor(.lamports)?.value,
if let instructionLamports = instruction.decodedData?.paramFor(BraveWallet.Lamports)?.value,
let instructionLamportsValue = BDouble(instructionLamports) {
if let fromPubkey = instruction.accountMetas[safe: 0]?.pubkey,
fromPubkey == fromAddress {
valueFromInstructions += instructionLamportsValue
}
}
default:
if let instructionLamports = instruction.decodedData?.paramFor(.lamports)?.value,
if let instructionLamports = instruction.decodedData?.paramFor(BraveWallet.Lamports)?.value,
let instructionLamportsValue = BDouble(instructionLamports) {
valueFromInstructions += instructionLamportsValue
}
Expand Down Expand Up @@ -528,7 +528,7 @@ enum TransactionParser {
}
details.append(contentsOf: accounts)

if let lamportsParam = decodedData.paramFor(.lamports),
if let lamportsParam = decodedData.paramFor(BraveWallet.Lamports),
let lamportsValue = formatter.decimalString(for: lamportsParam.value, radix: .decimal, decimals: 9)?.trimmingTrailingZeros {
details.append(.init(key: Strings.Wallet.solanaAmount, value: "\(lamportsValue) SOL"))
}
Expand All @@ -543,7 +543,7 @@ enum TransactionParser {

} else if instruction.isTokenProgram {
let accounts = decodedData.accountParams.enumerated().compactMap { (index, param) -> SolanaTxDetails.ParsedSolanaInstruction.KeyValue? in
if param.name == "signers" { // special case
if param.name == BraveWallet.Signers { // special case
// the signers are the `accountMetas` from this index to the end of the array
// its possible to have any number of signers, including 0
if instruction.accountMetas[safe: index] != nil {
Expand All @@ -561,8 +561,8 @@ enum TransactionParser {
}
details.append(contentsOf: accounts)

if let amountParam = decodedData.paramFor(.amount),
let decimalsParam = decodedData.paramFor(.decimals),
if let amountParam = decodedData.paramFor(BraveWallet.Amount),
let decimalsParam = decodedData.paramFor(BraveWallet.Decimals),
let decimals = Int(decimalsParam.value),
let amountValue = formatter.decimalString(for: amountParam.value, radix: .decimal, decimals: decimals)?.trimmingTrailingZeros {
details.append(.init(key: Strings.Wallet.solanaAmount, value: amountValue))
Expand Down
28 changes: 4 additions & 24 deletions Sources/BraveWallet/Extensions/SolanaInstruction+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ extension BraveWallet.SolanaInstruction {

/// Returns the `to_account` pubkey for the instruction if available
var toPubkey: String? {
guard let index = decodedData?.accountParams.firstIndex(where: { $0.name == "to_account" }) else { return nil }
guard let index = decodedData?.accountParams.firstIndex(where: { $0.name == BraveWallet.ToAccount }) else { return nil }
return accountMetas[safe: index]?.pubkey
}

/// Returns the `from_account` pubkey for the instruction if available
var fromPubkey: String? {
guard let index = decodedData?.accountParams.firstIndex(where: { $0.name == "from_account" }) else { return nil }
guard let index = decodedData?.accountParams.firstIndex(where: { $0.name == BraveWallet.FromAccount }) else { return nil }
return accountMetas[safe: index]?.pubkey
}
}
Expand Down Expand Up @@ -138,27 +138,7 @@ extension BraveWallet.SolanaTokenInstruction {
}

extension BraveWallet.DecodedSolanaInstructionData {
func paramFor(_ paramKey: ParamKey) -> BraveWallet.SolanaInstructionParam? {
params.first(where: { $0.name == paramKey.rawValue })
}

// brave-core/components/brave_wallet/browser/solana_instruction_data_decoder.cc
// GetSystemInstructionParams() / GetTokenInstructionParams()
enum ParamKey: String {
case lamports
case amount
case decimals
case space
case owner
case base
case seed
case fromSeed = "from_seed"
case fromOwner = "from_owner"
case nonceAccount = "nonce_account"
case authorityType = "authority_type"
case newAuthority = "new_authority"
case mintAuthority = "mint_authority"
case freezeAuthority = "freeze_authority"
case numOfSigners = "num_of_signers"
func paramFor(_ paramName: String) -> BraveWallet.SolanaInstructionParam? {
params.first(where: { $0.name == paramName })
}
}
22 changes: 11 additions & 11 deletions Tests/BraveWalletTests/TransactionParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -895,9 +895,9 @@ class TransactionParserTests: XCTestCase {
decodedData: .init(
instructionType: UInt32(BraveWallet.SolanaSystemInstruction.transfer.rawValue),
accountParams: [
.init(name: "from_account", localizedName: "From Account"),
.init(name: "to_account", localizedName: "To Account"),],
params: [.init(name: "lamports", localizedName: "Lamports", value: "10000", type: .uint64)]
.init(name: BraveWallet.FromAccount, localizedName: "From Account"),
.init(name: BraveWallet.ToAccount, localizedName: "To Account"),],
params: [.init(name: BraveWallet.Lamports, localizedName: "Lamports", value: "10000", type: .uint64)]
)
)
let expectedParsedTransfer = SolanaTxDetails.ParsedSolanaInstruction(
Expand Down Expand Up @@ -925,14 +925,14 @@ class TransactionParserTests: XCTestCase {
decodedData: .init(
instructionType: UInt32(BraveWallet.SolanaSystemInstruction.withdrawNonceAccount.rawValue),
accountParams: [
.init(name: "nonce_account", localizedName: "Nonce Account"),
.init(name: "to_account", localizedName: "To Account"),
.init(name: BraveWallet.NonceAccount, localizedName: "Nonce Account"),
.init(name: BraveWallet.ToAccount, localizedName: "To Account"),
.init(name: "recentblockhashes_sysvar", localizedName: "RecentBlockhashes sysvar"),
.init(name: "rent_sysvar", localizedName: "Rent sysvar"),
.init(name: "nonce_authority", localizedName: "Nonce Authority")
],
params: [
.init(name: "lamports", localizedName: "Lamports", value: "40", type: .uint64)
.init(name: BraveWallet.Lamports, localizedName: "Lamports", value: "40", type: .uint64)
]
)
)
Expand Down Expand Up @@ -961,11 +961,11 @@ class TransactionParserTests: XCTestCase {
decodedData: .init(
instructionType: UInt32(BraveWallet.SolanaSystemInstruction.createAccount.rawValue),
accountParams: [
.init(name: "from_account", localizedName: "From Account"),
.init(name: "new_account", localizedName: "New Account"),
.init(name: BraveWallet.FromAccount, localizedName: "From Account"),
.init(name: BraveWallet.NewAccount, localizedName: "New Account"),
],
params: [
.init(name: "lamports", localizedName: "Lamports", value: "2000", type: .uint64),
.init(name: BraveWallet.Lamports, localizedName: "Lamports", value: "2000", type: .uint64),
.init(name: "space", localizedName: "Space", value: "1", type: .unknown),
.init(name: "owner_program", localizedName: "Owner Program", value: toPubkey, type: .unknown)
]
Expand Down Expand Up @@ -995,11 +995,11 @@ class TransactionParserTests: XCTestCase {
decodedData: .init(
instructionType: UInt32(BraveWallet.SolanaSystemInstruction.createAccountWithSeed.rawValue),
accountParams: [
.init(name: "from_account", localizedName: "From Account"),
.init(name: BraveWallet.FromAccount, localizedName: "From Account"),
.init(name: "created_account", localizedName: "Created Account"),
],
params: [
.init(name: "lamports", localizedName: "Lamports", value: "300", type: .uint64),
.init(name: BraveWallet.Lamports, localizedName: "Lamports", value: "300", type: .uint64),
.init(name: "base", localizedName: "Base", value: toPubkey, type: .unknown),
.init(name: "seed", localizedName: "Seed", value: "", type: .unknown),
.init(name: "space", localizedName: "Space", value: "1", type: .unknown),
Expand Down

0 comments on commit c36995b

Please sign in to comment.