Skip to content

Commit c491bea

Browse files
author
Greg S
committed
WIP #55: Added Alias models ; Added NamespaceInfo.alias for cow upgrade ;
1 parent 4c06905 commit c491bea

File tree

10 files changed

+287
-3
lines changed

10 files changed

+287
-3
lines changed

src/infrastructure/NamespaceHttp.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import {from as observableFrom, Observable} from 'rxjs';
1818
import {map, mergeMap} from 'rxjs/operators';
1919
import {Address} from '../model/account/Address';
2020
import {PublicAccount} from '../model/account/PublicAccount';
21+
import {MosaicId} from '../model/mosaic/MosaicId';
22+
import {AddressAlias} from '../model/namespace/AddressAlias';
23+
import {Alias} from '../model/namespace/Alias';
24+
import {AliasType} from '../model/namespace/AliasType';
25+
import {EmptyAlias} from '../model/namespace/EmptyAlias';
26+
import {MosaicAlias} from '../model/namespace/MosaicAlias';
2127
import {NamespaceId} from '../model/namespace/NamespaceId';
2228
import {NamespaceInfo} from '../model/namespace/NamespaceInfo';
2329
import {NamespaceName} from '../model/namespace/NamespaceName';
@@ -70,6 +76,7 @@ export class NamespaceHttp extends Http implements NamespaceRepository {
7076
PublicAccount.createFromPublicKey(namespaceInfoDTO.namespace.owner, networkType),
7177
new UInt64(namespaceInfoDTO.namespace.startHeight),
7278
new UInt64(namespaceInfoDTO.namespace.endHeight),
79+
this.extractAlias(namespaceInfoDTO.namespace),
7380
);
7481
}))));
7582
}
@@ -98,6 +105,7 @@ export class NamespaceHttp extends Http implements NamespaceRepository {
98105
PublicAccount.createFromPublicKey(namespaceInfoDTO.namespace.owner, networkType),
99106
new UInt64(namespaceInfoDTO.namespace.startHeight),
100107
new UInt64(namespaceInfoDTO.namespace.endHeight),
108+
this.extractAlias(namespaceInfoDTO.namespace),
101109
);
102110
});
103111
}))));
@@ -130,6 +138,7 @@ export class NamespaceHttp extends Http implements NamespaceRepository {
130138
PublicAccount.createFromPublicKey(namespaceInfoDTO.namespace.owner, networkType),
131139
new UInt64(namespaceInfoDTO.namespace.startHeight),
132140
new UInt64(namespaceInfoDTO.namespace.endHeight),
141+
this.extractAlias(namespaceInfoDTO.namespace),
133142
);
134143
});
135144
}))));
@@ -169,4 +178,21 @@ export class NamespaceHttp extends Http implements NamespaceRepository {
169178
}
170179
return result;
171180
}
181+
182+
/**
183+
* Extract the alias from a namespace
184+
*
185+
* @internal
186+
* @access private
187+
* @param namespace
188+
*/
189+
private extractAlias(namespace: any): Alias {
190+
if (namespace.alias && namespace.alias.type === AliasType.Mosaic) {
191+
return new MosaicAlias(namespace.alias.type, namespace.alias.mosaicId);
192+
} else if (namespace.alias && namespace.alias.type === AliasType.Address) {
193+
return new AddressAlias(namespace.alias.type, namespace.alias.address);
194+
}
195+
196+
return new EmptyAlias();
197+
}
172198
}

src/model/model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export * from './mosaic/XEM';
4343

4444
// Namespace
4545
export * from '../service/Namespace';
46+
export * from './namespace/AliasType';
47+
export * from './namespace/Alias';
48+
export * from './namespace/AddressAlias';
49+
export * from './namespace/MosaicAlias';
4650
export * from './namespace/NamespaceId';
4751
export * from './namespace/NamespaceInfo';
4852
export * from './namespace/NamespaceName';
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 {namespaceId as NamespaceIdGenerator} from 'nem2-library';
17+
import {Address} from '../account/Address';
18+
import {Alias} from './Alias';
19+
20+
/**
21+
* The AddressAlias structure describes address aliases
22+
*
23+
* @since 0.10.2
24+
*/
25+
export class AddressAlias implements Alias {
26+
27+
/**
28+
* Create AddressAlias object
29+
*
30+
* @param type
31+
* @param content
32+
*/
33+
constructor(/**
34+
* The alias type
35+
*/
36+
public readonly type: number,
37+
/**
38+
* The alias address
39+
*/
40+
public readonly address: Address) {
41+
}
42+
43+
/**
44+
* Compares AddressAlias for equality.
45+
*
46+
* @return boolean
47+
*/
48+
public equals(alias: any): boolean {
49+
if (alias instanceof AddressAlias) {
50+
return this.address.equals(alias.address);
51+
}
52+
return false;
53+
}
54+
}

src/model/namespace/Alias.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 {namespaceId as NamespaceIdGenerator} from 'nem2-library';
17+
import {Address} from '../account/Address';
18+
import {MosaicId} from '../mosaic/MosaicId';
19+
import {AddressAlias} from './AddressAlias';
20+
import {AliasType} from './AliasType';
21+
import {MosaicAlias} from './MosaicAlias';
22+
23+
/**
24+
* The alias structure defines an interface for Aliases
25+
*
26+
* @since 0.10.2
27+
*/
28+
export interface Alias {
29+
/**
30+
* The alias type
31+
*
32+
* - 0 : No alias
33+
* - 1 : Mosaic id alias
34+
* - 2 : Address alias
35+
*/
36+
readonly type: number;
37+
38+
/**
39+
* The alias address
40+
*/
41+
readonly address?: Address;
42+
43+
/**
44+
* The alias mosaicId
45+
*/
46+
readonly mosaicId?: MosaicId;
47+
}

