
package com.github.bitcoinlabs.bitcoinmobileandroid;

import java.util.ArrayList;
import java.util.List;

import com.google.bitcoin.core.Address;
import com.google.bitcoin.core.ECKey;
import com.google.bitcoin.core.NetworkParameters;

import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class WalletOpenHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "test2";
    private static final int DATABASE_VERSION = 2;
    
    public static final String KEY = "key";
    public static final String ADDRESS = "address58";
    

    WalletOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(
            "CREATE TABLE keys (" +
            "id         INTEGER PRIMARY KEY AUTOINCREMENT," +
            ADDRESS + " TEXT," +
            KEY + "     BLOB);");
    }

    @Override
    public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
        onCreate(db);
    }
    
    public Address newKey() {
        ECKey key = new ECKey();
        Address address = addKey(key);
        return address;
    }

    public Address addKey(ECKey key) {
        SQLiteDatabase db = getWritableDatabase();
        Address address = key.toAddress(NetworkParameters.prodNet());
        db.execSQL(
                "INSERT INTO keys ('address58', 'key') VALUES (?, ?)",
                new Object[] { address.toString(), key.toASN1()} );
        db.close();
        return address;
    }
    
    public Cursor getAddresses() {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query("keys", new String[]{ADDRESS}, null, null, null, null, null, "5");
        return cursor;
    }
}

@@ -501,13 +501,12 @@
  = %22
-test2
+keys
 %22;%0A 
@@ -556,15 +556,11 @@
 N = 
-2
+1
 ;%0A
-    
 %0A   
@@ -653,21 +653,16 @@
 ress58%22;
-%0A    
 %0A%0A    Wa
@@ -1130,37 +1130,33 @@
 eate(db);%0A    %7D%0A
-    
 %0A
+
     public Addre
@@ -1582,14 +1582,32 @@
 key.
-toASN1
+getPrivKey().toByteArray
 ()%7D 
@@ -1659,20 +1659,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub

rename database to "keys", save only the private keys