Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/interfaces/error.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export enum IErrorInternal {
UnableToGenerateKeypair = 'Unable to generate key-pair',
UnableToDeriveNextKeypair = 'Unable to derive next key-pair',
UnableToEncryptKeypair = 'Unable to encrypt key-pair',
UnableToSaveKeypairLocal = 'Unable to save key-pair to local storage',
UnableToGetLocalKeypair = 'Unable to get key-pair from local storage',
UnableToEncryptMasterKey = 'Unable to encrypt master key',
UnableToDecryptKeypair = 'Unable to decrypt keypair',
UnableToDecryptMasterKey = 'Unable to decrypt master key',
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/network.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export type IContentType = {
regenWalletResponse?: IKeypairEncrypted[];
signMessageResponse?: IGenericKeyPair<string>;
decryptKeypairResponse?: IKeypair;
saveKeypairResponse?: string[];
getKeypairsResponse?: IKeypairEncrypted[];
} & IApiContentType;

// Content received from mempool node / intercom server API endpoints
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/success.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@ export enum ISuccessInternal {
SeedPhraseObtained = 'Successfully obtained seed phrase',
MasterKeyObtained = 'Successfully obtained master key',
KeypairDecrypted = 'Successfully decrypted key-pair',
KeypairSaved = 'Successfully saved key-pair to local storage',
KeypairObtained = 'Successfully retreived key-pair from local storage',
RespondedToRbPayment = 'Successfully responded to receipt-based payment',
}
1 change: 1 addition & 0 deletions src/mgmt/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export const DEFAULT_HEADERS = {
'Content-Type': 'application/json',
},
};
export const KEYPAIR_LOCAL_STORAGE = 'ABlockJS_KeypairLocalStorage';
2 changes: 1 addition & 1 deletion src/mgmt/key.mgmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,4 @@ export function testSeedPhrase(seed: string): boolean {
*/
export function generateSeedPhrase(): string {
return generateSeed().unwrapOr('');
}
}
51 changes: 51 additions & 0 deletions src/services/ablock.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,57 @@ export class ABlockWallet {
}
}

/**
* Save keypairs to localStorage. (Browser)
* It is recommended to use user defined methods for I/O operations (see https://github.com/ABlockOfficial/A-Block.js#getting-started)
*
* @param {IKeypairEncrypted} encryptedKeypair - Encrypted key-pair to save
* @return {*} {void}
*/
saveKeypairs(encryptedKeypair: IKeypairEncrypted[]): IClientResponse {
try {
if (!this.keyMgmt) throw new Error(IErrorInternal.ClientNotInitialized);
throwIfErr(this.keyMgmt.saveKeypairs(encryptedKeypair));
return {
status: 'success',
reason: ISuccessInternal.KeypairSaved,
};
} catch (error) {
return {
status: 'error',
reason: `${error}`,
};
}
}

/**
* Get keypairs from localStorage. (Browser)
* It is recommended to use user defined methods for I/O operations (see https://github.com/ABlockOfficial/A-Block.js#getting-started)
*
* @export
* @param {string} keypairs IKeypairEncrypted[] flattened to a string
* @return {*} {void}
*/
getKeypairs(): IClientResponse {
try {
if (!this.keyMgmt) throw new Error(IErrorInternal.ClientNotInitialized);
return {
status: 'success',
reason: ISuccessInternal.KeypairObtained,
content: {
getKeypairsResponse: throwIfErr(
this.keyMgmt.getKeypairs()
),
},
};
} catch (error) {
return {
status: 'error',
reason: `${error}`,
};
}
}

/* -------------------------------------------------------------------------- */
/* Utils */

Expand Down
34 changes: 34 additions & 0 deletions src/services/mgmt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
getAddressVersion,
getNextDerivedKeypair,
getPassphraseBuffer,
KEYPAIR_LOCAL_STORAGE,
SEED_REGEN_THRES,
TEMP_ADDRESS_VERSION,
} from '../mgmt';
Expand Down Expand Up @@ -505,4 +506,37 @@ export class mgmtClient {
if (encryptedMasterKey.isErr()) return err(encryptedMasterKey.error);
return ok(encryptedMasterKey.value);
}

/**
* Save keypairs to localStorage. (Browser)
*
* @export
* @param {string} keypairs IKeypairEncrypted[] flattened to a string
* @return {*} {void} address of saved keypair
*/
public saveKeypairs(keypairs: IKeypairEncrypted[]): IResult<void> {
console.log(window);
console.log(window.localStorage)
if (!keypairs || typeof window !== 'undefined') {
const flattened = JSON.stringify(keypairs);
window.localStorage.setItem(KEYPAIR_LOCAL_STORAGE, flattened);
return ok(undefined)
}
return err(IErrorInternal.UnableToSaveKeypairLocal)
}

/**
* Save keypairs to localStorage. (Browser)
*
* @export
* @return {*} {IKeypairEncrypted[]}
*/
public getKeypairs(): IResult<IKeypairEncrypted[]> {
let result = null;
if (typeof window !== 'undefined')
result = window.localStorage.getItem(KEYPAIR_LOCAL_STORAGE);
if (result != null)
return ok(JSON.parse(result))
return err(IErrorInternal.UnableToGetLocalKeypair)
}
}