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

[WASM] Fixed CoinType.deriveAddressFromPublicKeyAndDerivation #3271

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion wasm/src/CoinTypeExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ namespace TW::Wasm {
auto CoinTypeExt::deriveAddressFromPublicKey(TWCoinType coin, WasmPublicKey* publicKey) {
return TWStringToStd(TWCoinTypeDeriveAddressFromPublicKey(coin, publicKey->instance));
}
auto CoinTypeExt::deriveAddressFromPublicKeyAndDerivation(TWCoinType coin, WasmPublicKey* publicKey, TWDerivation derivation) {
return TWStringToStd(TWCoinTypeDeriveAddressFromPublicKeyAndDerivation(coin, publicKey->instance, derivation));
}

EMSCRIPTEN_BINDINGS(Wasm_CoinTypeExt) {
class_<CoinTypeExt>("CoinTypeExt")
Expand All @@ -92,6 +95,7 @@ namespace TW::Wasm {
.class_function("derivationPath", &CoinTypeExt::derivationPath)
.class_function("derivationPathWithDerivation", &CoinTypeExt::derivationPathWithDerivation)
.class_function("deriveAddress", &CoinTypeExt::deriveAddress, allow_raw_pointers())
.class_function("deriveAddressFromPublicKey", &CoinTypeExt::deriveAddressFromPublicKey, allow_raw_pointers());
.class_function("deriveAddressFromPublicKey", &CoinTypeExt::deriveAddressFromPublicKey, allow_raw_pointers())
.class_function("deriveAddressFromPublicKeyAndDerivation", &CoinTypeExt::deriveAddressFromPublicKeyAndDerivation, allow_raw_pointers());
};
}
1 change: 1 addition & 0 deletions wasm/src/CoinTypeExt.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ namespace TW::Wasm {
static auto derivationPathWithDerivation(TWCoinType coin, TWDerivation derivation);
static auto deriveAddress(TWCoinType coin, WasmPrivateKey* privateKey);
static auto deriveAddressFromPublicKey(TWCoinType coin, WasmPublicKey* publicKey);
static auto deriveAddressFromPublicKeyAndDerivation(TWCoinType coin, WasmPublicKey* publicKey, TWDerivation derivation);
};
}
34 changes: 31 additions & 3 deletions wasm/tests/CoinType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { assert } from "chai";

describe("CoinType", () => {
it("test raw value", () => {
const { CoinType, CoinTypeExt, Blockchain, Purpose, Curve, Derivation, PrivateKey, HexCoding } = globalThis.core;
const { CoinType } = globalThis.core;

assert.equal(CoinType.bitcoin.value, 0);
assert.equal(CoinType.litecoin.value, 2);
Expand All @@ -18,15 +18,43 @@ describe("CoinType", () => {
assert.equal(CoinType.binance.value, 714);
assert.equal(CoinType.cosmos.value, 118);
assert.equal(CoinType.solana.value, 501);
});

it("test CoinTypeExt methods", () => {
const { CoinType, CoinTypeExt, Blockchain, Purpose, Curve, Derivation } = globalThis.core;

assert.equal(CoinTypeExt.blockchain(CoinType.solana), Blockchain.solana);
assert.equal(CoinTypeExt.purpose(CoinType.solana), Purpose.bip44);
assert.equal(CoinTypeExt.curve(CoinType.solana), Curve.ed25519);
assert.isTrue(CoinTypeExt.validate(CoinType.solana, "Bxp8yhH9zNwxyE4UqxP7a7hgJ5xTZfxNNft7YJJ2VRjT"))
assert.equal(CoinTypeExt.derivationPath(CoinType.solana), "m/44'/501'/0'");
assert.equal(CoinTypeExt.derivationPathWithDerivation(CoinType.solana, Derivation.solanaSolana), "m/44'/501'/0'/0'");
let data = HexCoding.decode("8778cc93c6596387e751d2dc693bbd93e434bd233bc5b68a826c56131821cb63")
});

it("test deriveAddress", () => {
const { CoinType, CoinTypeExt, PrivateKey, HexCoding } = globalThis.core;

const data = HexCoding.decode("8778cc93c6596387e751d2dc693bbd93e434bd233bc5b68a826c56131821cb63");
const key = PrivateKey.createWithData(data);
let addr = CoinTypeExt.deriveAddress(CoinType.solana, key);
const addr = CoinTypeExt.deriveAddress(CoinType.solana, key);
assert.equal(addr, "7v91N7iZ9mNicL8WfG6cgSCKyRXydQjLh6UYBWwm6y1Q")
});

it("test deriveAddressFromPublicKey", () => {
const { CoinType, CoinTypeExt, PublicKey, PublicKeyType, HexCoding } = globalThis.core;

const data = HexCoding.decode("044516c4aa5352035e1bb5be132694e1389a4ac37d32e5e717d35ee4c4dfab513226a9d14ea37a55962ad3644a08e2ce551b4495beabb9b09e688c7b92eba18acc");
const key = PublicKey.createWithData(data, PublicKeyType.secp256k1Extended);
const addr = CoinTypeExt.deriveAddressFromPublicKey(CoinType.ethereum, key);
assert.equal(addr, "0x996891c410FB76C19DBA72C6f6cEFF2d9DD069b1");
});

it("test deriveAddressFromPublicKeyAndDerivation", () => {
const { CoinType, CoinTypeExt, Derivation, PublicKey, PublicKeyType, HexCoding } = globalThis.core;

const data = HexCoding.decode("0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798");
const key = PublicKey.createWithData(data, PublicKeyType.secp256k1);
const addr = CoinTypeExt.deriveAddressFromPublicKeyAndDerivation(CoinType.bitcoin, key, Derivation.bitcoinSegwit);
assert.equal(addr, "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4");
});
});
Loading