Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfmcnally committed Oct 10, 2023
1 parent 97d7cdf commit 3141a48
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 46 deletions.
6 changes: 6 additions & 0 deletions Sources/Envelope/Base/Assertion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ extension Assertion: Equatable {
}
}

extension Assertion {
public var envelope: Envelope {
Envelope(self)
}
}

/// Support for manipulating assertions.

public extension Envelope {
Expand Down
72 changes: 57 additions & 15 deletions Sources/Envelope/Extensions/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,91 @@ extension EnvelopeError {

public extension Assertion {
/// Creates an attachment assertion. See: [BCR-2023-006](https://github.com/BlockchainCommons/Research/blob/master/papers/bcr-2023-006-envelope-attachment.md)
init(attachment: Envelope, vendor: String, conformsTo: String? = nil) {
init(payload: Envelope, vendor: String, conformsTo: String? = nil) {
self.init(
predicate: KnownValue.attachment,
object: attachment
object: payload
.wrap()
.addAssertion(.vendor, vendor)
.addAssertion(.conformsTo, conformsTo)
)
}

var attachmentPayload: Envelope {
get throws {
try object.unwrap()
}
}

var attachmentVendor: String {
get throws {
try object.extractObject(String.self, forPredicate: .vendor)
}
}

var attachmentConformsTo: String? {
get throws {
try object.extractOptionalObject(String.self, forPredicate: .conformsTo)
}
}

@discardableResult
func validateAttachment() throws -> Self {
let payload = try attachmentPayload
let vendor = try attachmentVendor
let conformsTo = try attachmentConformsTo
let assertion = Assertion(payload: payload, vendor: vendor, conformsTo: conformsTo)
let e = Envelope(assertion)
guard e.isEquivalent(to: self.envelope) else {
throw EnvelopeError.invalidAttachment
}
return self
}
}

public extension Envelope {
func addAttachment(_ attachment: Envelope, vendor: String, conformsTo: String? = nil, salted: Bool = false) -> Envelope {
init(payload: Envelope, vendor: String, conformsTo: String? = nil) {
self.init(Assertion(payload: payload, vendor: vendor, conformsTo: conformsTo))
}

func addAttachment(_ payload: Envelope, vendor: String, conformsTo: String? = nil, salted: Bool = false) -> Envelope {
addAssertion(
Assertion(attachment: attachment, vendor: vendor, conformsTo: conformsTo)
Assertion(payload: payload, vendor: vendor, conformsTo: conformsTo)
)
}
}

public extension Envelope {
var attachmentPayload: Envelope {
get throws {
try object.unwrap()
guard case .assertion(let assertion) = self else {
throw EnvelopeError.invalidAttachment
}
return try assertion.attachmentPayload
}
}

var attachmentVendor: String {
get throws {
try object.extractObject(String.self, forPredicate: .vendor)
guard case .assertion(let assertion) = self else {
throw EnvelopeError.invalidAttachment
}
return try assertion.attachmentVendor
}
}

var attachmentConformsTo: String? {
get throws {
try object.extractOptionalObject(String.self, forPredicate: .conformsTo)
guard case .assertion(let assertion) = self else {
throw EnvelopeError.invalidAttachment
}
return try assertion.attachmentConformsTo
}
}

func attachments(withVendor vendor: String? = nil, conformingTo conformsTo: String? = nil) throws -> [Envelope] {
try assertions(withPredicate: .attachment).filter { envelope in
try validateAttachment(envelope)
try envelope.validateAttachment()
if let vendor {
guard try envelope.attachmentVendor == vendor else {
return false
Expand All @@ -63,15 +107,13 @@ public extension Envelope {
}
}

func validateAttachment(_ envelope: Envelope) throws {
let payload = try envelope.attachmentPayload
let vendor = try envelope.attachmentVendor
let conformsTo = try envelope.attachmentConformsTo
let assertion = Assertion(attachment: payload, vendor: vendor, conformsTo: conformsTo)
let e = Envelope(assertion)
guard e.isEquivalent(to: envelope) else {
@discardableResult
func validateAttachment() throws -> Self {
guard case .assertion(let assertion) = self else {
throw EnvelopeError.invalidAttachment
}
try assertion.validateAttachment()
return self
}

func attachment(withVendor vendor: String? = nil, conformingTo conformsTo: String? = nil) throws -> Envelope {
Expand Down
2 changes: 1 addition & 1 deletion Tests/EnvelopeTests/CompressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import WolfBase
class CompressionTests: XCTestCase {
let source = "Lorem ipsum dolor sit amet consectetur adipiscing elit mi nibh ornare proin blandit diam ridiculus, faucibus mus dui eu vehicula nam donec dictumst sed vivamus bibendum aliquet efficitur. Felis imperdiet sodales dictum morbi vivamus augue dis duis aliquet velit ullamcorper porttitor, lobortis dapibus hac purus aliquam natoque iaculis blandit montes nunc pretium."
func testCompress() throws {
print(source)
// print(source)
let original = Envelope(source)
XCTAssertEqual(original.cborData.count, 369)
let compressed = try original.compress().checkEncoding(tags: globalTags)
Expand Down
10 changes: 5 additions & 5 deletions Tests/EnvelopeTests/Core/TypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class TypeTests: XCTestCase {
func testDate() throws {
let envelope = try Envelope(Date(iso8601: "2018-01-07")).checkEncoding()
XCTAssertEqual(envelope.format(), "2018-01-07")
print(envelope.diagnostic(annotate: true, context: globalFormatContext))
print(envelope.diagnostic())
// print(envelope.diagnostic(annotate: true, context: globalFormatContext))
// print(envelope.diagnostic())

let e = Envelope(Date(timeIntervalSince1970: 1693454262.5))//.checkEncoding()
print(e.format(context: globalFormatContext))
print(e.diagnostic(annotate: true, context: globalFormatContext))
print(e.diagnostic())
// print(e.format(context: globalFormatContext))
// print(e.diagnostic(annotate: true, context: globalFormatContext))
// print(e.diagnostic())
}
}
14 changes: 10 additions & 4 deletions Tests/EnvelopeTests/CryptoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,14 @@ class CryptoTests: XCTestCase {
// representing SSKR groups and the inner array elements each holding the encrypted
// seed and a single share.
let contentKey = SymmetricKey()
let seedEnvelope = Envelope(danSeed)
let seedEnvelope = danSeed.envelope
// print(seedEnvelope.format(context: globalFormatContext))

let encryptedSeedEnvelope = try seedEnvelope
.wrap()
.encryptSubject(with: contentKey)

// print(encryptedSeedEnvelope.format(context: globalFormatContext))

let envelopes = encryptedSeedEnvelope
.split(groupThreshold: 1, groups: [(2, 3)], contentKey: contentKey)
Expand Down Expand Up @@ -461,9 +466,10 @@ class CryptoTests: XCTestCase {

// At some future point, Dan retrieves two of the three envelopes so he can recover his seed.
let recoveredEnvelopes = [bobEnvelope, carolEnvelope]
let a = try Envelope(shares: recoveredEnvelopes)
let recoveredSeed = try a
.extractSubject(Seed.self)
let recoveredSeedEnvelope = try Envelope(shares: recoveredEnvelopes).unwrap()
// print(recoveredSeedEnvelope.format(context: globalFormatContext))

let recoveredSeed = try Seed(recoveredSeedEnvelope)

// The recovered seed is correct.
XCTAssertEqual(danSeed.data, recoveredSeed.data)
Expand Down
10 changes: 5 additions & 5 deletions Tests/EnvelopeTests/ElisionExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ class ElisionExampleTests: XCTestCase {
)

let edits = credential.diff(target: warranty)
print(credential.cborData.count)
print(warranty.cborData.count)
print(edits.cborData.count)
print(edits.format())
// print(credential.cborData.count)
// print(warranty.cborData.count)
// print(edits.cborData.count)
// print(edits.format())
XCTAssert(try credential.transform(edits: edits).isIdentical(to: warranty))
}

Expand Down Expand Up @@ -316,7 +316,7 @@ class ElisionExampleTests: XCTestCase {
]
"""
)
print($0.mermaidFormat())
// print($0.mermaidFormat())
}

/// The only actual assertions we want to reveal are `birthDate` and `photo`, so we do this by finding those specific assertions by their predicate. The `shallowDigests` attribute returns just a necessary set of attributes to reveal the assertion, its predicate, and its object (yes, all three of them need to be revealed) but *not* any deeper assertions on them.
Expand Down
2 changes: 1 addition & 1 deletion Tests/EnvelopeTests/FormatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2474,6 +2474,6 @@ class FormatTests: XCTestCase {
linkStyle 44 stroke-width:2.0px
linkStyle 45 stroke-width:2.0px
"""#)
print(warranty.hex(annotate: true, context: globalFormatContext))
//print(warranty.hex(annotate: true, context: globalFormatContext))
}
}
16 changes: 8 additions & 8 deletions Tests/EnvelopeTests/GraphTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GraphTests: XCTestCase {
.addAssertion("sameAs", "http://www.twitter.com/")
.addAssertion("sameAs", "http://www.instagram.com/")
.addAssertion("sameAs", "http://plus.google.com/")
let john = Envelope(ARID())
let _/*john*/ = Envelope(ARID())
.addAssertion("@context", "https://schema.org")
.addAssertion("address", address)
.addAssertion("colleague", "http://www.example.com/JohnColleague.html")
Expand All @@ -36,8 +36,8 @@ class GraphTests: XCTestCase {
.addAssertion("nationality", "Albanian")
.addAssertion("telephone", "(123) 456-6789")
.addAssertion("url", url)
print(john.format)
print(john.mermaidFormat())
// print(john.format)
// print(john.mermaidFormat())
}
// https://neo4j.com/press-releases/neo4j-financial-services-momentum/
func testNeo4j() throws {
Expand All @@ -60,15 +60,15 @@ class GraphTests: XCTestCase {
.addAssertion("ARITY", "*")
let postedBy = Envelope("POSTED_BY")
.addAssertion("ARITY", "1")
let tweet = Envelope("Tweet")
let _/*tweet*/ = Envelope("Tweet")
.addAssertion(mentions, user)
.addAssertion(retweets, "Tweet")
.addAssertion(using, "Source")
.addAssertion(contains, "Link")
.addAssertion(tags, "Hashtag")
.addAssertion(postedBy, "User")
print(tweet.format)
print(tweet.mermaidFormat())
// print(tweet.format)
// print(tweet.mermaidFormat())
}

// https://docs.stardog.com/assets/images/tutorials/learn-sparql/rdf-beatles.png
Expand Down Expand Up @@ -98,12 +98,12 @@ class GraphTests: XCTestCase {
.addAssertion("length", 125)
.addAssertion(":writer", ":Paul_McCartney")
.addAssertion(":writer", ":John_Lennon")
let pleasePleaseMe = Envelope(":Please_Please_Me")
let _/*pleasePleaseMe*/ = Envelope(":Please_Please_Me")
.addType(":Album")
.addAssertion(.hasName, "Please Please Me")
.addAssertion(.date, try Date(iso8601: "1963-03-22"))
.addAssertion(":artist", theBeatles)
.addAssertion(":track", loveMeDo)
print(pleasePleaseMe.format)
// print(pleasePleaseMe.format)
}
}
14 changes: 7 additions & 7 deletions Tests/EnvelopeTests/ScenarioTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ScenarioTests: XCTestCase {
]
]
"""
print(aliceSignedDocument.format())
// print(aliceSignedDocument.format())
XCTAssertEqual(aliceSignedDocument.format(), expectedFormat)

// Signatures have a random component, so anything with a signature will have a
Expand Down Expand Up @@ -720,7 +720,7 @@ class ScenarioTests: XCTestCase {
func testExampleCredential() {
let omarCID = ARID()
let omarPrivateKey = PrivateKeyBase()
let omar = Envelope(omarCID)
let _/*omar*/ = Envelope(omarCID)
.addAssertion(.hasName, "Omar Chaim")
.addAssertion("githubID", "omarc-bc-guy")
.addAssertion("pubkeyURL", "https://github.com/omarc-bc-guy.keys")
Expand All @@ -731,23 +731,23 @@ class ScenarioTests: XCTestCase {
let jonathanPrivateKey = PrivateKeyBase()
let jonathanPublicKey = jonathanPrivateKey.publicKeys
let ur = jonathanPublicKey.ur
let jonathan = Envelope(jonathanCID)
let _/*jonathan*/ = Envelope(jonathanCID)
.addAssertion(.hasName, "Jonathan Jakes")
.addAssertion("githubID", "jojokes")
.addAssertion("pubkey", ur.string)
.wrap()
.sign(with: jonathanPrivateKey, note: "Self-signed by Jonathan")

let certCID = ARID()
let cert = Envelope(certCID)
let _/*cert*/ = Envelope(certCID)
.addAssertion(.issuer, Envelope(omarCID).addAssertion(.note, "Omar's ARID"))
.addAssertion("subject", Envelope(jonathanCID).addAssertion(.note, "Jonathan's ARID"))
.addType("Assessment of Blockchain Tech Writing Expertise")
.wrap()
.sign(with: omarPrivateKey, note: "Signed by Omar")

print(omar.format())
print(jonathan.format())
print(cert.format())
// print(omar.format())
// print(jonathan.format())
// print(cert.format())
}
}

0 comments on commit 3141a48

Please sign in to comment.