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%
3.5 KB · 104 lines swift
Raw Blame History
1import Foundation23struct LookupTableMeta: BufferLayout {4    let typeIndex: UInt325    let deactivationSlot: UInt646    let lastExtendedSlot: UInt647    let lastExtendedStartIndex: UInt88    let authority: [PublicKey]910    public func serialize(to _: inout Data) throws {11        fatalError()12    }1314    public init(from reader: inout BinaryReader) throws {15        typeIndex = try .init(from: &reader)16        deactivationSlot = try .init(from: &reader)17        lastExtendedSlot = try .init(from: &reader)18        lastExtendedStartIndex = try .init(from: &reader)19        _ = try UInt8(from: &reader)20        authority = [try PublicKey(from: &reader)]21    }22}2324public struct AddressLookupTableState: Equatable, Codable, BufferLayout {25    let typeIndex: UInt3226    let deactivationSlot: UInt6427    let lastExtendedSlot: UInt6428    let lastExtendedSlotStartIndex: UInt829    let authority: PublicKey?30    let addresses: [PublicKey]3132    public func serialize(to _: inout Data) throws {33        fatalError()34    }3536    public init(from reader: inout BinaryReader) throws {37        var readerForAddresses = reader3839        typeIndex = try .init(from: &reader)40        deactivationSlot = try .init(from: &reader)41        lastExtendedSlot = try .init(from: &reader)42        lastExtendedSlotStartIndex = try .init(from: &reader)43        _ = try UInt8(from: &reader)44        authority = try PublicKey(bytes: reader.read(count: PublicKey.numberOfBytes))4546        try readerForAddresses.read(count: AddressLookupTableAccount.lookUpTableMetaSize)47        var addresses: [PublicKey] = []48        while readerForAddresses.remainBytes > PublicKey.numberOfBytes {49            try addresses.append(PublicKey(bytes: readerForAddresses.read(count: PublicKey.numberOfBytes)))50        }5152        self.addresses = addresses53    }54}5556public struct AddressLookupTableAccount: Equatable {57    public static let lookUpTableMetaSize = 565859    public let key: PublicKey60    public let state: AddressLookupTableState6162    public init(key: PublicKey, state: AddressLookupTableState) {63        self.key = key64        self.state = state65    }6667    var isActive: Bool {68        return state.deactivationSlot == UInt64.max69    }7071//    // TODO: implement?72//    public func serialize(to writer: inout Data) throws {73//        fatalError()74//    }75//76//    // TODO: Improve77//    public init(from reader: inout BinaryReader) throws {78//        var accountDataReader = reader79//        let meta = try LookupTableMeta(from: &accountDataReader)80//81//        // TODO: Transalte JS code to Swift82//        // const serializedAddressesLen = accountData.length - LOOKUP_TABLE_META_SIZE;83//        // assert(serializedAddressesLen >= 0, 'lookup table is invalid');84//        // assert(serializedAddressesLen % 32 === 0, 'lookup table is invalid');85//86//        let serializedAddressesLen = reader.remainBytes - Self.lookUpTableMetaSize87//        let numSerializedAddresses = serializedAddressesLen / 3288//89//        var addresses: [PublicKey] = []90//        try reader.read(count: Self.lookUpTableMetaSize)91//        for _ in 0 ..< numSerializedAddresses {92//            addresses.append(try PublicKey(bytes: try reader.read(count: PublicKey.numberOfBytes)))93//        }94//95//        return .init(96//            deactivationSlot: meta.deactivationSlot,97//            lastExtendedSlot: meta.lastExtendedSlot,98//            lastExtendedSlotStartIndex: meta.lastExtendedStartIndex,99//            authority: meta.authority.first,100//            addresses: addresses101//        )102//    }103}104