Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion e2e/infrastructure/TransactionHttp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {assert, expect} from 'chai';
import * as CryptoJS from 'crypto-js';
import {ChronoUnit} from 'js-joda';
import {keccak_256, sha3_256} from 'js-sha3';
import {convert, nacl_catapult} from 'nem2-library';
import {nacl_catapult} from 'nem2-library';
import { Convert as convert } from '../../src/core/format';
import {AccountHttp} from '../../src/infrastructure/AccountHttp';
import { NamespaceHttp } from '../../src/infrastructure/infrastructure';
import {Listener} from '../../src/infrastructure/Listener';
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './src/infrastructure/infrastructure';
export * from './src/model/model';
export * from './src/service/service';
export * from './src/core/utils/utility';
export * from './src/core/format';
52 changes: 52 additions & 0 deletions src/core/format/Base32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2019 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as utilities from './Utilities';

export class Base32 {
/**
* Base32 encodes a binary buffer.
* @param {Uint8Array} data The binary data to encode.
* @returns {string} The base32 encoded string corresponding to the input data.
*/
public static Base32Encode = (data: Uint8Array): string => {
if (0 !== data.length % utilities.Decoded_Block_Size) {
throw Error(`decoded size must be multiple of ${utilities.Decoded_Block_Size}`);
}
const output = new Array(data.length / utilities.Decoded_Block_Size * utilities.Encoded_Block_Size);
for (let i = 0; i < data.length / utilities.Decoded_Block_Size; ++i) {
utilities.encodeBlock(data, i * utilities.Decoded_Block_Size, output, i * utilities.Encoded_Block_Size);
}
return output.join('');
}

/**
* Base32 decodes a base32 encoded string.
* @param {string} encoded The base32 encoded string to decode.
* @returns {Uint8Array} The binary data corresponding to the input string.
*/
public static Base32Decode = (encoded: string): Uint8Array => {
if (0 !== encoded.length % utilities.Encoded_Block_Size) {
throw Error(`encoded size must be multiple of ${utilities.Encoded_Block_Size}`);
}

const output = new Uint8Array(encoded.length / utilities.Encoded_Block_Size * utilities.Decoded_Block_Size);
for (let i = 0; i < encoded.length / utilities.Encoded_Block_Size; ++i) {
utilities.decodeBlock(encoded, i * utilities.Encoded_Block_Size, output, i * utilities.Decoded_Block_Size);
}
return output;
}
}
175 changes: 175 additions & 0 deletions src/core/format/Convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Copyright 2019 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as utilities from './Utilities';

