-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: grapebaba <[email protected]>
- Loading branch information
Showing
10 changed files
with
221 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2023 [email protected] | ||
* Copyright 2022-2023 [email protected] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with | ||
|
@@ -33,41 +33,62 @@ | |
* @author fearlessfe | ||
* @since 2023.02 | ||
*/ | ||
@SuppressWarnings("checkstyle:AbbreviationAsWordInName") | ||
public class ED25519KeyDerive { | ||
|
||
private final String defaultDerivePath = "m/44H/784H/0H/0H/0H"; | ||
private byte[] key; | ||
private byte[] chaincode; | ||
private static final String DEFAULT_DERIVE_PATH = "m/44H/784H/0H/0H/0H"; | ||
private final byte[] key; | ||
private final byte[] chaincode; | ||
|
||
/** | ||
* Create key by default path ed 25519 key derive. | ||
* | ||
* @param seed the seed | ||
* @return the ed 25519 key derive | ||
*/ | ||
public static ED25519KeyDerive createKeyByDefaultPath(byte[] seed) { | ||
return createMasterKey(seed).deriveFromPath(""); | ||
} | ||
|
||
/** | ||
* Create master key ed 25519 key derive. | ||
* | ||
* @param seed the seed | ||
* @return the ed 25519 key derive | ||
*/ | ||
public static ED25519KeyDerive createMasterKey(byte[] seed) { | ||
byte[] i = HDUtils.hmacSha512("ed25519 seed".getBytes(Charset.defaultCharset()), seed); | ||
byte[] il = Arrays.copyOfRange(i, 0, 32); | ||
byte[] ir = Arrays.copyOfRange(i, 32, 64); | ||
return new ED25519KeyDerive(il, ir); | ||
} | ||
|
||
/** | ||
* Instantiates a new Ed 25519 key derive. | ||
* | ||
* @param key the key | ||
* @param chaincode the chaincode | ||
*/ | ||
public ED25519KeyDerive(byte[] key, byte[] chaincode) { | ||
this.key = key; | ||
this.chaincode = chaincode; | ||
} | ||
|
||
/** | ||
* Derive ed 25519 key derive. | ||
* | ||
* @param index the index | ||
* @return the ed 25519 key derive | ||
*/ | ||
public ED25519KeyDerive derive(int index) { | ||
if (!hasHardenedBit(index)) {} | ||
if (!hasHardenedBit(index)) { | ||
// todo: create an exception | ||
throw new RuntimeException(); | ||
} | ||
|
||
byte[] indexBytes = new byte[4]; | ||
ByteBuffer.wrap(indexBytes).putInt(index); | ||
|
||
// byte[] a = new byte[]{0x00}; | ||
// | ||
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
// outputStream.write(a); | ||
// outputStream.write(this.key); | ||
// outputStream.write(indexBytes); | ||
|
||
byte[] data = Bytes.concat(new byte[] {0x00}, this.key, indexBytes); | ||
|
||
byte[] i = HDUtils.hmacSha512(this.chaincode, data); | ||
|
@@ -77,9 +98,15 @@ public ED25519KeyDerive derive(int index) { | |
return new ED25519KeyDerive(il, ir); | ||
} | ||
|
||
/** | ||
* Derive from path ed 25519 key derive. | ||
* | ||
* @param path the path | ||
* @return the ed 25519 key derive | ||
*/ | ||
public ED25519KeyDerive deriveFromPath(String path) { | ||
if (StringUtils.isAnyBlank(path)) { | ||
path = defaultDerivePath; | ||
path = DEFAULT_DERIVE_PATH; | ||
} | ||
HDPath hdPath = HDPath.parsePath(path); | ||
Iterator<ChildNumber> it = hdPath.iterator(); | ||
|
@@ -94,10 +121,20 @@ private boolean hasHardenedBit(int a) { | |
return (a & ChildNumber.HARDENED_BIT) != 0; | ||
} | ||
|
||
/** | ||
* Get key byte [ ]. | ||
* | ||
* @return the byte [ ] | ||
*/ | ||
public byte[] getKey() { | ||
return key; | ||
} | ||
|
||
/** | ||
* Get chaincode byte [ ]. | ||
* | ||
* @return the byte [ ] | ||
*/ | ||
public byte[] getChaincode() { | ||
return chaincode; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.