Skip to content

Commit

Permalink
Merge pull request #174 from semuxgo/bip32-ed25519
Browse files Browse the repository at this point in the history
Add BIP32-Ed25519 implementation
  • Loading branch information
semuxgo authored Jul 2, 2019
2 parents 5214af1 + 118e1e4 commit ceb6bac
Show file tree
Hide file tree
Showing 21 changed files with 523 additions and 291 deletions.
15 changes: 10 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
Expand All @@ -624,7 +624,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down Expand Up @@ -736,6 +736,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>

<!-- ED25519 -->
<dependency>
Expand Down Expand Up @@ -782,19 +787,19 @@
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>2.1</version>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.1</version>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.12.2</version>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/semux/core/TransactionResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import java.util.ArrayList;
import java.util.List;

import org.bouncycastle.util.encoders.Hex;
import org.ethereum.vm.DataWord;
import org.ethereum.vm.LogInfo;
import org.ethereum.vm.OpCode;
import org.ethereum.vm.program.InternalTransaction;
import org.semux.Network;
import org.semux.crypto.Hex;
import org.semux.util.Bytes;
import org.semux.util.SimpleDecoder;
import org.semux.util.SimpleEncoder;
Expand Down Expand Up @@ -419,7 +419,7 @@ public byte[] toBytesForMerkle() {

@Override
public String toString() {
return "TransactionResult [code=" + code + ", returnData=" + Hex.toHexString(returnData) + ", # logs="
return "TransactionResult [code=" + code + ", returnData=" + Hex.encode(returnData) + ", # logs="
+ logs.size() + "]";
}
}
16 changes: 8 additions & 8 deletions src/main/java/org/semux/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -689,17 +689,17 @@ public Key addAccountWithNextHdKey() {
requireHdWalletInitialized();

synchronized (accounts) {
HdKeyPair rootAddress = BIP_44.getRootAddressFromSeed(hdSeed, KeyVersion.MAINNET, CoinType.SEMUX);
HdKeyPair address = BIP_44.getAddress(rootAddress, nextHdAccountIndex++);
Key newKey = Key.fromRawPrivateKey(address.getPrivateKey().getPrivateKey());
HdKeyPair rootKey = BIP_44.getRootKeyPairFromSeed(hdSeed, KeyVersion.MAINNET, CoinType.SEMUX_SLIP10);
HdKeyPair key = BIP_44.getChildKeyPair(rootKey, nextHdAccountIndex++);
Key newKey = Key.fromRawPrivateKey(key.getPrivateKey().getKeyData());
ByteArray to = ByteArray.of(newKey.toAddress());

// put the accounts into
accounts.put(to, newKey);

// set a default alias
if (!aliases.containsKey(to)) {
setAddressAlias(newKey.toAddress(), getAliasFromPath(address.getPath()));
setAddressAlias(newKey.toAddress(), getAliasFromPath(key.getPath()));
}

return newKey;
Expand All @@ -723,17 +723,17 @@ public int scanForHdKeys(AccountState accountState) {
found++;
}

HdKeyPair rootAddress = BIP_44.getRootAddressFromSeed(hdSeed, getKeyVersion(network), CoinType.SEMUX);
HdKeyPair rootAddress = BIP_44.getRootKeyPairFromSeed(hdSeed, getKeyVersion(network), CoinType.SEMUX_SLIP10);

nextHdAccountIndex = 0;

int start = nextHdAccountIndex;
int endIndex = start + MAX_HD_WALLET_SCAN_AHEAD;

for (int i = start; i < endIndex; i++) {
HdKeyPair address = BIP_44.getAddress(rootAddress, i);
HdKeyPair childKey = BIP_44.getChildKeyPair(rootAddress, i);

Key key = Key.fromRawPrivateKey(address.getPrivateKey().getPrivateKey());
Key key = Key.fromRawPrivateKey(childKey.getPrivateKey().getKeyData());
boolean isUsedAccount = isUsedAccount(accountState, key.toAddress());

ByteArray to = ByteArray.of(key.toAddress());
Expand All @@ -743,7 +743,7 @@ public int scanForHdKeys(AccountState accountState) {
endIndex += MAX_HD_WALLET_SCAN_AHEAD;
if (addAccount(key)) {
if (!aliases.containsKey(to)) {
aliases.put(to, address.getPath());
aliases.put(to, childKey.getPath());
}
found++;
}
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/org/semux/crypto/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import java.util.Arrays;
import java.util.Collection;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.semux.crypto.cache.PublicKeyCache;
import org.semux.util.Bytes;
import org.semux.util.SystemUtil;
Expand Down Expand Up @@ -346,20 +344,12 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;

Signature signature = (Signature) o;

return new EqualsBuilder()
.append(s, signature.s)
.append(a, signature.a)
.isEquals();
return Arrays.equals(toBytes(), ((Signature) o).toBytes());
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(s)
.append(a)
.toHashCode();
return Arrays.hashCode(toBytes());
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/semux/crypto/bip32/CoinType.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
public enum CoinType {
BITCOIN(Scheme.BIP32, 0, false),

SEMUX(Scheme.SLIP10_ED25519, 7562605, true);
SEMUX_SLIP10(Scheme.SLIP10_ED25519, 7562605, true),

SEMUX(Scheme.BIP32_ED25519, 7562605, false);

private final Scheme scheme;
private final long coinType;
Expand Down
Loading

0 comments on commit ceb6bac

Please sign in to comment.