Skip to content

Commit aff8179

Browse files
committed
Added #93
Merged encoders from Lib into SDK
1 parent 59f08ac commit aff8179

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2541
-42
lines changed

e2e/infrastructure/TransactionHttp.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {assert, expect} from 'chai';
1717
import * as CryptoJS from 'crypto-js';
1818
import {ChronoUnit} from 'js-joda';
1919
import {keccak_256, sha3_256} from 'js-sha3';
20-
import {convert, nacl_catapult} from 'nem2-library';
20+
import {nacl_catapult} from 'nem2-library';
21+
import { Convert as convert } from '../../src/core/format';
2122
import {AccountHttp} from '../../src/infrastructure/AccountHttp';
2223
import { NamespaceHttp } from '../../src/infrastructure/infrastructure';
2324
import {Listener} from '../../src/infrastructure/Listener';

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export * from './src/infrastructure/infrastructure';
1818
export * from './src/model/model';
1919
export * from './src/service/service';
2020
export * from './src/core/utils/utility';
21+
export * from './src/core/format';

src/core/format/Base32.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as utilities from './Utilities';
18+
19+
export class Base32 {
20+
/**
21+
* Base32 encodes a binary buffer.
22+
* @param {Uint8Array} data The binary data to encode.
23+
* @returns {string} The base32 encoded string corresponding to the input data.
24+
*/
25+
public static Base32Encode = (data: Uint8Array): string => {
26+
if (0 !== data.length % utilities.Decoded_Block_Size) {
27+
throw Error(`decoded size must be multiple of ${utilities.Decoded_Block_Size}`);
28+
}
29+
const output = new Array(data.length / utilities.Decoded_Block_Size * utilities.Encoded_Block_Size);
30+
for (let i = 0; i < data.length / utilities.Decoded_Block_Size; ++i) {
31+
utilities.encodeBlock(data, i * utilities.Decoded_Block_Size, output, i * utilities.Encoded_Block_Size);
32+
}
33+
return output.join('');
34+
}
35+
36+
/**
37+
* Base32 decodes a base32 encoded string.
38+
* @param {string} encoded The base32 encoded string to decode.
39+
* @returns {Uint8Array} The binary data corresponding to the input string.
40+
*/
41+
public static Base32Decode = (encoded: string): Uint8Array => {
42+
if (0 !== encoded.length % utilities.Encoded_Block_Size) {
43+
throw Error(`encoded size must be multiple of ${utilities.Encoded_Block_Size}`);
44+
}
45+
46+
const output = new Uint8Array(encoded.length / utilities.Encoded_Block_Size * utilities.Decoded_Block_Size);
47+
for (let i = 0; i < encoded.length / utilities.Encoded_Block_Size; ++i) {
48+
utilities.decodeBlock(encoded, i * utilities.Encoded_Block_Size, output, i * utilities.Decoded_Block_Size);
49+
}
50+
return output;
51+
}
52+
}

