Search in sources :

Example 1 with HDWallet

use of com.toshi.crypto.HDWallet in project toshi-android-client by toshiapp.

the class SignInPresenter method tryCreateWallet.

private void tryCreateWallet(final String masterSeed) {
    if (this.onGoingTask)
        return;
    startLoadingTask();
    final Subscription sub = new HDWallet().createFromMasterSeed(masterSeed).flatMapCompletable(this::initWallet).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnCompleted(AppPrefs.INSTANCE::setHasBackedUpPhrase).subscribe(this::handleWalletSuccess, this::handleWalletError);
    this.subscriptions.add(sub);
}
Also used : CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) HDWallet(com.toshi.crypto.HDWallet) AppPrefs(com.toshi.util.sharedPrefs.AppPrefs)

Example 2 with HDWallet

use of com.toshi.crypto.HDWallet in project toshi-android-client by toshiapp.

the class SigningInterceptor method intercept.

@Override
public Response intercept(final Chain chain) throws IOException {
    final Request original = chain.request();
    final String timestamp = original.url().queryParameter(TIMESTAMP_QUERY_PARAMETER);
    if (timestamp == null) {
        // Only signing outgoing requests that have a timestamp argument
        return chain.proceed(original);
    }
    final HDWallet wallet = getWallet();
    if (wallet == null) {
        // Only signing outgoing requests that have a timestamp argument
        return chain.proceed(original);
    }
    final Buffer buffer = new Buffer();
    final String method = original.method();
    final String path = original.url().encodedPath();
    String encodedBody = "";
    if (original.body() != null) {
        original.body().writeTo(buffer);
        final byte[] body = buffer.readByteArray();
        final byte[] hashedBody = HashUtil.sha3(body);
        encodedBody = Base64.encodeToString(hashedBody, Base64.NO_WRAP);
    }
    final String forSigning = method + "\n" + path + "\n" + timestamp + "\n" + encodedBody;
    final String signature = wallet.signIdentity(forSigning);
    final HttpUrl url = chain.request().url().newBuilder().removeAllQueryParameters(TIMESTAMP_QUERY_PARAMETER).build();
    final Request request = original.newBuilder().removeHeader(TIMESTAMP_QUERY_PARAMETER).method(original.method(), original.body()).addHeader(TIMESTAMP_HEADER, timestamp).addHeader(SIGNATURE_HEADER, signature).addHeader(ADDRESS_HEADER, wallet.getOwnerAddress()).url(url).build();
    return chain.proceed(request);
}
Also used : Buffer(okio.Buffer) Request(okhttp3.Request) HDWallet(com.toshi.crypto.HDWallet) HttpUrl(okhttp3.HttpUrl)

Aggregations

HDWallet (com.toshi.crypto.HDWallet)2 AppPrefs (com.toshi.util.sharedPrefs.AppPrefs)1 HttpUrl (okhttp3.HttpUrl)1 Request (okhttp3.Request)1 Buffer (okio.Buffer)1 Subscription (rx.Subscription)1 CompositeSubscription (rx.subscriptions.CompositeSubscription)1