Skip to content

Commit cb426f3

Browse files
Implement KeyGenerator
1 parent 77485b2 commit cb426f3

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/core/format/KeyGenerator.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2018 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 { UInt64 } from '../../model/UInt64';
18+
import { sha3_256 } from 'js-sha3';
19+
20+
export class KeyGenerator {
21+
/**
22+
* @returns {UInt64} Deterministic uint64 value for the given string
23+
*/
24+
public static fromString(input: string) {
25+
if (input.length === 0) {
26+
throw Error(`Input must not be empty`);
27+
}
28+
if (input.length > 1024) {
29+
throw Error(`Input exceeds 1024 characters (has ${input.length})`);
30+
}
31+
const format = /^[a-zA-Z0-9_]+$/gm;
32+
if (input.match(format) === null) {
33+
throw Error(`Input has invalid format (accepted characters: a-z, A-Z, 0-9, _)`);
34+
}
35+
const hex = sha3_256(input)
36+
return UInt64.fromHex(hex.substr(0, 16))
37+
}
38+
}

src/core/format/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export * from './RawAddress';
1818
export * from './RawArray';
1919
export * from './Convert';
2020
export * from './IdGenerator';
21+
export * from './KeyGenerator';
2122
export * from './RawUInt64';
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 { expect } from 'chai';
17+
import { UInt64 } from '../../../src/model/UInt64';
18+
import { KeyGenerator } from '../../../src/core/format/KeyGenerator';
19+
20+
describe('key generator', () => {
21+
describe('generate key from string', () => {
22+
it('throws if input is empty', () => {
23+
expect(() => KeyGenerator.fromString('')).to.throw(Error, 'Input must not be empty');
24+
})
25+
it('returns UInt64', () => {
26+
expect(KeyGenerator.fromString('a')).to.be.instanceOf(UInt64);
27+
})
28+
it('throws if input has invalid format', () => {
29+
expect(() => KeyGenerator.fromString('$abc')).to.throw(Error, '');
30+
expect(() => KeyGenerator.fromString('ab-c')).to.throw(Error, '');
31+
expect(() => KeyGenerator.fromString('abc.')).to.throw(Error, '');
32+
})
33+
it('generates correct keys', () => {
34+
expect(KeyGenerator.fromString('a').toHex()).to.equal('80084BF2FBA02475');
35+
})
36+
it('generates keys deterministically', () => {
37+
expect(KeyGenerator.fromString('abc').toHex()).to.equal('3A985DA74FE225B2');
38+
expect(KeyGenerator.fromString('abc').toHex()).to.equal('3A985DA74FE225B2');
39+
expect(KeyGenerator.fromString('def').toHex()).to.equal('8E0D8F672252ACB0');
40+
expect(KeyGenerator.fromString('def').toHex()).to.equal('8E0D8F672252ACB0');
41+
expect(KeyGenerator.fromString('abc').toHex()).to.equal('3A985DA74FE225B2');
42+
})
43+
})
44+
});

0 commit comments

Comments
 (0)