export class Convert {

/**
* Decodes two hex characters into a byte.
* @param {string} char1 The first hex digit.
* @param {string} char2 The second hex digit.
* @returns {number} The decoded byte.
*/
public static toByte = (char1: string, char2: string): number => {
const byte = utilities.tryParseByte(char1, char2);
if (undefined === byte) {
throw Error(`unrecognized hex char`);
}
return byte;
}

/**
* Determines whether or not a string is a hex string.
* @param {string} input The string to test.
* @returns {boolean} true if the input is a hex string, false otherwise.
*/
public static isHexString = (input: string): boolean => {
if (0 !== input.length % 2) {
return false;
}
for (let i = 0; i < input.length; i += 2) {
if (undefined === utilities.tryParseByte(input[i], input[i + 1])) {
return false;
}
}
return true;
}

/**
* Converts a hex string to a uint8 array.
* @param {string} input A hex encoded string.
* @returns {Uint8Array} A uint8 array corresponding to the input.
*/
public static hexToUint8 = (input: string): Uint8Array => {
if (0 !== input.length % 2) {
throw Error(`hex string has unexpected size '${input.length}'`);
}
const output = new Uint8Array(input.length / 2);
for (let i = 0; i < input.length; i += 2) {
output[i / 2] = Convert.toByte(input[i], input[i + 1]);
}
return output;
}

/**
* Reversed convertion hex string to a uint8 array.
* @param {string} input A hex encoded string.
* @returns {Uint8Array} A uint8 array corresponding to the input.
*/
public static hexToUint8Reverse = (input: string): Uint8Array => {
if (0 !== input.length % 2) {
throw Error(`hex string has unexpected size '${input.length}'`);
}
const output = new Uint8Array(input.length / 2);
for (let i = 0; i < input.length; i += 2) {
output[output.length - 1 - (i / 2)] = Convert.toByte(input[i], input[i + 1]);
}
return output;
}

/**
* Converts a uint8 array to a hex string.
* @param {Uint8Array} input A uint8 array.
* @returns {string} A hex encoded string corresponding to the input.
*/
public static uint8ToHex = (input) => {
let s = '';
for (const byte of input) {
s += utilities.Nibble_To_Char_Map[byte >> 4];
s += utilities.Nibble_To_Char_Map[byte & 0x0F];
}

return s;
}

/**
* Converts a uint8 array to a uint32 array.
* @param {Uint8Array} input A uint8 array.
* @returns {Uint32Array} A uint32 array created from the input.
*/
public static uint8ToUint32 = (input) => new Uint32Array(input.buffer);

/**
* Converts a uint32 array to a uint8 array.
* @param {Uint32Array} input A uint32 array.
* @returns {Uint8Array} A uint8 array created from the input.
*/
public static uint32ToUint8 = (input: Uint32Array): Uint8Array => new Uint8Array(input.buffer);

/** Converts an unsigned byte to a signed byte with the same binary representation.
* @param {number} input An unsigned byte.
* @returns {number} A signed byte with the same binary representation as the input.
*
*/
public static uint8ToInt8 = (input: number): number => {
if (0xFF < input) {
throw Error(`input '${input}' is out of range`);
}
return input << 24 >> 24;
}

/** Converts a signed byte to an unsigned byte with the same binary representation.
* @param {number} input A signed byte.
* @returns {number} An unsigned byte with the same binary representation as the input.
*/
public static int8ToUint8 = (input: number): number => {
if (127 < input || -128 > input) {
throw Error(`input '${input}' is out of range`);
}
return input & 0xFF;
}

/**
* Converts a raw javascript string into a string of single byte characters using utf8 encoding.
* This makes it easier to perform other encoding operations on the string.
* @param {string} input - A raw string
* @return {string} - UTF-8 string
*/
public static rstr2utf8 = (input: string): string => {
let output = '';

for (let n = 0; n < input.length; n++) {
const c = input.charCodeAt(n);

if (128 > c) {
output += String.fromCharCode(c);
} else if ((127 < c) && (2048 > c)) {
output += String.fromCharCode((c >> 6) | 192);
output += String.fromCharCode((c & 63) | 128);
} else {
output += String.fromCharCode((c >> 12) | 224);
output += String.fromCharCode(((c >> 6) & 63) | 128);
output += String.fromCharCode((c & 63) | 128);
}
}

return output;
}

/**
* Convert UTF-8 to hex
* @param {string} input - An UTF-8 string
* @return {string}
*/
public static utf8ToHex = (input: string): string => {
const rawString = Convert.rstr2utf8(input);
let result = '';
for (let i = 0; i < rawString.length; i++) {
result += rawString.charCodeAt(i).toString(16);
}
return result;
}
}
53 changes: 53 additions & 0 deletions src/core/format/IdGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2019 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {sha3_256} from 'js-sha3';
import * as utilities from './Utilities';

export class IdGenerator {
/**
* Generates a mosaic id given a nonce and a public id.
* @param {object} nonce The mosaic nonce.
* @param {object} ownerPublicId The public id.
* @returns {module:coders/uint64~uint64} The mosaic id.
*/
public static generateMosaicId = (nonce, ownerPublicId) => {
const hash = sha3_256.create();
hash.update(nonce);
hash.update(ownerPublicId);
const result = new Uint32Array(hash.arrayBuffer());
return [result[0], result[1] & 0x7FFFFFFF];
}

/**
* Parses a unified namespace name into a path.
* @param {string} name The unified namespace name.
* @returns {array<module:coders/uint64~uint64>} The namespace path.
*/
public static generateNamespacePath = (name: string) => {
if (0 >= name.length) {
utilities.throwInvalidFqn('having zero length', name);
}
let namespaceId = utilities.idGeneratorConst.namespace_base_id;
const path = [];
const start = utilities.split(name, (substringStart, size) => {
namespaceId = utilities.generateNamespaceId(namespaceId, utilities.extractPartName(name, substringStart, size));
utilities.append(path, namespaceId, name);
});
namespaceId = utilities.generateNamespaceId(namespaceId, utilities.extractPartName(name, start, name.length - start));
utilities.append(path, namespaceId, name);
return path;
}
}
Loading