refactor: remove all use of cast in modules within nim_status

This commit is contained in:
Michael Bradley, Jr 2021-07-05 16:23:27 -05:00 committed by Eric Mastro
parent 9e70dbc89a
commit 16045f5cee
6 changed files with 25 additions and 26 deletions

View File

@ -26,7 +26,7 @@ proc `$`*(acc: Account): string =
echo "Addr: ", acc.address
echo "PrivateKey: ", acc.privateKey
echo "PublicKey: ", acc.publicKey
echo "Path: ", cast[string](acc.path)
echo "Path: ", acc.path.string
echo "Account end"
proc toDisplayString*(account: Account): string =
@ -38,7 +38,7 @@ proc getSeed*(mnemonic: Mnemonic, password: KeystorePass = ""): KeySeed =
KeySeed sha512.pbkdf2(mnemonic.string, salt, 2048, 64)
proc splitHMAC*(seed: string, salt: string): ExtendedPrivKeyResult =
let hmacResult = sha512.hmac(masterSecret, cast[seq[byte]](seed))
let hmacResult = sha512.hmac(masterSecret, seed.toBytes())
let secretKey = hmacResult.data[0..31]
let chainCode = hmacResult.data[32..63]
let sk = SkSecretKey.fromRaw(secretKey)
@ -78,7 +78,7 @@ proc child(self: ExtendedPrivKey, child: PathLevel): ExtendedPrivKeyResult =
err($sk.error())
proc derive*(seed: Keyseed, path: KeyPath): SecretKeyResult =
var extPrivK = splitHMAC(cast[string](seed), masterSecret).get()
var extPrivK = splitHMAC(string.fromBytes(openArray[byte](seed)), masterSecret).get()
for child in path.pathNodes:
if child.isErr(): return err(child.error().cstring)

View File

@ -5,15 +5,15 @@ type
Mnemonic* = distinct string
KeySeed* = distinct seq[byte]
KeystorePass* = string
KeyPath* = distinct string
PathLevel* = distinct uint32
PathLevelResult* = Result[PathLevel, string]
ExtendedPrivKey* = object
secretKey*: SkSecretKey
chainCode*: seq[byte]

View File

@ -63,17 +63,17 @@ type
invitationAdmin* {.serializedFieldName($ChatType.InvitationAdmin), dbColumnName($ChatCol.InvitationAdmin).}: string
muted* {.serializedFieldName($ChatType.Muted), dbColumnName($ChatCol.Muted).}: bool
proc getChats*(db: DbConn): seq[Chat] =
proc getChats*(db: DbConn): seq[Chat] =
let query = """SELECT * from chats"""
result = db.all(Chat, query)
proc getChatById*(db: DbConn, id: string): Option[Chat] =
proc getChatById*(db: DbConn, id: string): Option[Chat] =
let query = """SELECT * from chats where id = ?"""
result = db.one(Chat, query, id)
proc saveChat*(db: DbConn, chat: Chat) =
proc saveChat*(db: DbConn, chat: Chat) =
let query = fmt"""INSERT INTO chats(
{$ChatCol.Id},
{$ChatCol.Name},
@ -91,9 +91,9 @@ proc saveChat*(db: DbConn, chat: Chat) =
{$ChatCol.Profile},
{$ChatCol.InvitationAdmin},
{$ChatCol.Muted})
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
"""
db.exec(query,
db.exec(query,
chat.id,
chat.name,
chat.color,
@ -146,8 +146,8 @@ proc blockContact*(db: DbConn, contact: Contact): seq[Chat] =
# Recalculate denormalized fields
query = fmt"""UPDATE chats
SET unviewed_message_count = (SELECT COUNT(1)
FROM user_messages WHERE seen = 0
SET unviewed_message_count = (SELECT COUNT(1)
FROM user_messages WHERE seen = 0
AND local_chat_id = chats.id)"""
db.exec(query)
@ -167,6 +167,6 @@ proc blockContact*(db: DbConn, contact: Contact): seq[Chat] =
let encodedMessage = $$lastMessage
query = fmt"""UPDATE chats SET last_message = ? WHERE id = ?"""
db.exec(query, encodedMessage, c.id)
c.lastMessage = some(cast[seq[byte]](encodedMessage))
c.lastMessage = some(encodedMessage.toBytes())
chats

View File

@ -7,7 +7,7 @@ import nimcrypto/sysrand as sysrand
import nimcrypto/sha2 as sha2
import account/types
type
type
EntropyStrength = distinct uint
BitSeq = seq[byte]
Language* = enum
@ -17,7 +17,7 @@ proc `$`*(s: BitSeq): string =
var str: string
for b in s:
str.add(if b == 1: '1' else: '0')
return str
@ -25,7 +25,7 @@ proc `$`*(s: BitSeq): string =
proc getBits*(b: byte): BitSeq =
var s = newSeq[byte]()
for i in 0..7:
let bit = cast[byte]((b shr i) and 1)
let bit = (b shr i) and 1
s.insert(bit, 0)
return s
@ -40,7 +40,7 @@ proc getBits*(byteSeq: seq[byte]): BitSeq =
proc getBits*(byteStr: string): BitSeq =
var s: BitSeq
for b in byteStr:
let bits = getBits(cast[byte](b))
let bits = getBits(b.byte)
s = concat(s, bits)
return s
@ -86,4 +86,3 @@ proc mnemonicPhrase*(strength: int, language: Language): Mnemonic =
return Mnemonic words.join(wordSeparator)

View File

@ -63,7 +63,7 @@ proc deriveAccounts*(multiAcc: MultiAccount, paths: seq[KeyPath]): seq[Account]
var accounts: seq[Account]
for p in paths:
let skResult = derive(multiAcc.keySeed, p)
var acc = buildAccount(cast[PrivateKey](skResult.get()))
var acc = buildAccount(PrivateKey(skResult.get()))
acc.path = p
accounts.add(acc)
return accounts
@ -71,7 +71,7 @@ proc deriveAccounts*(multiAcc: MultiAccount, paths: seq[KeyPath]): seq[Account]
proc importMnemonic*(mnemonicPhrase: Mnemonic, bip39Passphrase: string): MultiAccount =
let seed = getSeed(Mnemonic mnemonicPhrase, bip39Passphrase)
# Ensure seed is within expected limits
let lseed = len(cast[seq[byte]](seed))
let lseed = openArray[byte](seed).len
if lseed < MIN_SEED_BYTES or lseed > MAX_SEED_BYTES:
return MultiAccount()

View File

@ -16,8 +16,8 @@ procSuite "mnemonic":
echo "BitSeq: ", s
let mnemonic = mnemonicPhrase(128, Language.English)
let mnemonic = mnemonicPhrase(128, Language.English).string
echo "phrase:"
echo mnemonic
assert mnemonic.split(" ").len == 12