src/model/namespace/AliasType.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
/**
18+
* The alias type. Supported types are:
19+
* 0: No alias.
20+
* 1: Mosaic id alias.
21+
* 2: Address alias.
22+
*
23+
* @since 0.10.2
24+
*/
25+
export enum AliasType {
26+
None = 0,
27+
Mosaic = 1,
28+
Address = 2,
29+
}

src/model/namespace/EmptyAlias.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 {namespaceId as NamespaceIdGenerator} from 'nem2-library';
17+
import {Address} from '../account/Address';
18+
import {Alias} from './Alias';
19+
20+
/**
21+
* The EmptyAlias structure describes empty aliases (type:0)
22+
*
23+
* @since 0.10.2
24+
*/
25+
export class EmptyAlias implements Alias {
26+
/**
27+
* The alias type
28+
*/
29+
public readonly type: number;
30+
31+
/**
32+
* Create EmptyAlias object
33+
*
34+
* @param type
35+
* @param content
36+
*/
37+
constructor() {
38+
this.type = 0;
39+
}
40+
41+
/**
42+
* Compares EmptyAlias for equality.
43+
*
44+
* @return boolean
45+
*/
46+
public equals(alias: any): boolean {
47+
return alias instanceof EmptyAlias || alias.type === 0;
48+
}
49+
}

src/model/namespace/MosaicAlias.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 {namespaceId as NamespaceIdGenerator} from 'nem2-library';
17+
import {MosaicId} from '../mosaic/MosaicId';
18+
import {Alias} from './Alias';
19+
20+
/**
21+
* The MosaicAlias structure describe mosaic aliases
22+
*
23+
* @since 0.10.2
24+
*/
25+
export class MosaicAlias implements Alias {
26+
27+
/**
28+
* Create AddressAlias object
29+
*
30+
* @param type
31+
* @param mosaicId
32+
*/
33+
constructor(/**
34+
* The alias type
35+
*/
36+
public readonly type: number,
37+
/**
38+
* The alias address
39+
*/
40+
public readonly mosaicId: MosaicId) {
41+
}
42+
43+
/**
44+
* Compares AddressAlias for equality.
45+
*
46+
* @return boolean
47+
*/
48+
public equals(alias: any): boolean {
49+
if (alias instanceof MosaicAlias) {
50+
return this.mosaicId.equals(alias.mosaicId);
51+
}
52+
return false;
53+
}
54+
}

src/model/namespace/NamespaceInfo.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import {PublicAccount} from '../account/PublicAccount';
1818
import {UInt64} from '../UInt64';
19+
import {Alias} from './Alias';
1920
import {NamespaceId} from './NamespaceId';
2021

2122
/**
@@ -74,7 +75,11 @@ export class NamespaceInfo {
7475
/**
7576
* The height at which the ownership ends.
7677
*/
77-
public readonly endHeight: UInt64) {
78+
public readonly endHeight: UInt64,
79+
/**
80+
* The alias linked to a namespace.
81+
*/
82+
public readonly alias: Alias) {
7883

7984
}
8085

@@ -102,6 +107,14 @@ export class NamespaceInfo {
102107
return this.type === 1;
103108
}
104109

110+
/**
111+
* Has alias
112+
* @returns {boolean}
113+
*/
114+
public hasAlias(): boolean {
115+
return this.alias.type !== 0;
116+
}
117+
105118
/**
106119
* Get parent id
107120
* @returns {Id}

test/model/namespace/NamespaceInfo.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('NamespaceInfo', () => {
4141
parentId: new NamespaceId([0, 0]),
4242
startHeight: new UInt64([1, 0]),
4343
type: 0,
44+
alias: {type: 0},
4445
},
4546
};
4647
subNamespaceDTO = {
@@ -73,6 +74,7 @@ describe('NamespaceInfo', () => {
7374
50795,
7475
0,
7576
],
77+
alias: {type: 0},
7678
},
7779
};
7880
});
@@ -88,6 +90,7 @@ describe('NamespaceInfo', () => {
8890
expect(namespaceInfo.owner.publicKey).to.be.equal(rootNamespaceDTO.namespace.owner);
8991
deepEqual(namespaceInfo.startHeight, rootNamespaceDTO.namespace.startHeight);
9092
deepEqual(namespaceInfo.endHeight, rootNamespaceDTO.namespace.endHeight);
93+
deepEqual(namespaceInfo.alias, rootNamespaceDTO.namespace.alias);
9194
});
9295

9396
it('should return the NamespaceId in string format', () => {
@@ -156,6 +159,7 @@ describe('NamespaceInfo', () => {
156159
PublicAccount.createFromPublicKey(dto.namespace.owner, NetworkType.MIJIN_TEST),
157160
dto.namespace.startHeight,
158161
dto.namespace.endHeight,
162+
dto.namespace.alias,
159163
);
160164
}
161165

@@ -171,6 +175,7 @@ describe('NamespaceInfo', () => {
171175
PublicAccount.createFromPublicKey(dto.namespace.owner, NetworkType.MIJIN_TEST),
172176
dto.namespace.startHeight,
173177
dto.namespace.endHeight,
178+
dto.namespace.alias,
174179
);
175180
}
176181

0 commit comments

Comments
 (0)