Merge pull request #2 from gabrielbazan7/feat/WalletService02

Feat: Wallet Provider - 2
This commit is contained in:
Gabriel Masclef 2017-10-13 16:39:57 -03:00 committed by GitHub
commit fde2bd16fb
1 changed files with 100 additions and 100 deletions

View File

@ -8,114 +8,114 @@ import { AndroidFingerprintAuth } from '@ionic-native/android-fingerprint-auth';
@Injectable() @Injectable()
export class TouchIdProvider { export class TouchIdProvider {
private _isAvailable: boolean = false; private _isAvailable: boolean = false;
constructor( constructor(
private touchId: TouchID, private touchId: TouchID,
private androidFingerprintAuth: AndroidFingerprintAuth, private androidFingerprintAuth: AndroidFingerprintAuth,
private platform: PlatformProvider, private platform: PlatformProvider,
private config: ConfigProvider private config: ConfigProvider
) { } ) { }
init() { init() {
if (this.platform.isAndroid) this.checkAndroid(); if (this.platform.isAndroid) this.checkAndroid();
if (this.platform.isIOS) this.checkIOS(); if (this.platform.isIOS) this.checkIOS();
} }
checkIOS() { checkIOS() {
this.touchId.isAvailable() this.touchId.isAvailable()
.then( .then(
res => this._isAvailable = true, res => this._isAvailable = true,
err => console.log("Fingerprint is not available") err => console.log("Fingerprint is not available")
); );
} }
checkAndroid() { checkAndroid() {
this.androidFingerprintAuth.isAvailable() this.androidFingerprintAuth.isAvailable()
.then( .then(
res => { res => {
if (res.isAvailable) this._isAvailable = true if (res.isAvailable) this._isAvailable = true
else console.log("Fingerprint is not available") else console.log("Fingerprint is not available")
}); });
} }
verifyIOSFingerprint(): Promise<any> { verifyIOSFingerprint(): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.touchId.verifyFingerprint('Scan your fingerprint please') this.touchId.verifyFingerprint('Scan your fingerprint please')
.then( .then(
res => resolve(), res => resolve(),
err => reject() err => reject()
); );
}); });
} }
verifyAndroidFingerprint(): Promise<any> { verifyAndroidFingerprint(): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.androidFingerprintAuth.encrypt({ clientId: 'Copay' }) this.androidFingerprintAuth.encrypt({ clientId: 'Copay' })
.then(result => { .then(result => {
if (result.withFingerprint) { if (result.withFingerprint) {
console.log('Successfully authenticated with fingerprint.'); console.log('Successfully authenticated with fingerprint.');
resolve(); resolve();
} else if (result.withBackup) { } else if (result.withBackup) {
console.log('Successfully authenticated with backup password!'); console.log('Successfully authenticated with backup password!');
resolve(); resolve();
} else console.log('Didn\'t authenticate!'); } else console.log('Didn\'t authenticate!');
}).catch(error => { }).catch(error => {
if (error === this.androidFingerprintAuth.ERRORS.FINGERPRINT_CANCELLED) { if (error === this.androidFingerprintAuth.ERRORS.FINGERPRINT_CANCELLED) {
console.log('Fingerprint authentication cancelled'); console.log('Fingerprint authentication cancelled');
reject(); reject();
} else { } else {
console.error(error); console.error(error);
resolve(); resolve();
}; };
}); });
}); });
} }
isAvailable() { isAvailable() {
return this._isAvailable; return this._isAvailable;
} }
check(): Promise<any> { check(): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!this.isAvailable()) reject(); if (!this.isAvailable()) reject();
if (this.platform.isIOS) { if (this.platform.isIOS) {
this.verifyIOSFingerprint() this.verifyIOSFingerprint()
.then(() => { .then(() => {
resolve(); resolve();
}) })
.catch(() => { .catch(() => {
reject(); reject();
}); });
}; };
if (this.platform.isAndroid) { if (this.platform.isAndroid) {
this.verifyAndroidFingerprint() this.verifyAndroidFingerprint()
.then(() => { .then(() => {
resolve(); resolve();
}) })
.catch(() => { .catch(() => {
reject(); reject();
}); });
}; };
}); });
} }
isNeeded(wallet: any) { isNeeded(wallet: any) {
let config: any = this.config.get(); let config: any = this.config.get();
config.touchIdFor = config.touchIdFor || {}; config.touchIdFor = config.touchIdFor || {};
return config.touchIdFor[wallet.credentials.walletId]; return config.touchIdFor[wallet.credentials.walletId];
} }
checkWallet(wallet: any): Promise<any> { checkWallet(wallet: any): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!this.isAvailable()) reject(); if (!this.isAvailable()) reject();
if (this.isNeeded(wallet)) { if (this.isNeeded(wallet)) {
this.check().then(() => { this.check().then(() => {
resolve(); resolve();
}).catch(() => { }).catch(() => {
reject(); reject();
}); });
}; };
}); });
} }
} }