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 @@ -38,5 +38,7 @@ export enum ISuccessInternal {
3838 SeedPhraseObtained = 'Successfully obtained seed phrase' ,
3939 MasterKeyObtained = 'Successfully obtained master key' ,
4040 KeypairDecrypted = 'Successfully decrypted key-pair' ,
41+ KeypairSaved = 'Successfully saved key-pair to local storage' ,
42+ KeypairObtained = 'Successfully retreived key-pair from local storage' ,
4143 RespondedToRbPayment = 'Successfully responded to receipt-based payment' ,
4244}
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 @@ -1128,6 +1128,57 @@ export class ABlockWallet {
11281128 }
11291129 }
11301130
1131+ /**
1132+ * Save keypairs to localStorage. (Browser)
1133+ * It is recommended to use user defined methods for I/O operations (see https://github.com/ABlockOfficial/A-Block.js#getting-started)
1134+ *
1135+ * @param {IKeypairEncrypted } encryptedKeypair - Encrypted key-pair to save
1136+ * @return {* } {void}
1137+ */
1138+ saveKeypairs ( encryptedKeypair : IKeypairEncrypted [ ] ) : IClientResponse {
1139+ try {
1140+ if ( ! this . keyMgmt ) throw new Error ( IErrorInternal . ClientNotInitialized ) ;
1141+ throwIfErr ( this . keyMgmt . saveKeypairs ( encryptedKeypair ) ) ;
1142+ return {
1143+ status : 'success' ,
1144+ reason : ISuccessInternal . KeypairSaved ,
1145+ } ;
1146+ } catch ( error ) {
1147+ return {
1148+ status : 'error' ,
1149+ reason : `${ error } ` ,
1150+ } ;
1151+ }
1152+ }
1153+
1154+ /**
1155+ * Get keypairs from localStorage. (Browser)
1156+ * It is recommended to use user defined methods for I/O operations (see https://github.com/ABlockOfficial/A-Block.js#getting-started)
1157+ *
1158+ * @export
1159+ * @param {string } keypairs IKeypairEncrypted[] flattened to a string
1160+ * @return {* } {void}
1161+ */
1162+ getKeypairs ( ) : IClientResponse {
1163+ try {
1164+ if ( ! this . keyMgmt ) throw new Error ( IErrorInternal . ClientNotInitialized ) ;
1165+ return {
1166+ status : 'success' ,
1167+ reason : ISuccessInternal . KeypairObtained ,
1168+ content : {
1169+ getKeypairsResponse : throwIfErr (
1170+ this . keyMgmt . getKeypairs ( )
1171+ ) ,
1172+ } ,
1173+ } ;
1174+ } catch ( error ) {
1175+ return {
1176+ status : 'error' ,
1177+ reason : `${ error } ` ,
1178+ } ;
1179+ }
1180+ }
1181+
11311182 /* -------------------------------------------------------------------------- */
11321183 /* Utils */
11331184
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' ;
@@ -505,4 +506,37 @@ export class mgmtClient {
505506 if ( encryptedMasterKey . isErr ( ) ) return err ( encryptedMasterKey . error ) ;
506507 return ok ( encryptedMasterKey . value ) ;
507508 }
509+
510+ /**
511+ * Save keypairs to localStorage. (Browser)
512+ *
513+ * @export
514+ * @param {string } keypairs IKeypairEncrypted[] flattened to a string
515+ * @return {* } {void} address of saved keypair
516+ */
517+ public saveKeypairs ( keypairs : IKeypairEncrypted [ ] ) : IResult < void > {
518+ console . log ( window ) ;
519+ console . log ( window . localStorage )
520+ if ( ! keypairs || typeof window !== 'undefined' ) {
521+ const flattened = JSON . stringify ( keypairs ) ;
522+ window . localStorage . setItem ( KEYPAIR_LOCAL_STORAGE , flattened ) ;
523+ return ok ( undefined )
524+ }
525+ return err ( IErrorInternal . UnableToSaveKeypairLocal )
526+ }
527+
528+ /**
529+ * Save keypairs to localStorage. (Browser)
530+ *
531+ * @export
532+ * @return {* } {IKeypairEncrypted[]}
533+ */
534+ public getKeypairs ( ) : IResult < IKeypairEncrypted [ ] > {
535+ let result = null ;
536+ if ( typeof window !== 'undefined' )
537+ result = window . localStorage . getItem ( KEYPAIR_LOCAL_STORAGE ) ;
538+ if ( result != null )
539+ return ok ( JSON . parse ( result ) )
540+ return err ( IErrorInternal . UnableToGetLocalKeypair )
541+ }
508542}
You can’t perform that action at this time.
0 commit comments