Skip to content

Commit

Permalink
fix some bugs;
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Zhang committed Jul 26, 2016
1 parent 0423925 commit 8af9a70
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
13 changes: 13 additions & 0 deletions sdk/AntShares/Implementations/Wallets/IndexedDB/DbContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

constructor(private name: string) { }

public close(): void
{
this.db.close();
}

public static delete(name: string): PromiseLike<void>
{
let indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
Expand All @@ -20,6 +25,10 @@
{
reject(request.error);
};
request.onblocked = () =>
{
reject("blocked");
};
});
}

Expand All @@ -45,6 +54,10 @@
{
reject(request.error);
};
request.onblocked = () =>
{
reject("blocked");
};
});
}

Expand Down
23 changes: 20 additions & 3 deletions sdk/AntShares/Implementations/Wallets/IndexedDB/IndexedDBWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,21 @@ namespace AntShares.Implementations.Wallets.IndexedDB
});
}

public close(): PromiseLike<void>
{
return super.close().then(() =>
{
this.db.close();
});
}

public static create(name: string, password: string): PromiseLike<IndexedDBWallet>
{
let wallet = new IndexedDBWallet(name);
return wallet.init(name, password, true).then(() =>
return wallet.db.open().then(() =>
{
return wallet.init(name, password, true);
}).then(() =>
{
return wallet.createAccount();
}).then(() =>
Expand Down Expand Up @@ -182,7 +193,10 @@ namespace AntShares.Implementations.Wallets.IndexedDB

protected loadStoredData(name: string): PromiseLike<ArrayBuffer>
{
return DbContext.promise<ArrayBuffer>(this.db.transaction("Key", "readonly").store("Key").get(name));
return DbContext.promise<any>(this.db.transaction("Key", "readonly").store("Key").get(name)).then(result =>
{
return result.value.hexToBytes().buffer;
});
}

private onCoinsChanged(transaction: DbTransaction, added: AntShares.Wallets.Coin[], changed: AntShares.Wallets.Coin[], deleted: AntShares.Wallets.Coin[]): void
Expand Down Expand Up @@ -279,7 +293,10 @@ namespace AntShares.Implementations.Wallets.IndexedDB
public static open(name: string, password: string): PromiseLike<IndexedDBWallet>
{
let wallet = new IndexedDBWallet(name);
return wallet.init(name, password, false).then(() =>
return wallet.db.open().then(result =>
{
return wallet.init(name, password, false);
}).then(() =>
{
return wallet;
});
Expand Down
6 changes: 5 additions & 1 deletion sdk/AntShares/Uint160.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ namespace AntShares
public static parse(str: string): Uint160
{
if (str.length != 40) throw new RangeError();
return new Uint160(str.hexToBytes().buffer);
let x = str.hexToBytes();
let y = new Uint8Array(x.length);
for (let i = 0; i < y.length; i++)
y[i] = x[x.length - i - 1];
return new Uint160(y.buffer);
}
}
}
6 changes: 5 additions & 1 deletion sdk/AntShares/Uint256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ namespace AntShares
public static parse(str: string): Uint256
{
if (str.length != 64) throw new RangeError();
return new Uint256(str.hexToBytes().buffer);
let x = str.hexToBytes();
let y = new Uint8Array(x.length);
for (let i = 0; i < y.length; i++)
y[i] = x[x.length - i - 1];
return new Uint256(y.buffer);
}
}
}
24 changes: 13 additions & 11 deletions sdk/AntShares/Wallets/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,25 +395,27 @@
if (passwordHash[i] != passwordKeyHash[i])
throw new Error();
this.iv = new Uint8Array(results[1]);
return Promise.all([
this.loadStoredData("MasterKey").then(result =>
{
return window.crypto.subtle.decrypt({ name: "AES-CBC", iv: this.iv }, aesKey, <any>result);
}),
return this.loadStoredData("MasterKey");
}).then(result =>
{
return window.crypto.subtle.decrypt({ name: "AES-CBC", iv: this.iv }, aesKey, <any>result);
}).then(result =>
{
this.masterKey = new Uint8Array(result);
return Promise.all<any>([
this.loadAccounts(),
this.loadContracts(),
this.loadCoins(),
this.loadStoredData("Height"),
]);
}).then(results =>
{
this.masterKey = new Uint8Array(results[0]);
for (let i = 0; i < results[0].length; i++)
this.accounts.set(results[0][i].publicKeyHash.toString(), results[0][i]);
for (let i = 0; i < results[1].length; i++)
this.accounts.set(results[1][i].publicKeyHash.toString(), results[1][i]);
for (let i = 0; i < results[2].length; i++)
this.contracts.set(results[2][i].scriptHash.toString(), results[2][i]);
this.coins = new IO.Caching.TrackableCollection<string, Coin>(results[3]);
this.current_height = (new Uint32Array(results[4]))[0];
this.contracts.set(results[1][i].scriptHash.toString(), results[1][i]);
this.coins = new IO.Caching.TrackableCollection<string, Coin>(results[2]);
this.current_height = (new Uint32Array(results[3]))[0];
});
}
}).then(() =>
Expand Down

0 comments on commit 8af9a70

Please sign in to comment.