SPB Git

spb/os-vault Public

Self-custody, multi-chain crypto wallet for macOS. One recovery phrase, six chain families, zero API keys — nothing leaves your Mac.

Swift 96% Shell 3.4% Makefile 0.6%
1.6 KB · 55 lines swift
Raw Blame History
1import Foundation23// TODO: follow code from solana!4public struct CompiledInstruction: Equatable {5    public let programIdIndex: UInt86    let keyIndicesCount: [UInt8]7    let keyIndices: [UInt8]8    let dataLength: [UInt8]9    public let data: [UInt8]1011    public var accounts: [Int] {12        keyIndices.map { x in Int(x) }13    }1415    var programIdIndexValue: Int {16        Int(programIdIndex)17    }1819    var serializedData: Data {20        Data([programIdIndex]21            + keyIndicesCount22            + keyIndices23            + dataLength24            + data)25    }26}2728extension Sequence where Iterator.Element == TransactionInstruction {29    func compile(accountKeys: [PublicKey]) -> [CompiledInstruction] {30        var compiledInstructions = [CompiledInstruction]()3132        for instruction in self {33            let keysSize = instruction.keys.count3435            var keyIndices = Data()36            for i in 0 ..< keysSize {37                let index = accountKeys.firstIndex(of: instruction.keys[i].publicKey)!38                keyIndices.append(UInt8(index))39            }4041            let compiledInstruction = CompiledInstruction(42                programIdIndex: UInt8(accountKeys.firstIndex(of: instruction.programId)!),43                keyIndicesCount: [UInt8](Data.encodeLength(keysSize)),44                keyIndices: [UInt8](keyIndices),45                dataLength: [UInt8](Data.encodeLength(instruction.data.count)),46                data: instruction.data47            )4849            compiledInstructions.append(compiledInstruction)50        }5152        return compiledInstructions53    }54}55