<p>The solution I found was to change this line...</p>
<pre><code>    const signerStakeAddrBech32 = RewardAddress.from_address(address).to_address().to_bech32();
</code></pre>
<p>...by this one:</p>
<pre><code>    const signerStakeAddrBech32 = RewardAddress.from_address(Address.from_bytes(rewardAddressBytes)).to_address().to_bech32();
</code></pre>
<p>I built <code>rewardAddressBytes</code> from <code>address</code>, as follows:</p>
<pre><code>    const baseAddress = BaseAddress.from_address(address);
    const stakeCred = baseAddress.stake_cred();
    const rewardAddressBytes = new Uint8Array(29);
    rewardAddressBytes.set([0xe1], 0);
    rewardAddressBytes.set(stakeCred.to_bytes().slice(4, 32), 1);
</code></pre>

CC BY-SA 4.0
2024-05-23T01:39:49.563Z
2024-05-23T01:39:49.563Z
Falcon Stakepool
<p>I have the following code to verify the signature of a message using CIP-8:</p>
<pre><code>    const decoded = COSESign1.from_bytes( Buffer.from(signedData.signature, &quot;hex&quot;) );
    const headermap = decoded.headers().protected().deserialized_headers();
    const addressHex = Buffer.from( headermap.header( Label.new_text(&quot;address&quot;) ).to_bytes() )
        .toString(&quot;hex&quot;)
        .substring(4);
    const address = Address.from_bytes( Buffer.from(addressHex, &quot;hex&quot;) );

    const key = COSEKey.from_bytes( Buffer.from(signedData.key, &quot;hex&quot;) );
    const pubKeyBytes = key.header( Label.new_int( Int.new_negative(BigNum.from_str(&quot;2&quot;)) ) ).as_bytes();
    const publicKey = PublicKey.from_bytes(pubKeyBytes);

    const payload = decoded.payload();
    const signature = Ed25519Signature.from_bytes(decoded.signature());
    const receivedData = decoded.signed_data().to_bytes();

    const signerStakeAddrBech32 = RewardAddress.from_address(address).to_address().to_bech32();
    console.log(signerStakeAddrBech32);
    const utf8Payload = Buffer.from(payload).toString(&quot;utf8&quot;);
    const expectedPayload = `account: ${signerStakeAddrBech32}`;

    // verify:
    const isVerified = publicKey.verify(receivedData, signature);
    const payloadAsExpected = utf8Payload == expectedPayload;

    const isValidSignature = isVerified &amp;&amp; payloadAsExpected;
</code></pre>
<p>The problem is that I am getting <code>TypeError: Cannot read properties of undefined (reading 'to_address')</code> in this line:</p>
<pre><code>const signerStakeAddrBech32 = RewardAddress.from_address(address).to_address().to_bech32();
</code></pre>
<p>It looks like <code>RewardAddress.from_address(address)</code> is <code>undefined</code>, and that's why the line fails.</p>
<p>If I <code>console.log(address.to_bech32())</code> I get <code>addr_test1qpkmzcspgd4uezwvve3zddf05nz45v3gsefvf9zxqrpgdxnet4zhaqq30fefre0pgj6fdlgrqpv4753tvmyfrenvethsrxtf6x</code>, which is in fact the address I generated to do this test.</p>
<p>Furthermore, if I check the exact function that the <code>cardano-serialization-lib</code> is executing, I see this:</p>
<pre><code>    /**
    * @param {Address} addr
    * @returns {RewardAddress | undefined}
    */
    static from_address(addr) {
        _assertClass(addr, Address);
        const ret = wasm.rewardaddress_from_address(addr.__wbg_ptr);
        return ret === 0 ? undefined : RewardAddress.__wrap(ret);
    }
</code></pre>
<p>So question would be: what is the error here that doesn't let me get the right <code>RewardAddress</code>? Or why is this function above returning <code>ret === 0</code>?</p>

<wallet><cardano-serialization-lib><cardano-address><javascript><message-signing>
How to get a RewardAddress from an address using cardano-serialization-lib?
# How to get a RewardAddress from an address using cardano-serialization-lib?

(Asked by: username_0 on 2024-05-22)

I have the following code to verify the signature of a message using CIP-8:
    const decoded = COSESign1.from_bytes( Buffer.from(signedData.signature, "hex") );
    const headermap = decoded.headers().protected().deserialized_headers();
    const addressHex = Buffer.from( headermap.header( Label.new_text("address") ).to_bytes() )
        .toString("hex")
        .substring(4);
    const address = Address.from_bytes( Buffer.from(addressHex, "hex") );

    const key = COSEKey.from_bytes( Buffer.from(signedData.key, "hex") );
    const pubKeyBytes = key.header( Label.new_int( Int.new_negative(BigNum.from_str("2")) ) ).as_bytes();
    const publicKey = PublicKey.from_bytes(pubKeyBytes);

    const payload = decoded.payload();
    const signature = Ed25519Signature.from_bytes(decoded.signature());
    const receivedData = decoded.signed_data().to_bytes();

    const signerStakeAddrBech32 = RewardAddress.from_address(address).to_address().to_bech32();
    console.log(signerStakeAddrBech32);
    const utf8Payload = Buffer.from(payload).toString("utf8");
    const expectedPayload = `account: ${signerStakeAddrBech32}`;

    // verify:
    const isVerified = publicKey.verify(receivedData, signature);
    const payloadAsExpected = utf8Payload == expectedPayload;

    const isValidSignature = isVerified && payloadAsExpected;

The problem is that I am getting TypeError: Cannot read properties of undefined (reading 'to_address') in this line:
const signerStakeAddrBech32 = RewardAddress.from_address(address).to_address().to_bech32();

It looks like RewardAddress.from_address(address) is undefined, and that's why the line fails.
If I console.log(address.to_bech32()) I get addr_test1qpkmzcspgd4uezwvve3zddf05nz45v3gsefvf9zxqrpgdxnet4zhaqq30fefre0pgj6fdlgrqpv4753tvmyfrenvethsrxtf6x, which is in fact the address I generated to do this test.
Furthermore, if I check the exact function that the cardano-serialization-lib is executing, I see this:
    /**
    * @param {Address} addr
    * @returns {RewardAddress | undefined}
    */
    static from_address(addr) {
        _assertClass(addr, Address);
        const ret = wasm.rewardaddress_from_address(addr.__wbg_ptr);
        return ret === 0 ? undefined : RewardAddress.__wrap(ret);
    }

So question would be: what is the error here that doesn't let me get the right RewardAddress? Or why is this function above returning ret === 0?

**Tags:** wallet, cardano-serialization-lib, cardano-address, javascript, message-signing

---

## 1 answers

### Answer by username_0 on 2024-05-23. Score: 0

The solution I found was to change this line...
    const signerStakeAddrBech32 = RewardAddress.from_address(address).to_address().to_bech32();

...by this one:
    const signerStakeAddrBech32 = RewardAddress.from_address(Address.from_bytes(rewardAddressBytes)).to_address().to_bech32();

I built rewardAddressBytes from address, as follows:
    const baseAddress = BaseAddress.from_address(address);
    const stakeCred = baseAddress.stake_cred();
    const rewardAddressBytes = new Uint8Array(29);
    rewardAddressBytes.set([0xe1], 0);
    rewardAddressBytes.set(stakeCred.to_bytes().slice(4, 32), 1);


---

