diff --git a/Sources/MessageUtils/MessageUtils.swift b/Sources/MessageUtils/MessageUtils.swift index f42178f..4700ad9 100644 --- a/Sources/MessageUtils/MessageUtils.swift +++ b/Sources/MessageUtils/MessageUtils.swift @@ -64,6 +64,7 @@ public func serializeV1(msg: MessageP) -> Data { return serializedData } + public func signMessage(msgData: Data, signType: SignType, key: P256.Signing.PrivateKey) throws -> [UInt8] { diff --git a/Sources/MessageUtils/Structs/Field.swift b/Sources/MessageUtils/Structs/Field.swift index 2c6a4e6..fecfbb8 100644 --- a/Sources/MessageUtils/Structs/Field.swift +++ b/Sources/MessageUtils/Structs/Field.swift @@ -5,4 +5,12 @@ public struct Field { public let key: [UInt8] public let value: [UInt8] + public init( + key: [UInt8], + value: [UInt8] + ) { + self.key = key + self.value = value + } + } diff --git a/Sources/MessageUtils/Structs/Location.swift b/Sources/MessageUtils/Structs/Location.swift index 22a2fe3..7946e9c 100644 --- a/Sources/MessageUtils/Structs/Location.swift +++ b/Sources/MessageUtils/Structs/Location.swift @@ -3,5 +3,14 @@ public struct Location { public let x: UInt64 public let y: UInt64 public let z: UInt64 - -} \ No newline at end of file + + public init( + x: UInt64, + y: UInt64, + z: UInt64 + ) { + self.x = x + self.y = y + self.z = z + } +} diff --git a/Sources/MessageUtils/Structs/Message.swift b/Sources/MessageUtils/Structs/Message.swift index 492e51a..e000d9c 100644 --- a/Sources/MessageUtils/Structs/Message.swift +++ b/Sources/MessageUtils/Structs/Message.swift @@ -1,6 +1,6 @@ import Foundation -public struct Message : MessageP{ +public struct Message: MessageP { public let version: UInt8 public let messageType: MessageType @@ -8,13 +8,37 @@ public struct Message : MessageP{ public let RESERVED: UInt8 public let signType: SignType - public let timestamp : Date - public let devID : UInt128 + public let timestamp: Date + public let devID: UInt128 public let location: Location public let fields: [Field] + public init( - public func toString() -> String{ + version: UInt8, + messageType: MessageType, + devType: DeviceType, + RESERVED: UInt8, + signType: SignType, + timestamp: Date, + devID: UInt128, + location: Location, + fields: [Field] + ) { + + self.version = version + self.messageType = messageType + self.devType = devType + self.RESERVED = RESERVED + self.signType = signType + self.timestamp = timestamp + self.devID = devID + self.location = location + self.fields = fields + + } + + public func toString() -> String { var description = "" description += "MESSAGE -------\n" description += "V: \t\(self.version)\n" @@ -24,17 +48,18 @@ public struct Message : MessageP{ description += "Signature Type: \t\(self.signType.rawValue)\n" description += "Timestamp: \t\(self.timestamp)\n" description += "Device ID: \t\(self.devID)\n" - description += "Location: \tX: \(self.location.x)\tY: \(self.location.y)\tZ: \(self.location.z)\n" + description += + "Location: \tX: \(self.location.x)\tY: \(self.location.y)\tZ: \(self.location.z)\n" description += "Fields: \n" for field in self.fields { - description += "\t\(String(data: Data(field.key), encoding: .ascii) ?? "UNABLE TO DECODE"): \(String(data: Data(field.value), encoding: .ascii) ?? "UNABLE TO DECODE")\n" + description += + "\t\(String(data: Data(field.key), encoding: .ascii) ?? "UNABLE TO DECODE"): \(String(data: Data(field.value), encoding: .ascii) ?? "UNABLE TO DECODE")\n" } return description } } - -public struct SignedMessage : MessageP { +public struct SignedMessage: MessageP { public let version: UInt8 public let messageType: MessageType @@ -42,14 +67,13 @@ public struct SignedMessage : MessageP { public let RESERVED: UInt8 public let signType: SignType - public let timestamp : Date - public let devID : UInt128 + public let timestamp: Date + public let devID: UInt128 public let location: Location public let fields: [Field] public let signature: [UInt8] - - public func toString() -> String{ + public func toString() -> String { var description = "" description += "MESSAGE -------\n" description += "V: \t\(self.version)\n" @@ -59,13 +83,15 @@ public struct SignedMessage : MessageP { description += "Signature Type: \t\(self.signType.rawValue)\n" description += "Timestamp: \t\(self.timestamp)\n" description += "Device ID: \t\(self.devID)\n" - description += "Location: \tX: \(self.location.x)\tY: \(self.location.y)\tZ: \(self.location.z)\n" + description += + "Location: \tX: \(self.location.x)\tY: \(self.location.y)\tZ: \(self.location.z)\n" description += "Fields: \n" for field in self.fields { - description += "\t\(String(data: Data(field.key), encoding: .ascii) ?? "UNABLE TO DECODE"): \(String(data: Data(field.value), encoding: .ascii) ?? "UNABLE TO DECODE")\n" + description += + "\t\(String(data: Data(field.key), encoding: .ascii) ?? "UNABLE TO DECODE"): \(String(data: Data(field.value), encoding: .ascii) ?? "UNABLE TO DECODE")\n" } description += "Signature: \t\(self.signature)\n" return description } -} \ No newline at end of file +}