src/core/format/Convert.ts

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import * as utilities from './Utilities';
17+
18+
export class Convert {
19+
20+
/**
21+
* Decodes two hex characters into a byte.
22+
* @param {string} char1 The first hex digit.
23+
* @param {string} char2 The second hex digit.
24+
* @returns {number} The decoded byte.
25+
*/
26+
public static toByte = (char1: string, char2: string): number => {
27+
const byte = utilities.tryParseByte(char1, char2);
28+
if (undefined === byte) {
29+
throw Error(`unrecognized hex char`);
30+
}
31+
return byte;
32+
}
33+
34+
/**
35+
* Determines whether or not a string is a hex string.
36+
* @param {string} input The string to test.
37+
* @returns {boolean} true if the input is a hex string, false otherwise.
38+
*/
39+
public static isHexString = (input: string): boolean => {
40+
if (0 !== input.length % 2) {
41+
return false;
42+
}
43+
for (let i = 0; i < input.length; i += 2) {
44+
if (undefined === utilities.tryParseByte(input[i], input[i + 1])) {
45+
return false;
46+
}
47+
}
48+
return true;
49+
}
50+
51+
/**
52+
* Converts a hex string to a uint8 array.
53+
* @param {string} input A hex encoded string.
54+
* @returns {Uint8Array} A uint8 array corresponding to the input.
55+
*/
56+
public static hexToUint8 = (input: string): Uint8Array => {
57+
if (0 !== input.length % 2) {
58+
throw Error(`hex string has unexpected size '${input.length}'`);
59+
}
60+
const output = new Uint8Array(input.length / 2);
61+
for (let i = 0; i < input.length; i += 2) {
62+
output[i / 2] = Convert.toByte(input[i], input[i + 1]);
63+
}
64+
return output;
65+
}
66+
67+
/**
68+
* Reversed convertion hex string to a uint8 array.
69+
* @param {string} input A hex encoded string.
70+
* @returns {Uint8Array} A uint8 array corresponding to the input.
71+
*/
72+
public static hexToUint8Reverse = (input: string): Uint8Array => {
73+
if (0 !== input.length % 2) {
74+
throw Error(`hex string has unexpected size '${input.length}'`);
75+
}
76+
const output = new Uint8Array(input.length / 2);
77+
for (let i = 0; i < input.length; i += 2) {
78+
output[output.length - 1 - (i / 2)] = Convert.toByte(input[i], input[i + 1]);
79+
}
80+
return output;
81+
}
82+
83+
/**
84+
* Converts a uint8 array to a hex string.
85+
* @param {Uint8Array} input A uint8 array.
86+
* @returns {string} A hex encoded string corresponding to the input.
87+
*/
88+
public static uint8ToHex = (input) => {
89+
let s = '';
90+
for (const byte of input) {
91+
s += utilities.Nibble_To_Char_Map[byte >> 4];
92+
s += utilities.Nibble_To_Char_Map[byte & 0x0F];
93+
}
94+
95+
return s;
96+
}
97+
98+
/**
99+
* Converts a uint8 array to a uint32 array.
100+
* @param {Uint8Array} input A uint8 array.
101+
* @returns {Uint32Array} A uint32 array created from the input.
102+
*/
103+
public static uint8ToUint32 = (input) => new Uint32Array(input.buffer);
104+
105+
/**
106+
* Converts a uint32 array to a uint8 array.
107+
* @param {Uint32Array} input A uint32 array.
108+
* @returns {Uint8Array} A uint8 array created from the input.
109+
*/
110+
public static uint32ToUint8 = (input: Uint32Array): Uint8Array => new Uint8Array(input.buffer);
111+
112+
/** Converts an unsigned byte to a signed byte with the same binary representation.
113+
* @param {number} input An unsigned byte.
114+
* @returns {number} A signed byte with the same binary representation as the input.
115+
*
116+
*/
117+
public static uint8ToInt8 = (input: number): number => {
118+
if (0xFF < input) {
119+
throw Error(`input '${input}' is out of range`);
120+
}
121+
return input << 24 >> 24;
122+
}
123+
124+
/** Converts a signed byte to an unsigned byte with the same binary representation.
125+
* @param {number} input A signed byte.
126+
* @returns {number} An unsigned byte with the same binary representation as the input.
127+
*/
128+
public static int8ToUint8 = (input: number): number => {
129+
if (127 < input || -128 > input) {
130+
throw Error(`input '${input}' is out of range`);
131+
}
132+
return input & 0xFF;
133+
}
134+
135+
/**
136+
* Converts a raw javascript string into a string of single byte characters using utf8 encoding.
137+
* This makes it easier to perform other encoding operations on the string.
138+
* @param {string} input - A raw string
139+
* @return {string} - UTF-8 string
140+
*/
141+
public static rstr2utf8 = (input: string): string => {
142+
let output = '';
143+
144+
for (let n = 0; n < input.length; n++) {
145+
const c = input.charCodeAt(n);
146+
147+
if (128 > c) {
148+
output += String.fromCharCode(c);
149+
} else if ((127 < c) && (2048 > c)) {
150+
output += String.fromCharCode((c >> 6) | 192);
151+
output += String.fromCharCode((c & 63) | 128);
152+
} else {
153+
output += String.fromCharCode((c >> 12) | 224);
154+
output += String.fromCharCode(((c >> 6) & 63) | 128);
155+
output += String.fromCharCode((c & 63) | 128);
156+
}
157+
}
158+
159+
return output;
160+
}
161+
162+
/**
163+
* Convert UTF-8 to hex
164+
* @param {string} input - An UTF-8 string
165+
* @return {string}
166+
*/
167+
public static utf8ToHex = (input: string): string => {
168+
const rawString = Convert.rstr2utf8(input);
169+
let result = '';
170+
for (let i = 0; i < rawString.length; i++) {
171+
result += rawString.charCodeAt(i).toString(16);
172+
}
173+
return result;
174+
}
175+
}

src/core/format/IdGenerator.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import {sha3_256} from 'js-sha3';
17+
import * as utilities from './Utilities';
18+
19+
export class IdGenerator {
20+
/**
21+
* Generates a mosaic id given a nonce and a public id.
22+
* @param {object} nonce The mosaic nonce.
23+
* @param {object} ownerPublicId The public id.
24+
* @returns {module:coders/uint64~uint64} The mosaic id.
25+
*/
26+
public static generateMosaicId = (nonce, ownerPublicId) => {
27+
const hash = sha3_256.create();
28+
hash.update(nonce);
29+
hash.update(ownerPublicId);
30+
const result = new Uint32Array(hash.arrayBuffer());
31+
return [result[0], result[1] & 0x7FFFFFFF];
32+
}
33+
34+
/**
35+
* Parses a unified namespace name into a path.
36+
* @param {string} name The unified namespace name.
37+
* @returns {array<module:coders/uint64~uint64>} The namespace path.
38+
*/
39+
public static generateNamespacePath = (name: string) => {
40+
if (0 >= name.length) {
41+
utilities.throwInvalidFqn('having zero length', name);
42+
}
43+
let namespaceId = utilities.idGeneratorConst.namespace_base_id;
44+
const path = [];
45+
const start = utilities.split(name, (substringStart, size) => {
46+
namespaceId = utilities.generateNamespaceId(namespaceId, utilities.extractPartName(name, substringStart, size));
47+
utilities.append(path, namespaceId, name);
48+
});
49+
namespaceId = utilities.generateNamespaceId(namespaceId, utilities.extractPartName(name, start, name.length - start));
50+
utilities.append(path, namespaceId, name);
51+
return path;
52+
}
53+
}

0 commit comments

Comments
 (0)