diff --git a/.gitignore b/.gitignore index 8bd3a05..8ac8101 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /.settings/ /.classpath /.project +/.DS_Store diff --git a/pom.xml b/pom.xml index 528fd73..91fa557 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.metadium did-sdk-java - 0.2.0 + 0.2.1 Metadium DID SDK Java library for Metadium DID diff --git a/src/main/java/com/metadium/did/MetadiumWallet.java b/src/main/java/com/metadium/did/MetadiumWallet.java index ba15ff3..b26fcfe 100644 --- a/src/main/java/com/metadium/did/MetadiumWallet.java +++ b/src/main/java/com/metadium/did/MetadiumWallet.java @@ -57,15 +57,26 @@ public MetadiumKey getKey() { } /** - * create DID + * Create Did + * @param metaDelegator + * @return + * @throws DidException + */ + public static MetadiumWallet createDid(MetaDelegator metaDelegator) throws DidException { + return createDid(metaDelegator, null); + } + + /** + * create DID from key * * @param metaDelegator {@link MetaDelegator} + * @param key 지갑 키 * @return 생성된 DID 지갑 * @throws DidException */ - public static MetadiumWallet createDid(MetaDelegator metaDelegator) throws DidException { + public static MetadiumWallet createDid(MetaDelegator metaDelegator, MetadiumKey key) throws DidException { try { - MetadiumWallet metadiumDid = new MetadiumWallet(new MetadiumKey()); + MetadiumWallet metadiumDid = new MetadiumWallet(key == null ? new MetadiumKey() : key); String txHash = metaDelegator.createIdentityDelegated(metadiumDid.key); TransactionReceipt transactionReceipt = Web3jUtils.ethGetTransactionReceipt(metaDelegator.getWeb3j(), txHash); diff --git a/src/test/java/com/metadium/did/DidTest.java b/src/test/java/com/metadium/did/DidTest.java index ef15330..4b692a8 100644 --- a/src/test/java/com/metadium/did/DidTest.java +++ b/src/test/java/com/metadium/did/DidTest.java @@ -8,9 +8,13 @@ import java.math.BigInteger; import java.security.InvalidAlgorithmParameterException; +import java.security.SecureRandom; import java.text.ParseException; import org.junit.Test; +import org.web3j.crypto.Bip32ECKeyPair; +import org.web3j.crypto.ECKeyPair; +import org.web3j.crypto.MnemonicUtils; import org.web3j.utils.Numeric; import com.metadium.did.crypto.MetadiumKey; @@ -195,4 +199,31 @@ public void createDidEnterpriseTest() throws DidException, InvalidAlgorithmParam System.out.println("public_key="+didDocument.getPublicKey(wallet.getKid()).getPublicKeyHex()); System.out.println("kid="+wallet.getKid()); } + + @Test + public void createDidWithMnemonic() throws Exception { + // create mnemonic + byte[] initialEntropy = new byte[16]; + new SecureRandom().nextBytes(initialEntropy); + String mnemonic = MnemonicUtils.generateMnemonic(initialEntropy); + + // private to mnemonic + byte[] seed = MnemonicUtils.generateSeed(mnemonic, null); + Bip32ECKeyPair master = Bip32ECKeyPair.generateKeyPair(seed); + ECKeyPair keyPair = Bip32ECKeyPair.deriveKeyPair(master, /*KeyManager.BIP44_META_PATH*/new int[] {44 | 0x80000000, 916 | 0x80000000, 0x80000000, 0, 0}); + + // DID 생성 + MetaDelegator delegator = new MetaDelegator(); + MetadiumWallet wallet = MetadiumWallet.createDid(delegator, new MetadiumKey(keyPair)); + + // did 와 mnemonic 저장 + String did = wallet.getDid(); + + // did 와 mnemonic 으로 wallet 재생성 + ECKeyPair newKeyPair = Bip32ECKeyPair.deriveKeyPair(Bip32ECKeyPair.generateKeyPair(MnemonicUtils.generateSeed(mnemonic, null)), /*KeyManager.BIP44_META_PATH*/new int[] {44 | 0x80000000, 916 | 0x80000000, 0x80000000, 0, 0}); + MetadiumWallet newWallet = new MetadiumWallet(did, new MetadiumKey(newKeyPair)); + + assertEquals(wallet.getDid(), newWallet.getDid()); + assertEquals(wallet.getKey().getPrivateKey(), newWallet.getKey().getPrivateKey()); + } }