refactor: remove all use of cast in modules within nim_status
This commit is contained in:
parent
9e70dbc89a
commit
16045f5cee
|
@ -26,7 +26,7 @@ proc `$`*(acc: Account): string =
|
||||||
echo "Addr: ", acc.address
|
echo "Addr: ", acc.address
|
||||||
echo "PrivateKey: ", acc.privateKey
|
echo "PrivateKey: ", acc.privateKey
|
||||||
echo "PublicKey: ", acc.publicKey
|
echo "PublicKey: ", acc.publicKey
|
||||||
echo "Path: ", cast[string](acc.path)
|
echo "Path: ", acc.path.string
|
||||||
echo "Account end"
|
echo "Account end"
|
||||||
|
|
||||||
proc toDisplayString*(account: Account): string =
|
proc toDisplayString*(account: Account): string =
|
||||||
|
@ -38,7 +38,7 @@ proc getSeed*(mnemonic: Mnemonic, password: KeystorePass = ""): KeySeed =
|
||||||
KeySeed sha512.pbkdf2(mnemonic.string, salt, 2048, 64)
|
KeySeed sha512.pbkdf2(mnemonic.string, salt, 2048, 64)
|
||||||
|
|
||||||
proc splitHMAC*(seed: string, salt: string): ExtendedPrivKeyResult =
|
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 secretKey = hmacResult.data[0..31]
|
||||||
let chainCode = hmacResult.data[32..63]
|
let chainCode = hmacResult.data[32..63]
|
||||||
let sk = SkSecretKey.fromRaw(secretKey)
|
let sk = SkSecretKey.fromRaw(secretKey)
|
||||||
|
@ -78,7 +78,7 @@ proc child(self: ExtendedPrivKey, child: PathLevel): ExtendedPrivKeyResult =
|
||||||
err($sk.error())
|
err($sk.error())
|
||||||
|
|
||||||
proc derive*(seed: Keyseed, path: KeyPath): SecretKeyResult =
|
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:
|
for child in path.pathNodes:
|
||||||
if child.isErr(): return err(child.error().cstring)
|
if child.isErr(): return err(child.error().cstring)
|
||||||
|
|
|
@ -5,15 +5,15 @@ type
|
||||||
Mnemonic* = distinct string
|
Mnemonic* = distinct string
|
||||||
|
|
||||||
KeySeed* = distinct seq[byte]
|
KeySeed* = distinct seq[byte]
|
||||||
|
|
||||||
KeystorePass* = string
|
KeystorePass* = string
|
||||||
|
|
||||||
KeyPath* = distinct string
|
KeyPath* = distinct string
|
||||||
|
|
||||||
PathLevel* = distinct uint32
|
PathLevel* = distinct uint32
|
||||||
|
|
||||||
PathLevelResult* = Result[PathLevel, string]
|
PathLevelResult* = Result[PathLevel, string]
|
||||||
|
|
||||||
ExtendedPrivKey* = object
|
ExtendedPrivKey* = object
|
||||||
secretKey*: SkSecretKey
|
secretKey*: SkSecretKey
|
||||||
chainCode*: seq[byte]
|
chainCode*: seq[byte]
|
||||||
|
|
|
@ -63,17 +63,17 @@ type
|
||||||
invitationAdmin* {.serializedFieldName($ChatType.InvitationAdmin), dbColumnName($ChatCol.InvitationAdmin).}: string
|
invitationAdmin* {.serializedFieldName($ChatType.InvitationAdmin), dbColumnName($ChatCol.InvitationAdmin).}: string
|
||||||
muted* {.serializedFieldName($ChatType.Muted), dbColumnName($ChatCol.Muted).}: bool
|
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"""
|
let query = """SELECT * from chats"""
|
||||||
|
|
||||||
result = db.all(Chat, query)
|
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 = ?"""
|
let query = """SELECT * from chats where id = ?"""
|
||||||
|
|
||||||
result = db.one(Chat, query, 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(
|
let query = fmt"""INSERT INTO chats(
|
||||||
{$ChatCol.Id},
|
{$ChatCol.Id},
|
||||||
{$ChatCol.Name},
|
{$ChatCol.Name},
|
||||||
|
@ -91,9 +91,9 @@ proc saveChat*(db: DbConn, chat: Chat) =
|
||||||
{$ChatCol.Profile},
|
{$ChatCol.Profile},
|
||||||
{$ChatCol.InvitationAdmin},
|
{$ChatCol.InvitationAdmin},
|
||||||
{$ChatCol.Muted})
|
{$ChatCol.Muted})
|
||||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
|
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
|
||||||
"""
|
"""
|
||||||
db.exec(query,
|
db.exec(query,
|
||||||
chat.id,
|
chat.id,
|
||||||
chat.name,
|
chat.name,
|
||||||
chat.color,
|
chat.color,
|
||||||
|
@ -146,8 +146,8 @@ proc blockContact*(db: DbConn, contact: Contact): seq[Chat] =
|
||||||
|
|
||||||
# Recalculate denormalized fields
|
# Recalculate denormalized fields
|
||||||
query = fmt"""UPDATE chats
|
query = fmt"""UPDATE chats
|
||||||
SET unviewed_message_count = (SELECT COUNT(1)
|
SET unviewed_message_count = (SELECT COUNT(1)
|
||||||
FROM user_messages WHERE seen = 0
|
FROM user_messages WHERE seen = 0
|
||||||
AND local_chat_id = chats.id)"""
|
AND local_chat_id = chats.id)"""
|
||||||
db.exec(query)
|
db.exec(query)
|
||||||
|
|
||||||
|
@ -167,6 +167,6 @@ proc blockContact*(db: DbConn, contact: Contact): seq[Chat] =
|
||||||
let encodedMessage = $$lastMessage
|
let encodedMessage = $$lastMessage
|
||||||
query = fmt"""UPDATE chats SET last_message = ? WHERE id = ?"""
|
query = fmt"""UPDATE chats SET last_message = ? WHERE id = ?"""
|
||||||
db.exec(query, encodedMessage, c.id)
|
db.exec(query, encodedMessage, c.id)
|
||||||
c.lastMessage = some(cast[seq[byte]](encodedMessage))
|
c.lastMessage = some(encodedMessage.toBytes())
|
||||||
|
|
||||||
chats
|
chats
|
||||||
|
|
|
@ -7,7 +7,7 @@ import nimcrypto/sysrand as sysrand
|
||||||
import nimcrypto/sha2 as sha2
|
import nimcrypto/sha2 as sha2
|
||||||
import account/types
|
import account/types
|
||||||
|
|
||||||
type
|
type
|
||||||
EntropyStrength = distinct uint
|
EntropyStrength = distinct uint
|
||||||
BitSeq = seq[byte]
|
BitSeq = seq[byte]
|
||||||
Language* = enum
|
Language* = enum
|
||||||
|
@ -17,7 +17,7 @@ proc `$`*(s: BitSeq): string =
|
||||||
var str: string
|
var str: string
|
||||||
for b in s:
|
for b in s:
|
||||||
str.add(if b == 1: '1' else: '0')
|
str.add(if b == 1: '1' else: '0')
|
||||||
|
|
||||||
return str
|
return str
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ proc `$`*(s: BitSeq): string =
|
||||||
proc getBits*(b: byte): BitSeq =
|
proc getBits*(b: byte): BitSeq =
|
||||||
var s = newSeq[byte]()
|
var s = newSeq[byte]()
|
||||||
for i in 0..7:
|
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)
|
s.insert(bit, 0)
|
||||||
|
|
||||||
return s
|
return s
|
||||||
|
@ -40,7 +40,7 @@ proc getBits*(byteSeq: seq[byte]): BitSeq =
|
||||||
proc getBits*(byteStr: string): BitSeq =
|
proc getBits*(byteStr: string): BitSeq =
|
||||||
var s: BitSeq
|
var s: BitSeq
|
||||||
for b in byteStr:
|
for b in byteStr:
|
||||||
let bits = getBits(cast[byte](b))
|
let bits = getBits(b.byte)
|
||||||
s = concat(s, bits)
|
s = concat(s, bits)
|
||||||
|
|
||||||
return s
|
return s
|
||||||
|
@ -86,4 +86,3 @@ proc mnemonicPhrase*(strength: int, language: Language): Mnemonic =
|
||||||
|
|
||||||
|
|
||||||
return Mnemonic words.join(wordSeparator)
|
return Mnemonic words.join(wordSeparator)
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ proc deriveAccounts*(multiAcc: MultiAccount, paths: seq[KeyPath]): seq[Account]
|
||||||
var accounts: seq[Account]
|
var accounts: seq[Account]
|
||||||
for p in paths:
|
for p in paths:
|
||||||
let skResult = derive(multiAcc.keySeed, p)
|
let skResult = derive(multiAcc.keySeed, p)
|
||||||
var acc = buildAccount(cast[PrivateKey](skResult.get()))
|
var acc = buildAccount(PrivateKey(skResult.get()))
|
||||||
acc.path = p
|
acc.path = p
|
||||||
accounts.add(acc)
|
accounts.add(acc)
|
||||||
return accounts
|
return accounts
|
||||||
|
@ -71,7 +71,7 @@ proc deriveAccounts*(multiAcc: MultiAccount, paths: seq[KeyPath]): seq[Account]
|
||||||
proc importMnemonic*(mnemonicPhrase: Mnemonic, bip39Passphrase: string): MultiAccount =
|
proc importMnemonic*(mnemonicPhrase: Mnemonic, bip39Passphrase: string): MultiAccount =
|
||||||
let seed = getSeed(Mnemonic mnemonicPhrase, bip39Passphrase)
|
let seed = getSeed(Mnemonic mnemonicPhrase, bip39Passphrase)
|
||||||
# Ensure seed is within expected limits
|
# 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:
|
if lseed < MIN_SEED_BYTES or lseed > MAX_SEED_BYTES:
|
||||||
return MultiAccount()
|
return MultiAccount()
|
||||||
|
|
||||||
|
|
|
@ -16,8 +16,8 @@ procSuite "mnemonic":
|
||||||
|
|
||||||
echo "BitSeq: ", s
|
echo "BitSeq: ", s
|
||||||
|
|
||||||
let mnemonic = mnemonicPhrase(128, Language.English)
|
let mnemonic = mnemonicPhrase(128, Language.English).string
|
||||||
echo "phrase:"
|
echo "phrase:"
|
||||||
echo mnemonic
|
echo mnemonic
|
||||||
|
|
||||||
assert mnemonic.split(" ").len == 12
|
assert mnemonic.split(" ").len == 12
|
||||||
|
|
Loading…
Reference in New Issue