Unverified Commit 41ef1040 authored by Maciej Hirsz's avatar Maciej Hirsz Committed by GitHub
Browse files

feat: Convert rawData to Uint8Array (#294)



* feat: Convert rawData to Uint8Array

* fix: Allow for the lone `ec` byte in SQRC padding

* chore: Example QR code payload

Co-Authored-By: default avatarYJ <yjkimjunior@gmail.com>

* chore: Clarify comment
parent 9ef742b3
Pipeline #45165 failed with stage
in 17 seconds
......@@ -43,6 +43,11 @@ export default class Scanner extends React.PureComponent {
if (scannerStore.isBusy()) {
return;
}
// TODO: Actually use this to read UOS
// type is Uint8Array or null
const bytes = rawDataToU8A(txRequestData.rawData);
try {
const data = JSON.parse(txRequestData.data);
if (data.action === undefined) {
......@@ -207,3 +212,67 @@ const styles = StyleSheet.create({
paddingBottom: 20,
}
});
/*
Example Full Raw Data
---
4 // indicates binary
37 // indicates data length
00 // indicates multipart
0001 // frame count
0000 // first frame
--- UOS Specific Data
53 // indicates payload is for Substrate
01 // crypto: sr25519
00 // indicates action: signData
f4cd755672a8f9542ca9da4fbf2182e79135d94304002e6a09ffc96fef6e6c4c // public key
544849532049532053504152544121 // actual payload message to sign (should be SCALE)
0 // terminator
--- SQRC Filler Bytes
ec11ec11ec11ec // SQRC filler bytes
*/
function rawDataToU8A(rawData) {
if (!rawData) {
return null;
}
// Strip filler bytes padding at the end
if (rawData.substr(-2) === 'ec') {
rawData = rawData.substr(0, rawData.length - 2);
}
while (rawData.substr(-4) === 'ec11') {
rawData = rawData.substr(0, rawData.length - 4);
}
// Verify that the QR encoding is binary and it's ending with a proper terminator
if (rawData.substr(0, 1) !== '4' || rawData.substr(-1) !== '0') {
return null;
}
// Strip the encoding indicator and terminator for ease of reading
rawData = rawData.substr(1, rawData.length - 2);
const length8 = parseInt(rawData.substr(0, 2), 16) || 0;
const length16 = parseInt(rawData.substr(0, 4), 16) || 0;
let length = 0;
// Strip length prefix
if (length8 * 2 + 2 === rawData.length) {
rawData = rawData.substr(2);
length = length8;
} else if (length16 * 2 + 4 === rawData.length) {
rawData = rawData.substr(4);
length = length16;
} else {
return null;
}
const bytes = new Uint8Array(length);
for (let i = 0; i < length; i++) {
bytes[i] = parseInt(rawData.substr(i * 2, 2), 16);
}
return bytes;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment