@@ -96,16 +96,16 @@ export const verify = async (token) => {
9696} ;
9797
9898/**
99- *
99+
100+ * Validate the Address provided. To do this we take the Address (or Base Address)
101+ * and compare it to an address (BaseAddress or RewardAddress) reconstructed from the
102+ * publicKey.
100103 * @param {Loader.Cardano.Address } checkAddress
101104 * @param {Loader.Cardano.PublicKey } publicKey
102- * @returns
105+ * @returns { {status: bool, msg?: string, code?: number} }
103106 */
104- function verifyAddress ( checkAddress , publicKey ) {
105- console . log ( "In Verify Address" ) ;
106-
107- const baseAddress = Loader . Cardano . BaseAddress . from_address ( checkAddress ) ;
108-
107+ const verifyAddress = ( checkAddress , publicKey ) => {
108+ let errorMsg = "" ;
109109 try {
110110 //reconstruct address
111111 const paymentKeyHash = publicKey . hash ( ) ;
@@ -116,26 +116,37 @@ function verifyAddress(checkAddress, publicKey) {
116116 Loader . Cardano . StakeCredential . from_keyhash ( stakeKeyHash )
117117 ) ;
118118
119- if (
120- checkAddress . to_bech32 ( ) !== reconstructedAddress . to_address ( ) . to_bech32 ( )
121- ) {
122- return {
123- verified : false ,
124- code : 1 ,
125- message :
126- "Check address does not match Reconstructed Address (Public Key is not the correct key for this Address)" ,
127- } ;
128- }
129-
119+ const status = checkAddress . to_bech32 ( ) === reconstructedAddress . to_address ( ) . to_bech32 ( ) ;
130120 return {
131- verified : true ,
121+ status,
122+ msg : status ? "Valid Address" : "Base Address does not validate to Reconstructed address" ,
123+ code : 1
132124 } ;
133-
134125 } catch ( e ) {
126+ errorMsg += ` ${ e . message } `
127+ }
128+
129+ try {
130+ const stakeKeyHash = checkAddress . hash ( ) ;
131+ const reconstructedAddress = RewardAddress . new (
132+ checkAddress . network_id ( ) ,
133+ StakeCredential . from_keyhash ( stakeKeyHash )
134+ ) ;
135+
136+ const status = checkAddress . to_bech32 ( ) === reconstructedAddress . to_address ( ) . to_bech32 ( ) ;
135137 return {
136- verified : false ,
137- code : 3 ,
138- message : e . message ,
138+ status ,
139+ msg : status ? "Valid Address" : "Address does not validate to Reconstructed address" ,
140+ code : 1
139141 } ;
142+
143+ } catch ( e ) {
144+ errorMsg += ` ${ e . message } `
140145 }
141- }
146+
147+ return {
148+ status : false ,
149+ msg : `Error: ${ errorMsg } ` ,
150+ code : 3
151+ } ;
152+ } ;
0 commit comments