|
| 1 | +# Iden3Message Parser |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +With the latest update, the content of the QR code provided by the Issuer or Verifier could be in three different formats: |
| 6 | +- Url (iden3comm://?request_uri=) |
| 7 | +- Base64 encoded (iden3comm://?i_m=) |
| 8 | +- RAW Json format (json) |
| 9 | + |
| 10 | +## Code Snippet |
| 11 | +to parse the qr code content, you can use the following code snippet as an example: |
| 12 | + |
| 13 | +```dart |
| 14 | +class QrcodeParserUtils { |
| 15 | + final PolygonIdSdk _polygonIdSdk; |
| 16 | +
|
| 17 | + QrcodeParserUtils(this._polygonIdSdk); |
| 18 | +
|
| 19 | + Future<Iden3MessageEntity> getIden3MessageFromQrCode(String message) async { |
| 20 | + try { |
| 21 | + String rawMessage = message; |
| 22 | + if (message.startsWith("iden3comm://?i_m")) { |
| 23 | + rawMessage = await _getMessageFromBase64(message); |
| 24 | + } |
| 25 | +
|
| 26 | + if (message.startsWith("iden3comm://?request_uri")) { |
| 27 | + rawMessage = await _getMessageFromRemote(message); |
| 28 | + } |
| 29 | +
|
| 30 | + Iden3MessageEntity? _iden3Message = |
| 31 | + await _polygonIdSdk.iden3comm.getIden3Message(message: rawMessage); |
| 32 | + return _iden3Message; |
| 33 | + } catch (error) { |
| 34 | + throw Exception("Error while processing the QR code"); |
| 35 | + } |
| 36 | + } |
| 37 | +
|
| 38 | + Future<String> _getMessageFromRemote(String message) async { |
| 39 | + try { |
| 40 | + message = message.replaceAll("iden3comm://?request_uri=", ""); |
| 41 | + http.Response response = await http.get(Uri.parse(message)); |
| 42 | + if (response.statusCode != 200) { |
| 43 | + throw Exception("Error while getting the message from the remote"); |
| 44 | + } |
| 45 | + return response.body; |
| 46 | + } catch (error) { |
| 47 | + throw Exception("Error while getting the message from the remote"); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + Future<String> _getMessageFromBase64(String message) async { |
| 52 | + try { |
| 53 | + message = message.replaceAll("iden3comm://?i_m=", ""); |
| 54 | + String decodedMessage = String.fromCharCodes(base64.decode(message)); |
| 55 | + return decodedMessage; |
| 56 | + } catch (error) { |
| 57 | + throw Exception("Error while getting the message from the base64"); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
0 commit comments