Skip to content

Commit d209c8c

Browse files
Merge branch 'main' into feature/cip30
2 parents b457038 + 33303ca commit d209c8c

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Web3Token from 'web3-cardano-token/dist/browser';
1919

2020
// Connection to Nami wallet
2121
const cardano = window.cardano;
22-
await cardano.enable();
22+
await cardano.nami.enable();
2323

2424
// getting address from which we will sign message
2525
const address = (await cardano.getUsedAddresses())[0];

src/lib/sign.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export const sign = async (signer, expires_in = '1d', body = {}) => {
2525
} else {
2626
throw new Error('"signer" argument should be a function that returns a signature eg: "msg => web3.eth.personal.sign(msg, <YOUR_ADDRESS>)"')
2727
}
28+
29+
if (typeof(signature) === "object") {
30+
signature = signature.signature
31+
}
2832

2933
const {signature, key} = COSESign1Message;
3034

@@ -67,4 +71,4 @@ const buildMessage = data => {
6771
message.push(`${key}: ${data[key]}`)
6872
}
6973
return message.join('\n');
70-
};
74+
};

src/lib/verify.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)