Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fromSeed not importing correctly #1368

Open
DerekfromMadison opened this issue Jan 27, 2025 · 4 comments
Open

fromSeed not importing correctly #1368

DerekfromMadison opened this issue Jan 27, 2025 · 4 comments
Labels
defect Suspected defect such as a bug or regression

Comments

@DerekfromMadison
Copy link

What version were you using?

Possibly a problem for Curve keys only. Trying to import a seed string using fromSeed() does not generate the same public key.

  1. Generate a seed and public key using the Rust library
{
  "public_key": "XDEGU33QHCIXEL4QKW37BICBUJDDL2IFVSKTVUQIEO3JVBZ2AW7JE66A",
  "seed": "SXAIDJ4BO5XTRKA6FZ2PAIWPWAEMS33YHCAAADNECY4YRCM2XXKGAUDBFM",
}
  1. Import the seed into using the JS function fromSeed()
  const rustSeedString = 'SXAIDJ4BO5XTRKA6FZ2PAIWPWAEMS33YHCAAADNECY4YRCM2XXKGAUDBFM';
  const kp = fromSeed(Buffer.from(rustSeedString));
  console.log(kp.getPublicKey());
  //XB34B373UKSFV5FMDSY6RWAV26IEE3J465QF6J4DFNWEUX4UQI2TMLDW

The resultant public key does not match.

What environment was the server running in?

LInux - vscode

Is this defect reproducible?

generate rust key

        let keypair_type = KeyPairType::Curve; 
        let kp = KeyPair::new(keypair_type.clone()); 
        let seed = kp.seed().unwrap();
        let public_key = kp.public_key();
        println!("Seed: {}", seed);
        println!("Public Key: {}", public_key);

use resultant key in JS

  const rustSeedString = 'PUT_KEY_HERE';
  const kp = fromSeed(Buffer.from(rustSeedString));
  console.log(kp.getPublicKey());

Given the capability you are leveraging, describe your expectation?

Public keys should match

Given the expectation, what is the defect you are observing?

Public keys don't match

@DerekfromMadison DerekfromMadison added the defect Suspected defect such as a bug or regression label Jan 27, 2025
@aricart
Copy link
Member

aricart commented Jan 29, 2025

This looks to be an issue with rust?

Deno.test("curve - from seed", () => {
  const fromRust = "SXAIDJ4BO5XTRKA6FZ2PAIWPWAEMS33YHCAAADNECY4YRCM2XXKGAUDBFM"
  console.log(fromRust);
  const rckp = fromSeed(new TextEncoder().encode(fromRust));
  console.log(rckp.getPublicKey());
})
 deno test -A --filter "curve - from seed"
curve - from seed ...
------- post-test output -------
SXAIDJ4BO5XTRKA6FZ2PAIWPWAEMS33YHCAAADNECY4YRCM2XXKGAUDBFM
XB34B373UKSFV5FMDSY6RWAV26IEE3J465QF6J4DFNWEUX4UQI2TMLDW
----- post-test output end -----
curve - from seed ... ok (4ms)

ok | 1 passed | 0 failed | 93 filtered out (79ms)

Similar code, but this time from the go library:

func Test_FromRust(t *testing.T) {
	k := "SXAIDJ4BO5XTRKA6FZ2PAIWPWAEMS33YHCAAADNECY4YRCM2XXKGAUDBFM"
	t.Log(k)
	ck, err := FromCurveSeed([]byte(k))
	if err != nil {
		t.Fatal(err)
	}
	cp, err := ck.PublicKey()
	if err != nil {
		t.Fatal(err)
	}
	t.Log(cp)
}
}
=== RUN   Test_FromRust
    nkeys_test.go:734: SXAIDJ4BO5XTRKA6FZ2PAIWPWAEMS33YHCAAADNECY4YRCM2XXKGAUDBFM
    nkeys_test.go:743: XB34B373UKSFV5FMDSY6RWAV26IEE3J465QF6J4DFNWEUX4UQI2TMLDW
--- PASS: Test_FromRust (0.00s)
PASS

@aricart aricart transferred this issue from nats-io/nkeys.js Jan 29, 2025
@aricart
Copy link
Member

aricart commented Jan 29, 2025

@Jarema - not sure if this creates an encoding issue, but the serialization of the public key in go/javascript match.

@Jarema
Copy link
Member

Jarema commented Feb 13, 2025

I think this is an issue with https://github.com/wasmCloud/nkeys

@Jarema
Copy link
Member

Jarema commented Feb 13, 2025

I will dig it bit later, but if you use XKey module, it will work as expected:

    #[test]
    fn roundtrip_encoding_go() {
        let kp =
            XKey::from_seed("SXAIDJ4BO5XTRKA6FZ2PAIWPWAEMS33YHCAAADNECY4YRCM2XXKGAUDBFM").unwrap();
        let pk = "XB34B373UKSFV5FMDSY6RWAV26IEE3J465QF6J4DFNWEUX4UQI2TMLDW";
        let seed = kp.seed().unwrap();
        let public_key = kp.public_key();
        println!("XX Seed: {}", seed);
        println!("Public Key:\n{}", public_key);
        println!("expected:\n{}", pk);
        assert_eq!(public_key, pk);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect Suspected defect such as a bug or regression
Projects
None yet
Development

No branches or pull requests

3 participants