Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ABW-1268] CAP33 parse imported payloads #438

Merged
merged 27 commits into from
Apr 11, 2023
Merged

Conversation

CyonAlexRDX
Copy link
Contributor

@CyonAlexRDX CyonAlexRDX commented Apr 7, 2023

Jira ticket: ABW-1286

Description

This is an implementation of serialization and deserialization of CAP-33 the import string format between Olympia and Babylon wallets.

N.B. that Babylon clients only need to deserialize (parse), but I've implemented serialize so that I could generate test vectors for our consultants who maintain the Olympia wallet can use to verify their serialize implementation, as well as our Android client can use to verify its implementation of deserialize.

@CyonAlexRDX CyonAlexRDX changed the title Abw 1268 legacy parse qr [ABW-1268] CAP33 parse imported payloads Apr 10, 2023
@CyonAlexRDX CyonAlexRDX marked this pull request as ready for review April 10, 2023 14:59
@CyonAlexRDX CyonAlexRDX marked this pull request as draft April 11, 2023 09:45
@CyonAlexRDX CyonAlexRDX marked this pull request as ready for review April 11, 2023 11:21
}

// MARK: Parse Account
private static func _deserializeAccount(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function feels like it should almost be using a RegexBuilder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did start impl using PF SwiftParsing, but they have rewritten it for Swift 5.8 and not worth having to change the impl once we are in Swift 5.8....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, but RegexBuilders are forever. And very easy to use.

Comment on lines 227 to 232
var truncated = String(name.rawValue.prefix(30))
let forbiddenCharacters: [String] = Olympia.Export.Separator.allCases
for forbiddenChar in forbiddenCharacters {
truncated = truncated.replacingOccurrences(of: forbiddenChar, with: Olympia.Export.accountNameForbiddenCharReplacement)
}
return truncated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're replacing all forbidden characters with the same thing, I think it is clearer to do it in one go, something like

	public static func _sanitize(name: NonEmptyString?) -> String {
		guard let name else { return "" }
		let result = name.rawValue.prefix(30).replacing(
			Olympia.Export.Separator.regex,
			with: Olympia.Export.accountNameForbiddenCharReplacement
		)
		return String(result)
	}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where

	static public var regex: some RegexComponent {
		ChoiceOf {
			inter
			intra
			headerEnd
			accountNameEnd
		}
	}

Or some more compact version

try Self.deserialize(
payloads: .init(rawValue: OrderedSet(
uncheckedUniqueElements: payloadsStrings.compactMap { NonEmptyString(rawValue: $0) }
))!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This scares me, could we replace it with a guard-throw, since the function is throwing?

Copy link
Contributor

@kugel3 kugel3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool. I haven't tested it, I suppose I can't without an olympia account, or can I make one now? Just some small stylistic comments.

@CyonAlexRDX CyonAlexRDX merged commit f335c43 into main Apr 11, 2023
@CyonAlexRDX CyonAlexRDX deleted the ABW-1268_legacy_parse_qr branch April 11, 2023 19:23
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants