1616
1717import { keccak256 , sha3_256 } from 'js-sha3' ;
1818import RIPEMD160 = require( 'ripemd160' ) ;
19+ import { NetworkType } from '../../model/blockchain/NetworkType' ;
1920import { SignSchema } from '../crypto' ;
2021import { SHA3Hasher } from '../crypto/SHA3Hasher' ;
2122import { Base32 } from './Base32' ;
@@ -75,21 +76,21 @@ export class RawAddress {
7576 /**
7677 * Converts a public key to a decoded address for a specific network.
7778 * @param {Uint8Array } publicKey The public key.
78- * @param {number } networkIdentifier The network identifier.
79+ * @param {NetworkType } networkType The network identifier.
7980 * @returns {Uint8Array } The decoded address corresponding to the inputs.
8081 */
8182 public static publicKeyToAddress = ( publicKey : Uint8Array ,
82- networkIdentifier : number ) : Uint8Array => {
83+ networkType : NetworkType ) : Uint8Array => {
8384 // step 1: sha3 hash of the public key
84- const signSchema = SHA3Hasher . resolveSignSchema ( networkIdentifier ) ;
85+ const signSchema = SHA3Hasher . resolveSignSchema ( networkType ) ;
8586 const publicKeyHash = signSchema === SignSchema . SHA3 ? sha3_256 . arrayBuffer ( publicKey ) : keccak256 . arrayBuffer ( publicKey ) ;
8687
8788 // step 2: ripemd160 hash of (1)
8889 const ripemdHash = new RIPEMD160 ( ) . update ( new Buffer ( publicKeyHash ) ) . digest ( ) ;
8990
9091 // step 3: add network identifier byte in front of (2)
9192 const decodedAddress = new Uint8Array ( RawAddress . constants . sizes . addressDecoded ) ;
92- decodedAddress [ 0 ] = networkIdentifier ;
93+ decodedAddress [ 0 ] = networkType ;
9394 RawArray . copy ( decodedAddress , ripemdHash , RawAddress . constants . sizes . ripemd160 , 1 ) ;
9495
9596 // step 4: concatenate (3) and the checksum of (3)
@@ -106,11 +107,11 @@ export class RawAddress {
106107 /**
107108 * Determines the validity of a decoded address.
108109 * @param {Uint8Array } decoded The decoded address.
109- * @param {number } networkIdentifier The network identifier.
110+ * @param {NetworkType } networkType The network identifier.
110111 * @returns {boolean } true if the decoded address is valid, false otherwise.
111112 */
112- public static isValidAddress = ( decoded : Uint8Array , networkIdentifier : number ) : boolean => {
113- const signSchema = SHA3Hasher . resolveSignSchema ( networkIdentifier ) ;
113+ public static isValidAddress = ( decoded : Uint8Array , networkType : NetworkType ) : boolean => {
114+ const signSchema = SHA3Hasher . resolveSignSchema ( networkType ) ;
114115 const hash = signSchema === SignSchema . SHA3 ? sha3_256 . create ( ) : keccak256 . create ( ) ;
115116 const checksumBegin = RawAddress . constants . sizes . addressDecoded - RawAddress . constants . sizes . checksum ;
116117 hash . update ( decoded . subarray ( 0 , checksumBegin ) ) ;
@@ -122,17 +123,17 @@ export class RawAddress {
122123 /**
123124 * Determines the validity of an encoded address string.
124125 * @param {string } encoded The encoded address string.
125- * @param {number } networkIdentifier The network identifier.
126+ * @param {NetworkType } networkType The network identifier.
126127 * @returns {boolean } true if the encoded address string is valid, false otherwise.
127128 */
128- public static isValidEncodedAddress = ( encoded : string , networkIdentifier : number ) : boolean => {
129+ public static isValidEncodedAddress = ( encoded : string , networkType : NetworkType ) : boolean => {
129130 if ( RawAddress . constants . sizes . addressEncoded !== encoded . length ) {
130131 return false ;
131132 }
132133
133134 try {
134135 const decoded = RawAddress . stringToAddress ( encoded ) ;
135- return RawAddress . isValidAddress ( decoded , networkIdentifier ) ;
136+ return RawAddress . isValidAddress ( decoded , networkType ) ;
136137 } catch ( err ) {
137138 return false ;
138139 }
0 commit comments