Browse Source

Btc only dev (#36)

* fix ui issue

* remove unused code
V1.2.0-btc-release
JunZhang 5 years ago
committed by GitHub
parent
commit
209e63cffb
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/src/main/assets/bundleMap.json
  2. 4
      app/src/main/assets/script/BTC.bundle_4d617bb38a890cdb2baf.js
  3. 30
      app/src/main/assets/utils.bundle.js
  4. 5
      app/src/main/java/com/cobo/cold/callables/RequestUpdateCallable.java
  5. 10
      app/src/main/java/com/cobo/cold/callables/UpdateCallable.java
  6. 2
      app/src/main/java/com/cobo/cold/ui/fragment/main/electrum/ExportXpubGuideFragment.java
  7. 31
      app/src/main/java/com/cobo/cold/viewmodel/PsbtViewModel.java
  8. 1
      app/src/main/res/layout/common_modal.xml
  9. 90
      app/src/main/res/layout/export_xpub_guide.xml
  10. 45
      coinlib/src/main/java/com/cobo/coinlib/utils/HDKey.java
  11. 81
      coinlib/src/main/java/com/cobo/coinlib/v8/HDKeyImpl.java

2
app/src/main/assets/bundleMap.json

@ -1,3 +1,3 @@
{
"BTC": "BTC.bundle_a521a1b83cf8627d0ce5.js"
"BTC": "BTC.bundle_4d617bb38a890cdb2baf.js"
}

4
app/src/main/assets/script/BTC.bundle_a521a1b83cf8627d0ce5.js → app/src/main/assets/script/BTC.bundle_4d617bb38a890cdb2baf.js

File diff suppressed because one or more lines are too long

30
app/src/main/assets/utils.bundle.js

File diff suppressed because one or more lines are too long

5
app/src/main/java/com/cobo/cold/callables/RequestUpdateCallable.java

@ -68,7 +68,6 @@ public class RequestUpdateCallable implements Callable<Boolean> {
}
private boolean updating() throws Exception {
final byte[] checksum = calculateChecksum(updateData);
final byte[] readBuffer = new byte[CONSTANTS.CONFIG.PAGE_SIZE];
try (InputStream inputStream = new ByteArrayInputStream(updateData)) {
// skip metadata
@ -89,9 +88,7 @@ public class RequestUpdateCallable implements Callable<Boolean> {
.addBytePayload(CONSTANTS.TAGS.UPDATING_PACKAGE_TYPE, type)
.addHexPayload(CONSTANTS.TAGS.CURRENT_PASSWORD, password)
.addBytesPayload(CONSTANTS.TAGS.UPDATING_PACKAGE, availableBuffer);
if (type == TYPE_PACKAGE_END) {
builder.addBytesPayload(CONSTANTS.TAGS.UPDATING_CHECKSUM, checksum);
}
final Callable callable = new BlockingCallable(builder.build());
try {
Log.w(TAG, String.format("write the %sth package", packageIndex));

10
app/src/main/java/com/cobo/cold/callables/UpdateCallable.java

@ -106,8 +106,6 @@ public class UpdateCallable implements Callable<Void> {
}
private void updating() throws Exception {
final byte[] checksum = calculateChecksum(mUpdateData);
final byte[] readBuffer = new byte[CONSTANTS.CONFIG.PAGE_SIZE];
try (InputStream inputStream = new ByteArrayInputStream(mUpdateData)) {
@ -124,7 +122,7 @@ public class UpdateCallable implements Callable<Void> {
final byte[] availableBuffer = fitBuffer(readBuffer, read);
final int type = calculatePackageType(packageSize, readPosition, read);
for (int i = 1; i <= PACKET_RETRY_TIMES; ++i) {
if (writeUpdateBytes(checksum, packageIndex, availableBuffer, type, i)) break;
if (writeUpdateBytes(packageIndex, availableBuffer, type, i)) break;
}
readPosition += read;
++packageIndex;
@ -154,17 +152,13 @@ public class UpdateCallable implements Callable<Void> {
return false;
}
private boolean writeUpdateBytes(byte[] checksum, int packageIndex, byte[] availableBuffer, int type, int i) {
private boolean writeUpdateBytes(int packageIndex, byte[] availableBuffer, int type, int i) {
boolean writeSuccess = true;
final Packet.Builder builder = new Packet.Builder(CONSTANTS.METHODS.WRITE_UPDATE_BYTES)
.addBytePayload(CONSTANTS.TAGS.UPDATING_PACKAGE_TYPE, type)
.addHexPayload(CONSTANTS.TAGS.CURRENT_PASSWORD, password)
.addBytesPayload(CONSTANTS.TAGS.UPDATING_PACKAGE, availableBuffer);
if (type == TYPE_PACKAGE_END) {
builder.addBytesPayload(CONSTANTS.TAGS.UPDATING_CHECKSUM, checksum);
}
final Callable callable = new BlockingCallable(builder.build());
try {

2
app/src/main/java/com/cobo/cold/ui/fragment/main/electrum/ExportXpubGuideFragment.java

@ -107,6 +107,8 @@ public class ExportXpubGuideFragment extends BaseFragment<ExportXpubGuideBinding
Runnable runnable = null;
if (mActivity instanceof SetupVaultActivity) {
runnable = () -> navigate(R.id.action_to_setupCompleteFragment);
} else {
runnable = () -> popBackStack(R.id.assetFragment,false);
}
exportSuccess(mActivity, runnable);
}

31
app/src/main/java/com/cobo/cold/viewmodel/PsbtViewModel.java

@ -24,7 +24,11 @@ import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cobo.coinlib.Util;
import com.cobo.coinlib.utils.Coins;
import com.cobo.cold.AppExecutors;
import com.cobo.cold.MainApplication;
import com.cobo.cold.Utilities;
import com.cobo.cold.callables.GetMasterFingerprintCallable;
import com.cobo.cold.update.utils.Storage;
@ -38,6 +42,8 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.cobo.cold.viewmodel.GlobalViewModel.getAccount;
public class PsbtViewModel extends AndroidViewModel {
@ -60,6 +66,7 @@ public class PsbtViewModel extends AndroidViewModel {
if (inputs.length() < 1) {
throw new WatchWalletNotMatchException("no input match masterFingerprint");
}
adaptOutputs(psbt.getJSONArray("outputs"), outputs);
object.put("inputs", inputs);
object.put("outputs", outputs);
@ -68,6 +75,8 @@ public class PsbtViewModel extends AndroidViewModel {
private static void adaptInputs(JSONArray psbtInputs, JSONArray inputs) throws JSONException {
String masterKeyFingerprint = new GetMasterFingerprintCallable().call();
Coins.Account account = getAccount(MainApplication.getApplication());
for (int i = 0; i < psbtInputs.length(); i++) {
JSONObject psbtInput = psbtInputs.getJSONObject(i);
JSONObject in = new JSONObject();
@ -77,11 +86,13 @@ public class PsbtViewModel extends AndroidViewModel {
JSONArray bip32Derivation = psbtInput.getJSONArray("hdPath");
for (int j = 0; j < bip32Derivation.length(); j++) {
JSONObject item = bip32Derivation.getJSONObject(j);
if (item.getString("masterFingerprint").equals(masterKeyFingerprint)) {
String hdPath = item.getString("path");
if (item.getString("masterFingerprint").equals(masterKeyFingerprint)
&& hdPath.toUpperCase().startsWith(account.getPath())) {
utxo.put("publicKey", item.getString("pubkey"));
utxo.put("value", psbtInput.optInt("value"));
in.put("utxo", utxo);
in.put("ownerKeyPath", item.getString("path"));
in.put("ownerKeyPath", hdPath);
in.put("masterFingerprint", item.getString("masterFingerprint"));
inputs.put(in);
break;
@ -93,11 +104,27 @@ public class PsbtViewModel extends AndroidViewModel {
}
private static void adaptOutputs(JSONArray psbtOutputs, JSONArray outputs) throws JSONException {
String masterKeyFingerprint = new GetMasterFingerprintCallable().call();
Coins.Account account = getAccount(MainApplication.getApplication());
for(int i = 0; i < psbtOutputs.length(); i++) {
JSONObject psbtOutput = psbtOutputs.getJSONObject(i);
JSONObject out = new JSONObject();
out.put("address", psbtOutput.getString("address"));
out.put("value", psbtOutput.getInt("value"));
JSONArray bip32Derivation = psbtOutput.optJSONArray("hdPath");
if (bip32Derivation != null) {
for (int j = 0; j < bip32Derivation.length(); j++) {
JSONObject item = bip32Derivation.getJSONObject(j);
String hdPath = item.getString("path");
if (item.getString("masterFingerprint").equals(masterKeyFingerprint)
&& hdPath.toUpperCase().startsWith(account.getPath())) {
out.put("isChange",true);
out.put("changeAddressPath", hdPath);
}
}
}
outputs.put(out);
}
}

1
app/src/main/res/layout/common_modal.xml

@ -47,6 +47,7 @@
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:layout_marginHorizontal="10dp"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />

90
app/src/main/res/layout/export_xpub_guide.xml

@ -50,49 +50,61 @@
<include layout="@layout/divider" />
<TextView
android:id="@+id/text1"
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:padding="16dp"
android:text="@string/export_to_electrum_guide_hint"
android:textColor="@color/white"
android:textSize="15sp"
android:textStyle="bold" />
android:layout_height="match_parent"
android:fillViewport="true">
<com.cobo.cold.ui.views.SpanedTextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingExtra="6dp"
android:padding="16dp"
android:text="@string/export_to_electrum_guide"
android:textColor="@color/white"
android:textSize="13sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:padding="16dp"
android:text="@string/export_to_electrum_guide_hint"
android:textColor="@color/white"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/export"
style="@style/AcceptButton"
android:layout_width="match_parent"
android:layout_marginHorizontal="16dp"
android:layout_marginBottom="0dp"
android:text="@string/export_xpub" />
<TextView
android:id="@+id/skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:textColor="@color/colorAccent"
android:gravity="center"
android:text="@string/skip_export"/>
<com.cobo.cold.ui.views.SpanedTextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingExtra="6dp"
android:padding="16dp"
android:text="@string/export_to_electrum_guide"
android:textColor="@color/white"
android:textSize="13sp" />
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/export"
style="@style/AcceptButton"
android:layout_width="match_parent"
android:layout_marginHorizontal="16dp"
android:layout_marginBottom="0dp"
android:text="@string/export_xpub" />
<TextView
android:id="@+id/skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="10dp"
android:text="@string/skip_export"
android:textColor="@color/colorAccent" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</layout>

45
coinlib/src/main/java/com/cobo/coinlib/utils/HDKey.java

@ -1,45 +0,0 @@
/*
* Copyright (c) 2020 Cobo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* in the file COPYING. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cobo.coinlib.utils;
import com.cobo.coinlib.v8.HDKeyImpl;
public abstract class HDKey {
public static HDKey getInstance() {
return new HDKeyImpl();
}
/**
* get public key from a base58 encoded account level extend publicKey
*
* @param accountExtendPublicKey account level extend publicKey
* @return publicKey
*/
abstract public String getPublicKey(String accountExtendPublicKey);
/**
* derive a public key from a base58 encoded account level extend publicKey
*
* @param AccountExtendPublicKey account level extend publicKey
* @param changeIndex change index
* @param index publicKey index
* @return publicKey
*/
abstract public String derivePublicKey(String AccountExtendPublicKey, int changeIndex, int index);
}

81
coinlib/src/main/java/com/cobo/coinlib/v8/HDKeyImpl.java

@ -1,81 +0,0 @@
/*
* Copyright (c) 2020 Cobo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* in the file COPYING. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cobo.coinlib.v8;
import com.cobo.coinlib.utils.HDKey;
import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Array;
import com.eclipsesource.v8.V8Function;
import com.eclipsesource.v8.V8TypedArray;
import org.bouncycastle.util.encoders.Hex;
public class HDKeyImpl extends HDKey {
private V8 v8;
private V8Function getPublicKeyFunction;
private V8Function derivePublicKeyFunction;
private final String nameSpace = "cryptoCoinKit.utils.";
@Override
public String getPublicKey(String accountExtendPublicKey) {
checkAndInit();
if (getPublicKeyFunction == null) {
getPublicKeyFunction = (V8Function) v8.executeObjectScript(nameSpace + "xpubToPubkey");
}
V8Array params = new V8Array(v8);
params.push(accountExtendPublicKey);
V8TypedArray pubkeyBuffer = (V8TypedArray) getPublicKeyFunction.call(v8, params);
String publicKey = Hex.toHexString(pubkeyBuffer.getBytes(0, pubkeyBuffer.length()));
params.close();
pubkeyBuffer.close();
getPublicKeyFunction.close();
v8.close();
return publicKey;
}
@Override
public String derivePublicKey(String accountExtendPublicKey, int changeIndex, int index) {
checkAndInit();
if (derivePublicKeyFunction == null) {
derivePublicKeyFunction = (V8Function) v8.executeObjectScript(nameSpace + "xpubToPubkey");
}
V8Array params = new V8Array(v8);
V8Array derivePath = new V8Array(v8);
params.push(accountExtendPublicKey).
push(derivePath.push(changeIndex).push(index));
V8TypedArray pubkeyBuffer = (V8TypedArray) derivePublicKeyFunction.call(v8, params);
String publicKey = Hex.toHexString(pubkeyBuffer.getBytes(0, pubkeyBuffer.length()));
params.close();
pubkeyBuffer.close();
derivePath.close();
derivePublicKeyFunction.close();
v8.close();
return publicKey;
}
private void checkAndInit() {
if (v8 == null || v8.isReleased()) {
v8 = ScriptLoader.sInstance.loadByFileName("utils.bundle.js");
}
}
}
Loading…
Cancel
Save