Skip to content

Commit

Permalink
주어진 ec key 로 did 생성할 수 있는 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungBaeJeon committed Jan 21, 2021
1 parent b4e7244 commit e7f88b3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.settings/
/.classpath
/.project
/.DS_Store
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.metadium</groupId>
<artifactId>did-sdk-java</artifactId>
<version>0.2.0</version>
<version>0.2.1</version>

<name>Metadium DID SDK </name>
<description>Java library for Metadium DID</description>
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/metadium/did/MetadiumWallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/com/metadium/did/DidTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit e7f88b3

Please sign in to comment.