Skip to content

Commit

Permalink
fix(derivation): Fix Swift, Kotlin, JS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Aug 1, 2024
1 parent c8b4c2e commit 02abfb4
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ class TestCoinType {
fun testDerivationPath() {
var res = CoinType.createFromValue(CoinType.BITCOIN.value()).derivationPath().toString()
assertEquals(res, "m/84'/0'/0'/0/0")
res = CoinType.createFromValue(CoinType.BITCOIN.value()).derivationPathWithDerivation(Derivation.LEGACY).toString()
res = CoinType.createFromValue(CoinType.BITCOIN.value()).derivationPathWithDerivation(Derivation.BITCOINLEGACY).toString()
assertEquals(res, "m/44'/0'/0'/0/0")
res = CoinType.createFromValue(CoinType.SOLANA.value()).derivationPathWithDerivation(Derivation.SOLANA).toString()
res = CoinType.createFromValue(CoinType.SOLANA.value()).derivationPathWithDerivation(Derivation.SOLANASOLANA).toString()
assertEquals(res, "m/44'/501'/0'/0'")
}

@Test
fun testDeriveAddressFromPublicKeyAndDerivation() {
val publicKey = PublicKey("0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798".toHexByteArray(), PublicKeyType.SECP256K1)

val address = CoinType.BITCOIN.deriveAddressFromPublicKeyAndDerivation(publicKey, Derivation.SEGWIT)
val address = CoinType.BITCOIN.deriveAddressFromPublicKeyAndDerivation(publicKey, Derivation.BITCOINSEGWIT)
assertEquals(address, "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class TestAnyAddress {
fun testCreateWithPublicKeyDerivation() {
val coin = CoinType.BITCOIN
val pubkey = PublicKey(any_address_test_pubkey.toHexByteArray(), PublicKeyType.SECP256K1)
val address1 = AnyAddress(pubkey, coin, Derivation.SEGWIT)
val address1 = AnyAddress(pubkey, coin, Derivation.BITCOINSEGWIT)
assertEquals(address1.description(), any_address_test_address)

val address2 = AnyAddress(pubkey, coin, Derivation.LEGACY)
val address2 = AnyAddress(pubkey, coin, Derivation.BITCOINLEGACY)
assertEquals(address2.description(), "1JvRfEQFv5q5qy9uTSAezH7kVQf4hqnHXx")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ class TestHDWallet {
val coin = CoinType.BITCOIN
val wallet = HDWallet(words, password)

val key1 = wallet.getKeyDerivation(coin, Derivation.SEGWIT)
val key1 = wallet.getKeyDerivation(coin, Derivation.BITCOINSEGWIT)
assertEquals(key1.data().toHex(), "0x1901b5994f075af71397f65bd68a9fff8d3025d65f5a2c731cf90f5e259d6aac")

val key2 = wallet.getKeyDerivation(coin, Derivation.LEGACY)
val key2 = wallet.getKeyDerivation(coin, Derivation.BITCOINLEGACY)
assertEquals(key2.data().toHex(), "0x28071bf4e2b0340db41b807ed8a5514139e5d6427ff9d58dbd22b7ed187103a4")

val key3 = wallet.getKeyDerivation(coin, Derivation.TESTNET)
val key3 = wallet.getKeyDerivation(coin, Derivation.BITCOINTESTNET)
assertEquals(key3.data().toHex(), "0xca5845e1b43e3adf577b7f110b60596479425695005a594c88f9901c3afe864f")
}

Expand All @@ -137,13 +137,13 @@ class TestHDWallet {
val coin = CoinType.BITCOIN
val wallet = HDWallet(words, password)

val address1 = wallet.getAddressDerivation(coin, Derivation.SEGWIT)
val address1 = wallet.getAddressDerivation(coin, Derivation.BITCOINSEGWIT)
assertEquals(address1, "bc1qumwjg8danv2vm29lp5swdux4r60ezptzz7ce85")

val address2 = wallet.getAddressDerivation(coin, Derivation.LEGACY)
val address2 = wallet.getAddressDerivation(coin, Derivation.BITCOINLEGACY)
assertEquals(address2, "1PeUvjuxyf31aJKX6kCXuaqxhmG78ZUdL1")

val address3 = wallet.getAddressDerivation(coin, Derivation.TESTNET)
val address3 = wallet.getAddressDerivation(coin, Derivation.BITCOINTESTNET)
assertEquals(address3, "tb1qwgpxgwn33z3ke9s7q65l976pseh4edrzfmyvl0")
}

Expand Down
4 changes: 2 additions & 2 deletions swift/Tests/AnyAddressTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class AnyAddressTests: XCTestCase {
func testCreateWithPublicKeyDerivation() {
let coin = CoinType.bitcoin
let pubkey = PublicKey(data: Data(hexString: any_address_test_pubkey)!, type: .secp256k1)!
let address1 = AnyAddress(publicKey: pubkey, coin: coin, derivation: .segwit)
let address1 = AnyAddress(publicKey: pubkey, coin: coin, derivation: .bitcoinSegwit)
XCTAssertEqual(address1.description, any_address_test_address)

let address2 = AnyAddress(publicKey: pubkey, coin: coin, derivation: .legacy)
let address2 = AnyAddress(publicKey: pubkey, coin: coin, derivation: .bitcoinLegacy)
XCTAssertEqual(address2.description, "1JvRfEQFv5q5qy9uTSAezH7kVQf4hqnHXx")
}

Expand Down
10 changes: 5 additions & 5 deletions swift/Tests/CoinTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class CoinTypeTests: XCTestCase {

func testCoinDerivation() {
XCTAssertEqual(CoinType.bitcoin.derivationPath(), "m/84'/0'/0'/0/0")
XCTAssertEqual(CoinType.bitcoin.derivationPathWithDerivation(derivation: Derivation.legacy), "m/44'/0'/0'/0/0")
XCTAssertEqual(CoinType.solana.derivationPathWithDerivation(derivation: Derivation.solana), "m/44'/501'/0'/0'")
XCTAssertEqual(CoinType.bitcoin.derivationPathWithDerivation(derivation: Derivation.bitcoinLegacy), "m/44'/0'/0'/0/0")
XCTAssertEqual(CoinType.solana.derivationPathWithDerivation(derivation: Derivation.solanaSolana), "m/44'/501'/0'/0'")
}

func testDeriveAddressFromPublicKeyAndDerivation() {
let pkData = Data(hexString: "0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798")!
let publicKey = PublicKey(data: pkData, type: .secp256k1)!
let address = CoinType.bitcoin.deriveAddressFromPublicKeyAndDerivation(publicKey: publicKey, derivation: Derivation.segwit)

let address = CoinType.bitcoin.deriveAddressFromPublicKeyAndDerivation(publicKey: publicKey, derivation: Derivation.bitcoinSegwit)
XCTAssertEqual(address, "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4")
}
}
12 changes: 6 additions & 6 deletions swift/Tests/HDWalletTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ class HDWalletTests: XCTestCase {
let coin = CoinType.bitcoin
let wallet = HDWallet.test

let key1 = wallet.getKeyDerivation(coin: coin, derivation: .segwit)
let key1 = wallet.getKeyDerivation(coin: coin, derivation: .bitcoinSegwit)
XCTAssertEqual(key1.data.hexString, "1901b5994f075af71397f65bd68a9fff8d3025d65f5a2c731cf90f5e259d6aac")

let key2 = wallet.getKeyDerivation(coin: coin, derivation: .legacy)
let key2 = wallet.getKeyDerivation(coin: coin, derivation: .bitcoinLegacy)
XCTAssertEqual(key2.data.hexString, "28071bf4e2b0340db41b807ed8a5514139e5d6427ff9d58dbd22b7ed187103a4")

let key3 = wallet.getKeyDerivation(coin: coin, derivation: .testnet)
let key3 = wallet.getKeyDerivation(coin: coin, derivation: .bitcoinTestnet)
XCTAssertEqual(key3.data.hexString, "ca5845e1b43e3adf577b7f110b60596479425695005a594c88f9901c3afe864f")
}

Expand All @@ -114,13 +114,13 @@ class HDWalletTests: XCTestCase {
let coin = CoinType.bitcoin
let wallet = HDWallet.test

let address1 = wallet.getAddressDerivation(coin: coin, derivation: .segwit)
let address1 = wallet.getAddressDerivation(coin: coin, derivation: .bitcoinSegwit)
XCTAssertEqual(address1, "bc1qumwjg8danv2vm29lp5swdux4r60ezptzz7ce85")

let address2 = wallet.getAddressDerivation(coin: coin, derivation: .legacy)
let address2 = wallet.getAddressDerivation(coin: coin, derivation: .bitcoinLegacy)
XCTAssertEqual(address2, "1PeUvjuxyf31aJKX6kCXuaqxhmG78ZUdL1")

let address3 = wallet.getAddressDerivation(coin: coin, derivation: .testnet)
let address3 = wallet.getAddressDerivation(coin: coin, derivation: .bitcoinTestnet)
XCTAssertEqual(address3, "tb1qwgpxgwn33z3ke9s7q65l976pseh4edrzfmyvl0")
}

Expand Down
4 changes: 2 additions & 2 deletions swift/Tests/Keystore/KeyStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,15 @@ class KeyStoreTests: XCTestCase {
XCTAssertEqual(btc1.address, "bc1qturc268v0f2srjh4r2zu4t6zk4gdutqd5a6zny")
XCTAssertEqual(btc1.extendedPublicKey, "zpub6qbsWdbcKW9sC6shTKK4VEhfWvDCoWpfLnnVfYKHLHt31wKYUwH3aFDz4WLjZvjHZ5W4qVEyk37cRwzTbfrrT1Gnu8SgXawASnkdQ994atn")

let btc2 = try wallet.getAccount(password: password, coin: .bitcoin, derivation: .legacy)
let btc2 = try wallet.getAccount(password: password, coin: .bitcoin, derivation: .bitcoinLegacy)
XCTAssertEqual(btc2.address, "1NyRyFewhZcWMa9XCj3bBxSXPXyoSg8dKz")
XCTAssertEqual(btc2.extendedPublicKey, "xpub6CR52eaUuVb4kXAVyHC2i5ZuqJ37oWNPZFtjXaazFPXZD45DwWBYEBLdrF7fmCR9pgBuCA9Q57zZfyJjDUBDNtWkhWuGHNYKLgDHpqrHsxV")

let solana1 = try wallet.getAccount(password: password, coin: .solana, derivation: .default)
XCTAssertEqual(solana1.address, "HiipoCKL8hX2RVmJTz3vaLy34hS2zLhWWMkUWtw85TmZ")
XCTAssertEqual(solana1.derivationPath, "m/44'/501'/0'")

let solana2 = try wallet.getAccount(password: password, coin: .solana, derivation: .solana)
let solana2 = try wallet.getAccount(password: password, coin: .solana, derivation: .solanaSolana)
XCTAssertEqual(solana2.address, "CgWJeEWkiYqosy1ba7a3wn9HAQuHyK48xs3LM4SSDc1C")
XCTAssertEqual(solana2.derivationPath, "m/44'/501'/0'/0'")
}
Expand Down
4 changes: 2 additions & 2 deletions wasm/tests/CoinType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("CoinType", () => {
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.solana), "m/44'/501'/0'/0'");
assert.equal(CoinTypeExt.derivationPathWithDerivation(CoinType.solana, Derivation.solanaSolana), "m/44'/501'/0'/0'");
});

it("test deriveAddress", () => {
Expand All @@ -52,7 +52,7 @@ describe("CoinType", () => {

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

0 comments on commit 02abfb4

Please sign in to comment.