2017-09-25 02:59:46 +00:00
|
|
|
package cy.agorise.crystalwallet.models;
|
|
|
|
|
2017-11-13 03:04:06 +00:00
|
|
|
import android.content.Context;
|
|
|
|
|
2017-10-18 02:55:42 +00:00
|
|
|
import org.bitcoinj.core.ECKey;
|
2018-01-07 03:21:46 +00:00
|
|
|
import org.bitcoinj.crypto.ChildNumber;
|
|
|
|
import org.bitcoinj.crypto.DeterministicKey;
|
|
|
|
import org.bitcoinj.crypto.HDKeyDerivation;
|
2017-10-18 02:55:42 +00:00
|
|
|
|
2017-11-13 03:04:06 +00:00
|
|
|
import cy.agorise.crystalwallet.dao.CrystalDatabase;
|
|
|
|
import cy.agorise.crystalwallet.enums.SeedType;
|
|
|
|
import cy.agorise.graphenej.BrainKey;
|
|
|
|
|
2017-09-25 02:59:46 +00:00
|
|
|
/**
|
2017-10-18 02:55:42 +00:00
|
|
|
*
|
2017-09-25 02:59:46 +00:00
|
|
|
* Created by henry on 24/9/2017.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class GrapheneAccount extends CryptoNetAccount {
|
|
|
|
|
2017-10-18 00:35:37 +00:00
|
|
|
public static int subclass = 1;
|
2017-09-25 02:59:46 +00:00
|
|
|
protected String name;
|
2017-10-10 02:44:44 +00:00
|
|
|
protected String accountId;
|
2017-09-25 02:59:46 +00:00
|
|
|
|
2017-10-18 02:55:42 +00:00
|
|
|
public GrapheneAccount() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public GrapheneAccount(CryptoNetAccount account) {
|
|
|
|
super(account.getId(),account.getSeedId(),account.getAccountIndex(),account.getCryptoNet());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadInfo(GrapheneAccountInfo info){
|
|
|
|
this.name = info.getName();
|
|
|
|
this.accountId = info.getAccountId();
|
|
|
|
}
|
|
|
|
|
2017-10-10 02:44:44 +00:00
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setName(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getAccountId() {
|
|
|
|
return accountId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAccountId(String accountId) {
|
|
|
|
this.accountId = accountId;
|
|
|
|
}
|
2017-10-18 02:55:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the owner key, generates from the seed if it has not been generated. null if it can't be generated
|
|
|
|
*/
|
2017-11-13 03:04:06 +00:00
|
|
|
public ECKey getOwnerKey(Context context){
|
2018-01-10 02:33:23 +00:00
|
|
|
AccountSeed seed = CrystalDatabase.getAppDatabase(context).accountSeedDao().findById(this.getSeedId());
|
2018-01-10 02:00:46 +00:00
|
|
|
if(seed == null){
|
|
|
|
System.out.println("Error: Seed null " + this.getSeedId());
|
|
|
|
return null;
|
|
|
|
}
|
2017-11-13 03:04:06 +00:00
|
|
|
if(seed.getType().equals(SeedType.BRAINKEY)){
|
2018-01-10 02:00:46 +00:00
|
|
|
System.out.println("Seed type barinkey");
|
2018-01-07 03:21:46 +00:00
|
|
|
return seed.getPrivateKey();
|
2017-11-13 03:04:06 +00:00
|
|
|
}else{
|
2018-01-10 02:00:46 +00:00
|
|
|
System.out.println("Seed type bip39");
|
2018-01-07 03:21:46 +00:00
|
|
|
DeterministicKey masterKey = (DeterministicKey) seed.getPrivateKey();
|
|
|
|
DeterministicKey purposeKey = HDKeyDerivation.deriveChildKey(masterKey,
|
|
|
|
new ChildNumber(48, true));
|
|
|
|
DeterministicKey networkKey = HDKeyDerivation.deriveChildKey(purposeKey,
|
|
|
|
new ChildNumber(1, true));
|
|
|
|
DeterministicKey accountIndexKey = HDKeyDerivation.deriveChildKey(networkKey,
|
|
|
|
new ChildNumber(0, true));
|
|
|
|
DeterministicKey permission = HDKeyDerivation.deriveChildKey(accountIndexKey,
|
|
|
|
new ChildNumber(0, true));
|
|
|
|
DeterministicKey address = HDKeyDerivation.deriveChildKey(permission,
|
2018-01-10 02:00:46 +00:00
|
|
|
new ChildNumber(0, false));
|
2018-01-10 03:08:11 +00:00
|
|
|
return ECKey.fromPrivate(address.getPrivKeyBytes());
|
2017-11-13 03:04:06 +00:00
|
|
|
}
|
2017-10-18 02:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the active key, generates from the seed if it has not been generated. null if it can't be generated
|
|
|
|
*/
|
2017-11-13 03:04:06 +00:00
|
|
|
public ECKey getActiveKey(Context context){
|
2018-01-10 02:33:23 +00:00
|
|
|
AccountSeed seed = CrystalDatabase.getAppDatabase(context).accountSeedDao().findById(this.getSeedId());
|
2017-11-13 03:04:06 +00:00
|
|
|
if(seed == null)
|
|
|
|
return null;
|
|
|
|
if(seed.getType().equals(SeedType.BRAINKEY)){
|
|
|
|
return new BrainKey(seed.getMasterSeed(),0).getPrivateKey();
|
|
|
|
}else{
|
2018-01-23 02:22:57 +00:00
|
|
|
System.out.println("calculating activekey from bip39");
|
2018-01-07 03:21:46 +00:00
|
|
|
DeterministicKey masterKey = (DeterministicKey) seed.getPrivateKey();
|
|
|
|
DeterministicKey purposeKey = HDKeyDerivation.deriveChildKey(masterKey,
|
|
|
|
new ChildNumber(48, true));
|
|
|
|
DeterministicKey networkKey = HDKeyDerivation.deriveChildKey(purposeKey,
|
|
|
|
new ChildNumber(1, true));
|
|
|
|
DeterministicKey accountIndexKey = HDKeyDerivation.deriveChildKey(networkKey,
|
|
|
|
new ChildNumber(0, true));
|
|
|
|
DeterministicKey permission = HDKeyDerivation.deriveChildKey(accountIndexKey,
|
|
|
|
new ChildNumber(1, true));
|
2018-01-10 03:08:11 +00:00
|
|
|
DeterministicKey address = HDKeyDerivation.deriveChildKey(permission,
|
|
|
|
new ChildNumber(0, false)); //TODO implement multiple Address and accounts
|
|
|
|
return ECKey.fromPrivate(address.getPrivKeyBytes());
|
2017-11-13 03:04:06 +00:00
|
|
|
}
|
2017-10-18 02:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the memo key, generates from the seed if it has not been generated. null if it can't be generated
|
|
|
|
*/
|
2017-11-13 03:04:06 +00:00
|
|
|
public ECKey getMemoKey(Context context){
|
2018-01-10 02:33:23 +00:00
|
|
|
AccountSeed seed = CrystalDatabase.getAppDatabase(context).accountSeedDao().findById(this.getSeedId());
|
2017-11-13 03:04:06 +00:00
|
|
|
if(seed == null)
|
|
|
|
return null;
|
|
|
|
if(seed.getType().equals(SeedType.BRAINKEY)){
|
|
|
|
return new BrainKey(seed.getMasterSeed(),0).getPrivateKey();
|
|
|
|
}else{
|
2018-01-07 03:21:46 +00:00
|
|
|
DeterministicKey masterKey = (DeterministicKey) seed.getPrivateKey();
|
|
|
|
DeterministicKey purposeKey = HDKeyDerivation.deriveChildKey(masterKey,
|
|
|
|
new ChildNumber(48, true));
|
|
|
|
DeterministicKey networkKey = HDKeyDerivation.deriveChildKey(purposeKey,
|
|
|
|
new ChildNumber(1, true));
|
|
|
|
DeterministicKey accountIndexKey = HDKeyDerivation.deriveChildKey(networkKey,
|
|
|
|
new ChildNumber(0, true));
|
|
|
|
DeterministicKey permission = HDKeyDerivation.deriveChildKey(accountIndexKey,
|
|
|
|
new ChildNumber(3, true));
|
2018-01-10 03:08:11 +00:00
|
|
|
DeterministicKey address = HDKeyDerivation.deriveChildKey(permission,
|
|
|
|
new ChildNumber(0, false)); //TODO implement multiple Address and accounts
|
|
|
|
return ECKey.fromPrivate(address.getPrivKeyBytes());
|
2017-11-13 03:04:06 +00:00
|
|
|
}
|
2017-10-18 02:55:42 +00:00
|
|
|
}
|
2017-09-25 02:59:46 +00:00
|
|
|
}
|