-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrap bytes if not wrapped before signing in DApp browser
- Loading branch information
Showing
4 changed files
with
58 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
novawallet/Modules/DApp/DAppOperationConfirm/Model/DAppMessageSignFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Foundation | ||
import SubstrateSdk | ||
|
||
protocol DAppMessageSignFactoryProtocol { | ||
func serializeSigningMessage(from message: JSON) throws -> Data | ||
} | ||
|
||
final class PolkadotExtensionMessageSignFactory: DAppMessageSignFactoryProtocol { | ||
private func serializeMessage(from message: JSON) throws -> Data { | ||
if case let .stringValue(stringValue) = message { | ||
if stringValue.isHex() { | ||
return try Data(hexString: stringValue) | ||
} else { | ||
guard let data = stringValue.data(using: .utf8) else { | ||
throw CommonError.dataCorruption | ||
} | ||
|
||
return data | ||
} | ||
|
||
} else { | ||
throw CommonError.dataCorruption | ||
} | ||
} | ||
|
||
private func wrappingMessageIfNeeded(_ message: Data) throws -> Data { | ||
let prefix = "<Bytes>" | ||
let suffix = "</Bytes>" | ||
|
||
guard let suffixData = suffix.data(using: .ascii), let prefixData = prefix.data(using: .ascii) else { | ||
throw CommonError.dataCorruption | ||
} | ||
|
||
if | ||
message.prefix(prefixData.count) == prefixData, | ||
message.suffix(suffixData.count) == suffixData { | ||
return message | ||
} | ||
|
||
return prefixData + message + suffixData | ||
} | ||
|
||
func serializeSigningMessage(from message: JSON) throws -> Data { | ||
let serializedMessage = try serializeMessage(from: message) | ||
let wrappedMessage = try wrappingMessageIfNeeded(serializedMessage) | ||
|
||
return wrappedMessage | ||
} | ||
} |