Skip u8a -> string -> number[] for qrcode generation (#170)

* Skip u8a -> string -> number[] for qrcode generation

* Fix tests for Uint8Array conversion

* Bumps
This commit is contained in:
Jaco Greeff
2019-07-30 17:26:56 +02:00
committed by GitHub
parent 39a5147fb8
commit 6f5b3c9ccc
8 changed files with 58 additions and 38 deletions
+2 -2
View File
@@ -24,8 +24,8 @@
"react": "*"
},
"devDependencies": {
"@polkadot/keyring": "^0.95.0-beta.2",
"@polkadot/util-crypto": "^0.95.0-beta.2",
"@polkadot/keyring": "^0.95.0-beta.3",
"@polkadot/util-crypto": "^0.95.0-beta.3",
"xmlserializer": "^0.6.1"
}
}
+13 -6
View File
@@ -5,11 +5,11 @@
import { BaseProps } from './types';
import React from 'react';
import qrcode from 'qrcode-generator';
import styled from 'styled-components';
import { xxhashAsHex } from '@polkadot/util-crypto';
import { createFrames, createImgSize, decodeString } from './util';
import qrcode from './qrcode';
import { createFrames, createImgSize } from './util';
interface Props extends BaseProps {
size?: number;
@@ -18,7 +18,7 @@ interface Props extends BaseProps {
}
interface State {
frames: string[];
frames: Uint8Array[];
frameIdx: number;
image: string | null;
timerId: number | null;
@@ -27,9 +27,12 @@ interface State {
const FRAME_DELAY = 2100;
function getDataUrl (value: string): string {
function getDataUrl (value: Uint8Array): string {
const qr = qrcode(0, 'M');
// HACK See out qrcode stringToBytes override as used internally. This
// will only work for the case where we actuall pass `Bytes` in here
// @ts-ignore
qr.addData(value, 'Byte');
qr.make();
@@ -52,10 +55,11 @@ class Display extends React.PureComponent<Props, State> {
return null;
}
const frames: string[] = skipEncoding
? [decodeString(value)]
const frames: Uint8Array[] = skipEncoding
? [value]
: createFrames(value);
// encode on demand
return {
frames,
frameIdx: 0,
@@ -112,6 +116,9 @@ class Display extends React.PureComponent<Props, State> {
? 0
: frameIdx + 1;
// only encode the frames on demand, not above as part of the
// state derivation - in the case of large payloads, this should
// be slightly more responsive on initial load
this.setState({
frameIdx: nextIdx,
image: getDataUrl(frames[nextIdx])
+13
View File
@@ -0,0 +1,13 @@
// Copyright 2017-2019 @polkadot/react-qr authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import qrcode from 'qrcode-generator';
// HACK The default function take string -> number[], the Uint8array is compatible
// with that signature and the use thereof
// @ts-ignore
qrcode.stringToBytes = (data: Uint8Array): Uint8Array =>
data;
export default qrcode;
+1 -1
View File
@@ -78,7 +78,7 @@ describe('util', (): void => {
expect(
createFrames(
createSignPayload('5HbgaJEuVN5qGbkhgtuDQANivSWwHXWsC2erP1SQUXgciTVq', '0x12345678')
).map((str): string => u8aToHex(encodeString(str)))
).map((u8a): string => u8aToHex(u8a))
).toEqual([
'0x' +
'00' + // multipart
+4 -4
View File
@@ -46,7 +46,7 @@ export function createSignPayload (address: string, payload: string | Uint8Array
);
}
export function createFrames (input: Uint8Array): string[] {
export function createFrames (input: Uint8Array): Uint8Array[] {
const frames = [];
let idx = 0;
@@ -56,13 +56,13 @@ export function createFrames (input: Uint8Array): string[] {
idx += FRAME_SIZE;
}
return frames.map((frame, index: number): string =>
decodeString(u8aConcat(
return frames.map((frame, index: number): Uint8Array =>
u8aConcat(
MULTIPART,
encodeNumber(frames.length),
encodeNumber(index),
frame
))
)
);
}
+3 -3
View File
@@ -19,9 +19,9 @@
"styled-components": "^4.3.1"
},
"devDependencies": {
"@polkadot/keyring": "^0.95.0-beta.2",
"@polkadot/types": "^0.90.0-beta.1",
"@polkadot/util": "^0.95.0-beta.2"
"@polkadot/keyring": "^0.95.0-beta.3",
"@polkadot/types": "^0.90.0-beta.8",
"@polkadot/util": "^0.95.0-beta.3"
},
"peerDependencies": {
"@polkadot/keyring": "*",
+1 -1
View File
@@ -14,7 +14,7 @@
"store": "^2.0.12"
},
"devDependencies": {
"@polkadot/util": "^0.95.0-beta.2"
"@polkadot/util": "^0.95.0-beta.3"
},
"peerDependencies": {
"@polkadot/util": "*"
+21 -21
View File
@@ -1948,14 +1948,14 @@
typescript "^3.5.3"
vuepress "^1.0.2"
"@polkadot/keyring@^0.95.0-beta.2":
version "0.95.0-beta.2"
resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.95.0-beta.2.tgz#fc3dde2bb45fabb0ee992c060c7c692ee27436da"
integrity sha512-EeDIE4XeWGCviRwLBVJ1EvQvwzNvDh9PZrk0Ed0TRERTg+x3tC8s7pkSqKXzElmQqliXMJmpkTKcH/YEnyTN0Q==
"@polkadot/keyring@^0.95.0-beta.3":
version "0.95.0-beta.3"
resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.95.0-beta.3.tgz#fef0032284986d93879d7933220ca74dcba80afd"
integrity sha512-5Uj063fVrF3AlM2VDFOVxnNTyibzI8FOZhgsWNfihheypo1kkXdcMQgaHRtCXQlftITr8Siod8RdI5Yj4BOBow==
dependencies:
"@babel/runtime" "^7.5.5"
"@polkadot/util" "^0.95.0-beta.2"
"@polkadot/util-crypto" "^0.95.0-beta.2"
"@polkadot/util" "^0.95.0-beta.3"
"@polkadot/util-crypto" "^0.95.0-beta.3"
"@polkadot/ts@^0.1.62":
version "0.1.62"
@@ -1964,24 +1964,24 @@
dependencies:
"@types/chrome" "^0.0.86"
"@polkadot/types@^0.90.0-beta.1":
version "0.90.0-beta.1"
resolved "https://registry.yarnpkg.com/@polkadot/types/-/types-0.90.0-beta.1.tgz#7d70e07c34e04d6be4ef9aaee706df29d05f9576"
integrity sha512-0XipN9z21KXgg79HyCCF0APOPvx4MhD0DkGN4zUZJi/ZaGNH/aEcjMDqnWNZZyXVSb80srQNPa/HosDR8qxmKA==
"@polkadot/types@^0.90.0-beta.8":
version "0.90.0-beta.8"
resolved "https://registry.yarnpkg.com/@polkadot/types/-/types-0.90.0-beta.8.tgz#18388d9a2d23d1264917251075475afa9ce07841"
integrity sha512-74EfKjEbRFAy1ODoxVwuOMRKfi+CyWp/u1+EEW8ce6/ApXiVPYVfRWpBkTlsbKys9t/6U6Hq1xO5TW65tyYaUQ==
dependencies:
"@babel/runtime" "^7.5.5"
"@polkadot/util" "^0.95.0-beta.2"
"@polkadot/util-crypto" "^0.95.0-beta.2"
"@polkadot/util" "^0.95.0-beta.3"
"@polkadot/util-crypto" "^0.95.0-beta.3"
"@types/memoizee" "^0.4.2"
memoizee "^0.4.14"
"@polkadot/util-crypto@^0.95.0-beta.2":
version "0.95.0-beta.2"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.95.0-beta.2.tgz#74ce062bf9ad6325b7fa0f2bc1bfd6759044036f"
integrity sha512-P7Q0fCwhxJGUfISjUs2C2Cfz6zDjbGWfwOIOTXrvcDkuT+NXrfCxMpm70N5H4Rsu1WNEwlJegDyzg1HFL9cWVQ==
"@polkadot/util-crypto@^0.95.0-beta.3":
version "0.95.0-beta.3"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.95.0-beta.3.tgz#3532fcaa21964856b7c1667e87ea68b88731fbf4"
integrity sha512-4WUuQwFzM6Ze0t2ZuMrfu2/jmujrkuvTwZKU5eRgX6ce3cAiRbNyToJ66tQjhECVuaJwht5pp/mYZLO38ihOAQ==
dependencies:
"@babel/runtime" "^7.5.5"
"@polkadot/util" "^0.95.0-beta.2"
"@polkadot/util" "^0.95.0-beta.3"
"@polkadot/wasm-crypto" "^0.13.1"
"@types/bip39" "^2.4.2"
"@types/bs58" "^4.0.0"
@@ -1997,10 +1997,10 @@
tweetnacl "^1.0.1"
xxhashjs "^0.2.2"
"@polkadot/util@^0.95.0-beta.2":
version "0.95.0-beta.2"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.95.0-beta.2.tgz#50b56475206e9d1fdb641dd609f5ccbae21b08ab"
integrity sha512-yw4hERjg7O0Rm33MbVrlIyBmpmNuzc+tJ9t8cEUXfZO1Q0AQFVa2zd6KnKDWFJ2BkQJQTRWgYuUvUgGhFGr1nw==
"@polkadot/util@^0.95.0-beta.3":
version "0.95.0-beta.3"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.95.0-beta.3.tgz#80354da564f9b17c0257f55809feebc14a0f338d"
integrity sha512-zM1lBvMXNdfWTDG546cnKJkE9dTE1woDN0pKhEEJsHh4wwas+hxePQbJRNpXrQOYeTXpYRdr0EdRMzDL5PyA0g==
dependencies:
"@babel/runtime" "^7.5.5"
"@types/bn.js" "^4.11.5"