summaryrefslogtreecommitdiff
path: root/KeyRing.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-04-15 14:02:24 -0400
committerjoe <joe@jerkface.net>2014-04-15 14:02:24 -0400
commit4d96c66f6ece8383c04543c875556d1fae9422f6 (patch)
treeab237de0edfccaa7bf6978f4105e0c7877668e85 /KeyRing.hs
parentc43883d074b09c9d86de9873d261bd34aa7ac1fb (diff)
work in progress: writeWalletKeys
Diffstat (limited to 'KeyRing.hs')
-rw-r--r--KeyRing.hs47
1 files changed, 37 insertions, 10 deletions
diff --git a/KeyRing.hs b/KeyRing.hs
index d931302..2d92c7e 100644
--- a/KeyRing.hs
+++ b/KeyRing.hs
@@ -424,7 +424,7 @@ data KeySpec =
424 424
425 425
426buildKeyDB :: FilePath -> FilePath -> Maybe String -> KeyRingData 426buildKeyDB :: FilePath -> FilePath -> Maybe String -> KeyRingData
427 -> IO (KikiCondition ((KeyDB,Maybe String),[(FilePath,KikiReportAction)])) 427 -> IO (KikiCondition ((KeyDB,Maybe String,Maybe Packet),[(FilePath,KikiReportAction)]))
428buildKeyDB secring pubring grip0 keyring = do 428buildKeyDB secring pubring grip0 keyring = do
429 let isring (KeyRingFile {}) = True 429 let isring (KeyRingFile {}) = True
430 isring _ = False 430 isring _ = False
@@ -448,15 +448,15 @@ buildKeyDB secring pubring grip0 keyring = do
448 where isSecringKey (fn,Message ps) 448 where isSecringKey (fn,Message ps)
449 | fn==secring = listToMaybe ps 449 | fn==secring = listToMaybe ps
450 isSecringKey _ = Nothing 450 isSecringKey _ = Nothing
451 db_rings = foldl' (uncurry . merge) Map.empty ms
451 wk = listToMaybe $ do 452 wk = listToMaybe $ do
452 fp <- maybeToList grip 453 fp <- maybeToList grip
453 elm <- Map.toList db0 454 elm <- Map.toList db_rings
454 guard $ matchSpec (KeyGrip fp) elm 455 guard $ matchSpec (KeyGrip fp) elm
455 return $ keyPacket (snd elm) 456 return $ keyPacket (snd elm)
456 db0 = foldl' (uncurry . merge) Map.empty ms
457 457
458 wms <- mapM (readw wk) (files iswallet) 458 wms <- mapM (readw wk) (files iswallet)
459 let wms' = do 459 let wallet_keys = do
460 maybeToList wk 460 maybeToList wk
461 (fname,xs) <- wms 461 (fname,xs) <- wms
462 (_,sub,(_,m)) <- xs 462 (_,sub,(_,m)) <- xs
@@ -476,9 +476,9 @@ buildKeyDB secring pubring grip0 keyring = do
476 try r $ \(db'',report) -> do 476 try r $ \(db'',report) -> do
477 return $ KikiSuccess (db'', report0 ++ report) 477 return $ KikiSuccess (db'', report0 ++ report)
478 478
479 db <- foldM importWalletKey (KikiSuccess (db0,[])) wms' 479 db <- foldM importWalletKey (KikiSuccess (db_rings,[])) wallet_keys
480 try db $ \(db,report) -> do 480 try db $ \(db,report) -> do
481 return $ KikiSuccess ( (db, grip), report ) 481 return $ KikiSuccess ( (db, grip, wk), report )
482 482
483torhash key = maybe "" id $ derToBase32 <$> derRSA key 483torhash key = maybe "" id $ derToBase32 <$> derRSA key
484 484
@@ -582,7 +582,32 @@ doImportG doDecrypt db m0 tag fname key = do
582 , report ) 582 , report )
583 Just (False,sig) -> doInsert (Just sig) db -- We have a sig, but is missing usage@ tag 583 Just (False,sig) -> doInsert (Just sig) db -- We have a sig, but is missing usage@ tag
584 584
585 585isCryptoCoinKey p =
586 and [ isKey p
587 , key_algorithm p == ECDSA
588 , lookup 'c' (key p) == Just (MPI secp256k1_id)
589 ]
590
591getCryptoCoinTag p | isSignaturePacket p = do
592 -- CryptoCoins.secret
593 let sps = hashed_subpackets p ++ unhashed_subpackets p
594 u <- listToMaybe $ mapMaybe usage sps
595 CryptoCoins.lookupNetwork CryptoCoins.network_name u
596getCryptoCoinTag _ = Nothing
597
598
599writeWalletKeys :: KeyRingData -> KeyDB -> Maybe Packet -> IO (KikiCondition [(FilePath,KikiReportAction)])
600writeWalletKeys krd db wk = do
601 let all_crypto_keys = do
602 wk <- maybeToList wk
603 let kk = keykey wk
604 KeyData top topsigs uids subs <- maybeToList $ Map.lookup kk db
605 (subkk,SubKey mp sigs) <- Map.toList subs
606 let sub = packet mp
607 guard $ isCryptoCoinKey sub
608 tag <- take 1 $ mapMaybe getCryptoCoinTag (map (packet . fst) sigs)
609 return (tag,mp)
610 return $ KikiSuccess []
586 611
587runKeyRing :: KeyRingData -> (KeyRingRuntime -> a) -> IO (KikiResult a) 612runKeyRing :: KeyRingData -> (KeyRingRuntime -> a) -> IO (KikiResult a)
588runKeyRing keyring op = do 613runKeyRing keyring op = do
@@ -606,7 +631,7 @@ runKeyRing keyring op = do
606 ret <- case functorToEither ret of 631 ret <- case functorToEither ret of
607 Right {} -> do 632 Right {} -> do
608 bresult <- buildKeyDB secring pubring grip0 keyring -- build db 633 bresult <- buildKeyDB secring pubring grip0 keyring -- build db
609 try' bresult $ \((db,grip),report1) -> do 634 try' bresult $ \((db,grip,wk),report1) -> do
610 a <- return $ op KeyRingRuntime 635 a <- return $ op KeyRingRuntime
611 { rtPubring = pubring 636 { rtPubring = pubring
612 , rtSecring = secring 637 , rtSecring = secring
@@ -615,9 +640,11 @@ runKeyRing keyring op = do
615 , rtGrip = grip 640 , rtGrip = grip
616 , rtKeyDB = db 641 , rtKeyDB = db
617 } 642 }
618 report2 <- todo -- write files 643 r <- writeWalletKeys keyring db wk
644 try' r $ \report2 -> do
645 report3 <- todo -- write files
619 646
620 return $ KikiResult (KikiSuccess a) (report1 ++ report2) 647 return $ KikiResult (KikiSuccess a) (report1 ++ report3)
621 Left err -> return $ KikiResult err [] 648 Left err -> return $ KikiResult err []
622 649
623 forM_ lked $ \(Just lk, fname) -> do dotlock_release lk 650 forM_ lked $ \(Just lk, fname) -> do dotlock_release lk