Skip to content

Commit 7615a42

Browse files
authored
Merge pull request #288 from contractormario/task/g278_key_generator
Implement KeyGenerator
2 parents 1149ce6 + 11934dc commit 7615a42

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/core/format/KeyGenerator.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 { UInt64 } from '../../model/UInt64';
18+
import { sha3_256 } from 'js-sha3';
19+
20+
export class KeyGenerator {
21+
/**
22+
* Generate UInt64 from a string
23+
* @param {string} input Input string
24+
* @returns {UInt64} Deterministic uint64 value for the given string
25+
*/
26+
public static generateUInt64Key(input: string): UInt64 {
27+
if (input.length === 0) {
28+
throw Error(`Input must not be empty`);
29+
}
30+
const buf = sha3_256.arrayBuffer(input);
31+
const result = new Uint32Array(buf);
32+
return new UInt64([result[0], (result[1] | 0x80000000) >>> 0]);
33+
}
34+
}

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';

test/core/format/KeyGenerator.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.generateUInt64Key('')).to.throw(Error, 'Input must not be empty');
24+
})
25+
it('returns UInt64', () => {
26+
expect(KeyGenerator.generateUInt64Key('a')).to.be.instanceOf(UInt64);
27+
})
28+
it('generates correct keys', () => {
29+
expect(KeyGenerator.generateUInt64Key('a').toHex()).to.equal('F524A0FBF24B0880');
30+
})
31+
it('generates keys deterministically', () => {
32+
expect(KeyGenerator.generateUInt64Key('abc').toHex()).to.equal('B225E24FA75D983A');
33+
expect(KeyGenerator.generateUInt64Key('abc').toHex()).to.equal('B225E24FA75D983A');
34+
expect(KeyGenerator.generateUInt64Key('def').toHex()).to.equal('B0AC5222678F0D8E');
35+
expect(KeyGenerator.generateUInt64Key('def').toHex()).to.equal('B0AC5222678F0D8E');
36+
expect(KeyGenerator.generateUInt64Key('abc').toHex()).to.equal('B225E24FA75D983A');
37+
})
38+
})
39+
});

0 commit comments

Comments
 (0)