MystenLabs / sui

Sui, a next-generation smart contract platform with high throughput, low latency, and an asset-oriented programming model powered by the Move programming language
https://sui.io
Apache License 2.0
6.18k stars 11.18k forks source link

[Bug] Ed25519Keypair.toSuiAddress does not match sui client. #3640

Closed jamescarter-le closed 2 years ago

jamescarter-le commented 2 years ago

Steps to Reproduce Issue

Ed25519Keypair.getPublicKey().toSuiAddress() does not generate the appropriate Sui address matching the client.

sui client addresses

Showing 5 results.
0x4d0d03b6bf0e1a794203ac4652a2554c6efdf11a
sui keytool list

                Sui Address                 |              Public Key (Base64)
-------------------------------------------------------------------------------------------
 0x4d0d03b6bf0e1a794203ac4652a2554c6efdf11a | 160QtV/5pPE2KFVC7agRpdk4AORt6QhiE5due6VYcy0=
sui.keystore contents

[
  "xVs6E0Cf348L3QF6t0LAbLVDKjbb3zVe17R/mwv/3ZXXrRC1X/mk8TYoVULtqBGl2TgA5G3pCGITl257pVhzLQ==",
Sui Typescript SDK Usage:

const keyBuffer = Buffer.from("xVs6E0Cf348L3QF6t0LAbLVDKjbb3zVe17R/mwv/3ZXXrRC1X/mk8TYoVULtqBGl2TgA5G3pCGITl257pVhzLQ==", 'base64');
const keypair = Ed25519Keypair.fromSecretKey(keyBuffer);
console.log(keypair.getPublicKey().toBase64());
console.log('0x' + keypair.getPublicKey().toSuiAddress());

Output:
160QtV/5pPE2KFVC7agRpdk4AORt6QhiE5due6VYcy0=
0x1fb9536b4d1197fbe953c6ca004ab21ebeb1fb51

Expected Result

Expect that Ed25519Keypair when provided with the private key from sui.keystore should generate the same public key and Sui address as sui client addresses:

Expected PubKey: 160QtV/5pPE2KFVC7agRpdk4AORt6QhiE5due6VYcy0= Output PubKey: 160QtV/5pPE2KFVC7agRpdk4AORt6QhiE5due6VYcy0=Expected Address: 0x4d0d03b6bf0e1a794203ac4652a2554c6efdf11a Output Address: 0x1fb9536b4d1197fbe953c6ca004ab21ebeb1fb51 🔴

System Information

jamescarter-le commented 2 years ago

Committed PR with publickey.tests.ts corrected to demonstrate this issue. https://github.com/MystenLabs/sui/pull/3650

punwai commented 2 years ago

Hi James, we have recently changed our address derivation to begin a shift towards crypto agility (we are now adding bit flags to public keys before hashing them to get address). I was wondering if your binary is up to date? I was not able to reproduce this issue.

punwai commented 2 years ago

If you want to quickly test this without reinstalling the binaries, you can do (in the sui repo):

  1. cargo build
  2. ./target/debug/sui keytool list
jamescarter-le commented 2 years ago

@punwai

Thank you for your time looking into this issue. The problem as you mentioned in Discord, is that my terminal was not picking up the sui executable from the target folder, and instead the globally installed sui package.

My issue is that normally the Windows cmd prompt gives preference to files in the working directory (hence I was executing these from the target/release folder, whilst their new Windows Terminal uses Powershell as the shell, and this does treat sui and .\sui equivalently.

Thus I was running the outdated globally installed sui and not my main branch built sui executable. The output of .\sui and sui are not equivalent, and are down to the new type flag added to SuiAddress.

Apologies for wasting your time digging into this issue.