Skip to content

Commit

Permalink
fix: properly convert uint8arrays to strings (ArkEcosystemArchive#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsANameToo authored Jun 23, 2020
1 parent d3b4df6 commit 6983faa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
29 changes: 22 additions & 7 deletions __tests__/unit/specs/mixins/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,28 @@ describe("Mixins > Crypto", () => {
});
});

describe("stringToHex", () => {
it("should change a string into its hex representation", () => {
expect(wrapper.vm.stringToHex("abcdefghijklmnopqrstuvwxyz")).toEqual("6162636465666768696a6b6c6d6e6f707172737475767778797a");
expect(wrapper.vm.stringToHex("this is some string")).toEqual("7468697320697320736f6d6520737472696e67");
expect(wrapper.vm.stringToHex("\u0011unicode\u0011works")).toEqual("11756e69636f646511776f726b73");
expect(wrapper.vm.stringToHex("18470234asdfa{}[];',./")).toEqual("313834373032333461736466617b7d5b5d3b272c2e2f");
});
// TODO: enable once the Buffer issue is solved
describe("addressFromPublicKey", () => {
it.skip("should generate a correct address from a public key", () => {
expect(wrapper.vm.addressFromPublicKey("02dff0fdf2ca1ac13a08627e6ca1821b72fb07b50e6b4f73042ca1ac6c26108e82")).toEqual("ANkHGk5uZqNrKFNY5jtd4A88zzFR3LnJbe");
})
});

// TODO: enable once the Buffer issue is solved
describe("addressFromMultiSignatureAsset", () => {
it.skip("should generate a correct address from a multisignature asset", () => {
expect(wrapper.vm.addressFromMultiSignatureAsset({
"publicKeys": [
"02fb3def2593a00c5b84620addf28ff21bac452bd71a37d4d8e24f301683a81b56",
"021b358bdb2ff2fe20fd0b3ff2d1806e99c45864a1bff2adc200c407405dad6ee6",
"02bc9f661fcc8abca65fe9aff4614036867b7fdcc5730085ccc5cb854664d0194b",
"03b050073621b9b5caec9461d44d6bcf21a858c47dd88230ce723e25c1bc75c219",
"03c44c6b6cc9893ae21ca606712fd0f6f03c41ce81c4f6ce5a640f4b0b82ec1ce0"
],
"min": 3
}
)).toEqual("AXxNbmaKspf9UqgKhfTRDdn89NidP2gXWh");
});
};

});
10 changes: 4 additions & 6 deletions src/mixins/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ const privateKeyFromPassphrase = (passphrase: string): { publicKey: string; priv
const privateKey: Buffer = Buffer.from(sync(passphrase), "hex");

return {
publicKey: secp256k1.publicKeyCreate(privateKey, true).toString("hex"),
publicKey: stringToHex(secp256k1.publicKeyCreate(privateKey, true)),
// @ts-ignore
privateKey: privateKey.toString("hex"),
};
};

const stringToHex = (text: string): string => Buffer.from(text).toString("hex");

const publicKeyFromPassphrase = (passphrase: string): string => privateKeyFromPassphrase(passphrase).publicKey;

const addressFromPublicKey = (publicKey: string): string => {
Expand Down Expand Up @@ -57,11 +59,7 @@ export default {
const minKey: string = publicKeyFromPassphrase(numberToHex(min));
const keys: Buffer[] = [minKey, ...publicKeys].map((p) => Buffer.from(p, "hex"));

return addressFromPublicKey(secp256k1.publicKeyCombine(keys, true).toString("hex"));
},

stringToHex(text: string): string {
return Buffer.from(text).toString("hex");
return addressFromPublicKey(stringToHex(secp256k1.publicKeyCombine(keys, true)));
},
},
};

0 comments on commit 6983faa

Please sign in to comment.