Basic QR tests (+ number encoding fix) (#168)

* Basic QR tests (+ number encoding fix)

* skipEncoding for Address display

* Fixup comments
This commit is contained in:
Jaco Greeff
2019-07-29 17:40:08 +02:00
committed by GitHub
parent 0ad431ba87
commit f7bd11a293
8 changed files with 148 additions and 69 deletions
+54 -1
View File
@@ -2,8 +2,15 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { u8aConcat, u8aToU8a } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
import { ADDRESS_PREFIX, CMD_SIGN_TX, CRYPTO_SR25519, DEFAULT_IMG_SIZE, FRAME_SIZE, SUBSTRATE_ID } from './constants';
const MULTIPART = new Uint8Array([0]);
export function encodeNumber (value: number): Uint8Array {
return new Uint8Array([value >> 8, value & 256]);
return new Uint8Array([value >> 8, value & 0xff]);
}
export function encodeString (value: string): Uint8Array {
@@ -21,3 +28,49 @@ export function decodeString (value: Uint8Array): string {
return str + String.fromCharCode(code);
}, '');
}
export function createAddressPayload (address: string): Uint8Array {
return u8aConcat(
encodeString(ADDRESS_PREFIX),
encodeString(address)
);
}
export function createSignPayload (address: string, payload: string | Uint8Array): Uint8Array {
return u8aConcat(
SUBSTRATE_ID,
CRYPTO_SR25519,
CMD_SIGN_TX,
decodeAddress(address),
u8aToU8a(payload)
);
}
export function createFrames (input: Uint8Array): string[] {
const frames = [];
let idx = 0;
while (idx < input.length) {
frames.push(input.subarray(idx, idx + FRAME_SIZE));
idx += FRAME_SIZE;
}
return frames.map((frame, index: number): string =>
decodeString(u8aConcat(
MULTIPART,
encodeNumber(frames.length),
encodeNumber(index),
frame
))
);
}
export function createImgSize (size: number = DEFAULT_IMG_SIZE): Record<string, string> {
const height = `${size}px`;
return {
height,
width: height
};
}