@@ -21,6 +21,7 @@ import { NamespaceId } from '../../model/namespace/NamespaceId';
21
21
import { NetworkType } from '../../model/network/NetworkType' ;
22
22
import { Convert } from '../format/Convert' ;
23
23
import { RawAddress } from '../format/RawAddress' ;
24
+ import { DtoMapping } from './DtoMapping' ;
24
25
25
26
/**
26
27
* @internal
@@ -48,26 +49,40 @@ export class UnresolvedMapping {
48
49
}
49
50
50
51
/**
51
- * Map unresolved address string to Address or NamespaceId
52
- * @param {string } address The unresolved address in hex
52
+ * Map unresolved address string to Address or NamespaceId.
53
+ *
54
+ * Input examples:
55
+ * - 6826D27E1D0A26CA4E316F901E23E55C8711DB20DF45C536 Hex address (old rest)
56
+ * - NATNE7Q5BITMUTRRN6IB4I7FLSDRDWZA35C4KNQ Base32 address (new reset)
57
+ * - 99C2860B73398FD8D3000000000000000000000000000000 Hex namespace id (old rest)
58
+ * - THBIMC3THGH5RUYAAAAAAAAAAAAAAAAAAAAAAAA Base32 namespace id (new rest)
59
+ *
60
+ * @param {string } unresolvedAddressString The unresolved address in hex (old rest) or base32 address (new rest)
53
61
* @returns {UnresolvedAddress }
54
62
*/
55
- public static toUnresolvedAddress ( address : string ) : UnresolvedAddress {
56
- if ( ! Convert . isHexString ( address ) ) {
57
- throw new Error ( 'Input string is not in valid hexadecimal notation.' ) ;
58
- }
59
- // If bit 0 of byte 0 is not set (like in 0x90), then it is a regular address.
60
- // Else (e.g. 0x91) it represents a namespace id which starts at byte 1.
61
- const bit0 = Convert . hexToUint8 ( address . substr ( 1 , 2 ) ) [ 0 ] ;
62
- if ( ( bit0 & 16 ) === 16 ) {
63
- // namespaceId encoded hexadecimal notation provided
64
- // only 8 bytes are relevant to resolve the NamespaceId
65
- const relevantPart = address . substr ( 2 , 16 ) ;
66
- return NamespaceId . createFromEncoded ( Convert . uint8ToHex ( Convert . hexToUint8Reverse ( relevantPart ) ) ) ;
67
- }
63
+ public static toUnresolvedAddress ( unresolvedAddressString : string ) : UnresolvedAddress {
64
+ const fromHexToUnresolvedAddress = ( unresolvedAddressStringHex : string ) => {
65
+ // If bit 0 of byte 0 is not set (like in 0x90), then it is a regular address.
66
+ // Else (e.g. 0x91) it represents a namespace id which starts at byte 1.
67
+ const bit0 = Convert . hexToUint8 ( unresolvedAddressStringHex . substr ( 1 , 2 ) ) [ 0 ] ;
68
+ if ( ( bit0 & 16 ) === 16 ) {
69
+ // namespaceId encoded hexadecimal notation provided
70
+ // only 8 bytes are relevant to resolve the NamespaceId
71
+ const relevantPart = unresolvedAddressStringHex . substr ( 2 , 16 ) ;
72
+ return NamespaceId . createFromEncoded ( Convert . uint8ToHex ( Convert . hexToUint8Reverse ( relevantPart ) ) ) ;
73
+ }
68
74
69
- // read address from encoded hexadecimal notation
70
- return Address . createFromEncoded ( address ) ;
75
+ // read address from encoded hexadecimal notation
76
+ return Address . createFromEncoded ( unresolvedAddressStringHex ) ;
77
+ } ;
78
+
79
+ if ( Convert . isHexString ( unresolvedAddressString , 48 ) ) {
80
+ // Old Rest
81
+ return fromHexToUnresolvedAddress ( unresolvedAddressString ) ;
82
+ } else {
83
+ // New rest
84
+ return fromHexToUnresolvedAddress ( DtoMapping . toAddress ( unresolvedAddressString ) . encoded ( ) ) ;
85
+ }
71
86
}
72
87
73
88
/**
0 commit comments