This repository was archived by the owner on Mar 20, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +93
-1
lines changed Expand file tree Collapse file tree 7 files changed +93
-1
lines changed Original file line number Diff line number Diff line change @@ -71,6 +71,8 @@ export enum IErrorInternal {
7171 UnableToGenerateKeypair = 'Unable to generate key-pair' ,
7272 UnableToDeriveNextKeypair = 'Unable to derive next key-pair' ,
7373 UnableToEncryptKeypair = 'Unable to encrypt key-pair' ,
74+ UnableToSaveKeypairLocal = 'Unable to save key-pair to local storage' ,
75+ UnableToGetLocalKeypair = 'Unable to get key-pair from local storage' ,
7476 UnableToEncryptMasterKey = 'Unable to encrypt master key' ,
7577 UnableToDecryptKeypair = 'Unable to decrypt keypair' ,
7678 UnableToDecryptMasterKey = 'Unable to decrypt master key' ,
Original file line number Diff line number Diff line change @@ -39,6 +39,8 @@ export type IContentType = {
3939 regenWalletResponse ?: IKeypairEncrypted [ ] ;
4040 signMessageResponse ?: IGenericKeyPair < string > ;
4141 decryptKeypairResponse ?: IKeypair ;
42+ saveKeypairResponse ?: string [ ] ;
43+ getKeypairsResponse ?: IKeypairEncrypted [ ] ;
4244} & IApiContentType ;
4345
4446// Content received from mempool node / intercom server API endpoints
Original file line number Diff line number Diff line change @@ -39,5 +39,7 @@ export enum ISuccessInternal {
3939 SeedPhraseObtained = 'Successfully obtained seed phrase' ,
4040 MasterKeyObtained = 'Successfully obtained master key' ,
4141 KeypairDecrypted = 'Successfully decrypted key-pair' ,
42+ KeypairSaved = 'Successfully saved key-pair to local storage' ,
43+ KeypairObtained = 'Successfully retreived key-pair from local storage' ,
4244 RespondedToRbPayment = 'Successfully responded to receipt-based payment' ,
4345}
Original file line number Diff line number Diff line change @@ -14,3 +14,4 @@ export const DEFAULT_HEADERS = {
1414 'Content-Type' : 'application/json' ,
1515 } ,
1616} ;
17+ export const KEYPAIR_LOCAL_STORAGE = 'ABlockJS_KeypairLocalStorage' ;
Original file line number Diff line number Diff line change @@ -305,4 +305,4 @@ export function testSeedPhrase(seed: string): boolean {
305305 */
306306export function generateSeedPhrase ( ) : string {
307307 return generateSeed ( ) . unwrapOr ( '' ) ;
308- }
308+ }
Original file line number Diff line number Diff line change @@ -1150,6 +1150,57 @@ export class ABlockWallet {
11501150 }
11511151 }
11521152
1153+ /**
1154+ * Save keypairs to localStorage. (Browser)
1155+ * It is recommended to use user defined methods for I/O operations (see https://github.com/ABlockOfficial/A-Block.js#getting-started)
1156+ *
1157+ * @param {IKeypairEncrypted } encryptedKeypair - Encrypted key-pair to save
1158+ * @return {* } {void}
1159+ */
1160+ saveKeypairs ( encryptedKeypair : IKeypairEncrypted [ ] ) : IClientResponse {
1161+ try {
1162+ if ( ! this . keyMgmt ) throw new Error ( IErrorInternal . ClientNotInitialized ) ;
1163+ throwIfErr ( this . keyMgmt . saveKeypairs ( encryptedKeypair ) ) ;
1164+ return {
1165+ status : 'success' ,
1166+ reason : ISuccessInternal . KeypairSaved ,
1167+ } ;
1168+ } catch ( error ) {
1169+ return {
1170+ status : 'error' ,
1171+ reason : `${ error } ` ,
1172+ } ;
1173+ }
1174+ }
1175+
1176+ /**
1177+ * Get keypairs from localStorage. (Browser)
1178+ * It is recommended to use user defined methods for I/O operations (see https://github.com/ABlockOfficial/A-Block.js#getting-started)
1179+ *
1180+ * @export
1181+ * @param {string } keypairs IKeypairEncrypted[] flattened to a string
1182+ * @return {* } {void}
1183+ */
1184+ getKeypairs ( ) : IClientResponse {
1185+ try {
1186+ if ( ! this . keyMgmt ) throw new Error ( IErrorInternal . ClientNotInitialized ) ;
1187+ return {
1188+ status : 'success' ,
1189+ reason : ISuccessInternal . KeypairObtained ,
1190+ content : {
1191+ getKeypairsResponse : throwIfErr (
1192+ this . keyMgmt . getKeypairs ( )
1193+ ) ,
1194+ } ,
1195+ } ;
1196+ } catch ( error ) {
1197+ return {
1198+ status : 'error' ,
1199+ reason : `${ error } ` ,
1200+ } ;
1201+ }
1202+ }
1203+
11531204 /* -------------------------------------------------------------------------- */
11541205 /* Utils */
11551206
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ import {
2626 getAddressVersion ,
2727 getNextDerivedKeypair ,
2828 getPassphraseBuffer ,
29+ KEYPAIR_LOCAL_STORAGE ,
2930 SEED_REGEN_THRES ,
3031 TEMP_ADDRESS_VERSION ,
3132} from '../mgmt' ;
@@ -516,4 +517,37 @@ export class mgmtClient {
516517 if ( encryptedMasterKey . isErr ( ) ) return err ( encryptedMasterKey . error ) ;
517518 return ok ( encryptedMasterKey . value ) ;
518519 }
520+
521+ /**
522+ * Save keypairs to localStorage. (Browser)
523+ *
524+ * @export
525+ * @param {string } keypairs IKeypairEncrypted[] flattened to a string
526+ * @return {* } {void} address of saved keypair
527+ */
528+ public saveKeypairs ( keypairs : IKeypairEncrypted [ ] ) : IResult < void > {
529+ console . log ( window ) ;
530+ console . log ( window . localStorage )
531+ if ( ! keypairs || typeof window !== 'undefined' ) {
532+ const flattened = JSON . stringify ( keypairs ) ;
533+ window . localStorage . setItem ( KEYPAIR_LOCAL_STORAGE , flattened ) ;
534+ return ok ( undefined )
535+ }
536+ return err ( IErrorInternal . UnableToSaveKeypairLocal )
537+ }
538+
539+ /**
540+ * Save keypairs to localStorage. (Browser)
541+ *
542+ * @export
543+ * @return {* } {IKeypairEncrypted[]}
544+ */
545+ public getKeypairs ( ) : IResult < IKeypairEncrypted [ ] > {
546+ let result = null ;
547+ if ( typeof window !== 'undefined' )
548+ result = window . localStorage . getItem ( KEYPAIR_LOCAL_STORAGE ) ;
549+ if ( result != null )
550+ return ok ( JSON . parse ( result ) )
551+ return err ( IErrorInternal . UnableToGetLocalKeypair )
552+ }
519553}
You can’t perform that action at this time.
0 commit comments