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%
2.2 KB · 78 lines swift
Raw Blame History
1import Foundation23public struct AnyToken2022ExtensionState: BorshCodable, Codable, Equatable, Hashable {4    // MARK: - Hashable56    public func hash(into hasher: inout Hasher) {7        type.hash(into: &hasher)8        state.hash(into: &hasher)9    }1011    // MARK: - Equatable1213    public static func == (lhs: AnyToken2022ExtensionState, rhs: AnyToken2022ExtensionState) -> Bool {14        lhs.type == rhs.type &&15            lhs.state.jsonString == rhs.state.jsonString16    }1718    // MARK: - Properties1920    public let type: Token2022ExtensionType21    public let state: any Token2022ExtensionState2223    // MARK: - Codable2425    enum CodingKeys: String, CodingKey {26        case type27        case state28    }2930    public init(from decoder: Decoder) throws {31        let container = try decoder.container(keyedBy: CodingKeys.self)32        type = try container.decode(Token2022ExtensionType.self, forKey: .type)3334        switch type {35        case .transferFeeConfig:36            state = try container.decode(TransferFeeConfigExtensionState.self, forKey: .state)37        default:38            state = try container.decode(VecU8.self, forKey: .state)39        }40    }4142    public func encode(to encoder: Encoder) throws {43        var container = encoder.container(keyedBy: CodingKeys.self)44        try container.encode(type, forKey: .type)45        try container.encode(state, forKey: .state)46    }4748    // MARK: - BorshCodable4950    public init(from reader: inout BinaryReader) throws {51        guard let type = try Token2022ExtensionType(rawValue: UInt16(from: &reader)) else {52            throw BinaryReaderError.dataMismatch53        }54        self.type = type55        switch type {56        case .transferFeeConfig:57            state = try TransferFeeConfigExtensionState(from: &reader)58        case .interestBearingConfig:59            state = try InterestBearingConfigExtensionState(from: &reader)60        default:61            state = try VecU8(from: &reader)62        }63    }6465    init(66        type: Token2022ExtensionType,67        state: any Token2022ExtensionState68    ) {69        self.type = type70        self.state = state71    }7273    public func serialize(to data: inout Data) throws {74        try type.rawValue.serialize(to: &data)75        try state.serialize(to: &data)76    }77}78