QuantumMechanics / NEM-sdk

NEM Developer Kit for Node.js and the browser
MIT License
137 stars 82 forks source link

Send publicKey in HEX format #63

Closed AlexeyKorkoza closed 2 years ago

AlexeyKorkoza commented 5 years ago

There is a critical error in the package that I have found several days ago. NEM addresses are generated incorrect.

Below you can see a code that reproduce a bug in the package:

const nemSdk = require('./src');

const countAddress = 10;
let i = 0;
while (i < countAddress) {
    const rBytes = nemSdk.default.crypto.nacl.randomBytes(32);
    const privateKey = nemSdk.default.utils.convert.ua2hex(rBytes);
    const keyPair = nemSdk.default.crypto.keyPair.create(privateKey);
    const { publicKey } = keyPair;
    const address = nemSdk.default.model.address.toAddress(publicKey, 104);
    console.log('address', address);
    i++;
}

And also I attach a screenshot where you can see the same generated addresses: screenshot_204

I had to spend some time to find the error and I could. I decided to commit the fix because address might generate correct, didn't me ?

I attach another screenshot where you can see that XEM address are generated correct: screenshot_205

If you don't believe me then you can start the script to make sure.

roccomuso commented 5 years ago

is this confirmed to be a bug?

AlexeyKorkoza commented 5 years ago

Who must confirm the bug ?

evias commented 5 years ago

I retract my past comment.

@roccomuso its your code that is wrong - you must pass publicKey.toString() instead of directly the publicKey to toAddress.

evias commented 5 years ago

following code gives correct results, which is just changing your publicKey parameter:

(function() {
const countAddress = 10;
let i = 0;
while (i < countAddress) {
    const rBytes = nemSdk.crypto.nacl.randomBytes(32);
    const privateKey = nemSdk.utils.convert.ua2hex(rBytes);
    const keyPair = nemSdk.crypto.keyPair.create(privateKey);
    const { publicKey } = keyPair;
    const address = nemSdk.model.address.toAddress(publicKey.toString(), 104);
    console.log('public key', publicKey.toString(), 'address', address);
    i++;
}
})();

public key a8cf2ce17077ebf300e64efa8c0279197b4fd36ac277e856d2732c1f59072f94 address NC3CIST6KP2WNAJ5N4LEMPN56KDRKNE3ZAT3MXLG
public key 8234a52605e2469ab16001db3040942b7930d48348ac563a15a4d21b076d27a0 address NDKLFI2WJMS3YNHDHTEOTPIVCC4DXZZ6KF6KNLMO
public key b41b284182e1a7b5c451045bc262085721ca16fd06def2950711e9ce706316e7 address NCT37DBFONBOKU2WI46YRAYZ4PZNLYXMEKEUZDFM
public key fe7a0eeb69a5761088ed2763ba5b3e0e56c1d6d725929e3929dad8720cc8e870 address NCJ6RYHTJB4EX47MTFJVN5ZX3FOYTFYVTSDAYJBO
public key 1827a33deadaced6e20d21220891664c37330b8443eb3f3e61adb0ee0fe1b56d address ND7NUPPJCFF2TXBD62JRMDEOL5Q7NE4LFSTDXHAE
public key b34099bb9d163b4d4b38703268edcbcde7b846ab83a46cdfca5309dac93c2f05 address ND3KZC5HXPMIC76EBRY5GUWZ5UOBEVCKZ6TN6UIX
public key c34118b086aa9a723d11434bed7e8d6c490cdad6ef2ad0127ebfaed40e5a615e address ND2QJVZKUHLMN42YCKPFNKD7WS5HZYRCYQ4ZSF25
public key 366500388d99e723e007d100ef6221d8573395a345a74a33e165c1591bfedf5c address NDXTGTJ2PEC3XWPFLKEH3N6SVRPJ4FYMURXHP63J
public key cb77643d79bd042ba965037e69ef67c76a416af687a84fdfdddf8c4cd656f2ad address NA2YW7ABISNTD4GWJZJ2IS23HHSES3HNZJW77GMX
public key 6b1011722395d22edb26d6e845ae14f023db4e67f8a01f8214ca903e5e602841 address NCKDAM2FRCUTRFINPJISMEW33ENGUJPK7JIBD4RU