Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder83singh committed Nov 19, 2024
1 parent c32343a commit f813141
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 122 deletions.
79 changes: 64 additions & 15 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
name: Release for macOS
name: Build and upload assets
permissions:
contents: write
actions: write
on:
workflow_dispatch:
push:
tags:
- 'v*' # Triggers workflow for tags like "v1.0.0"

release:
types: [ published ]
# push:
# tags:
# - 'v*' # Triggers workflow for tags like "v1.0.0"
env:
repo_name: "kaspa-wallet-recovery"
binary: "kaspa-wallet-recovery-${{ github.ref_name }}"
jobs:
build:
runs-on: macos-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
# Build gnu-linux on ubuntu-18.04 and musl on ubuntu latest
# os: [ ubuntu-18.04, ubuntu-latest, windows-latest, macos-latest ]
os: [ macos-latest, windows-latest ]

name: Building, ${{ matrix.os }}
steps:
- name: Fix CRLF on Windows
if: runner.os == 'Windows'
run: git config --global core.autocrlf false

- name: Checkout Code
uses: actions/checkout@v3

Expand All @@ -22,29 +36,64 @@ jobs:
profile: minimal
toolchain: stable
override: true

- name: Set up rust
if: runner.os == 'macOS'
run: rustup target add x86_64-apple-darwin

- name: Build for macOS (x86_64)
if: runner.os == 'macOS'
run: cargo build --release --target x86_64-apple-darwin

- name: Build for macOS (ARM64)
if: runner.os == 'macOS'
run: cargo build --release --target aarch64-apple-darwin

- name: Package Binaries
- name: Package macOS Binaries
if: runner.os == 'macOS'
run: |
mkdir release-darwin-x86_64
cp target/x86_64-apple-darwin/release/kaspa-wallet-recovery release-darwin-x86_64/kaspa-wallet-recovery-x86_64
tar -czvf kaspa-wallet-recovery-darwin-x86_64.tar.gz release-darwin-x86_64
mkdir release-darwin-arm64
cp target/aarch64-apple-darwin/release/kaspa-wallet-recovery release-darwin-arm64/kaspa-wallet-recovery-arm64
tar -czvf kaspa-wallet-recovery-darwin-arm64.tar.gz release-darwin-arm64
folder="${{env.binary}}-osx-x86_64"
mkdir "${folder}"
cp target/x86_64-apple-darwin/release/${{ env.repo_name }} ${folder}/${{env.repo_name}}-x86_64
tar -czvf ${{env.binary}}-osx-x86_64.tar.gz ${folder}
folder="${{env.binary}}-osx-arm64"
mkdir "${folder}"
cp target/aarch64-apple-darwin/release/${{ env.repo_name }} ${folder}/${{env.repo_name}}-arm64
tar -czvf ${{env.binary}}-osx-arm64.tar.gz ${folder}
- name: Create GitHub Release
id: "create_release"
if: runner.os == 'macOS'
uses: ncipollo/release-action@v1
with:
allowUpdates: true
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ github.ref_name }}
name: Release ${{ github.ref_name }}
artifacts: kaspa-wallet-recovery-darwin-x86_64.tar.gz,kaspa-wallet-recovery-darwin-arm64.tar.gz

artifacts: ${{env.binary}}-osx-x86_64.tar.gz,${{env.binary}}-osx-arm64.tar.gz

- name: Build on Windows
if: runner.os == 'Windows'
shell: bash
run: |
cargo build --release
mkdir bin || true
cp target/release/${{ env.repo_name }}.exe bin/${{env.binary}}-win64.exe
archive="bin/${{env.binary}}-win64.zip"
asset_name="${{env.binary}}-win64.zip"
powershell "Compress-Archive bin/* \"${archive}\""
echo "archive=${archive}" >> $GITHUB_ENV
echo "asset_name=${asset_name}" >> $GITHUB_ENV
- name: Upload release asset
if: runner.os == 'Windows'
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: "./${{ env.archive }}"
asset_name: "${{ env.asset_name }}"
asset_content_type: application/zip
125 changes: 60 additions & 65 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use serde::{Deserialize, Serialize};
use std::{fs::{self, File}, io::{Write, BufWriter}};
use serde_json;
use crate::config::*;
use crate::{HmacSha512, Error, Mac, ToHex};
use std::sync::{Arc, Mutex};
use crate::{Error, HmacSha512, Mac, ToHex};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use std::{
fs::{self, File},
io::{BufWriter, Write},
};

// Enum definition
#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -20,100 +22,93 @@ pub struct Cache {
indexes: Arc<Mutex<Vec<WordChecked>>>,

#[serde(skip)]
file: String
file: String,
}
impl Default for Cache {
fn default() -> Self {
Cache{
indexes: Arc::new(Mutex::new(vec![WordChecked::None; 12])),
file: String::new()
}
Cache {
indexes: Arc::new(Mutex::new(vec![WordChecked::None; 0])),
file: String::new(),
}
}
}

impl Cache {
pub fn load()-> Result<Cache, Error>{
let hmac: HmacSha512 = HmacSha512::new_from_slice(format!("kaspa-wallet-recovery-{}-", MNEMONIC).as_bytes()).map_err(kaspa_bip32::Error::Hmac)?;
let hash = hmac.finalize().into_bytes().to_vec().to_hex().split_off(100);
pub fn load() -> Result<Cache, Error> {
let hmac: HmacSha512 =
HmacSha512::new_from_slice(format!("kaspa-wallet-recovery-{}-", MNEMONIC).as_bytes())
.map_err(kaspa_bip32::Error::Hmac)?;
let hash = hmac
.finalize()
.into_bytes()
.to_vec()
.to_hex()
.split_off(100);
let file_path = format!("cache/{}.json", hash);
fs::create_dir_all("cache").expect("could not create cache dir.");
let mut c = read_json(&file_path).unwrap_or_default();
c.file = file_path;
Ok(c)
}

pub fn is_checked(&self, index: usize, word: &String)->bool{
match self.indexes.lock().unwrap().get(index){
Some(c)=>{
match c {
WordChecked::All=>true,
WordChecked::None=>false,
WordChecked::Words(list)=>{
list.contains(word)
}

}
}
None=>false
pub fn is_checked(&self, index: usize, word: &String) -> bool {
match self.indexes.lock().unwrap().get(index) {
Some(c) => match c {
WordChecked::All => true,
WordChecked::None => false,
WordChecked::Words(list) => list.contains(word),
},
None => false,
}
}



pub fn mark_checked(&self, index: usize, word: &str){
let insert = match self.indexes.lock().unwrap().get_mut(index){
Some(c)=>{
match c {
WordChecked::All=>false,
WordChecked::None=>true,
WordChecked::Words(list)=>{
list.insert(word.into());
false
}

pub fn mark_checked(&self, index: usize, word: &str) {
let insert = match self.indexes.lock().unwrap().get_mut(index) {
Some(c) => match c {
WordChecked::All => false,
WordChecked::None => true,
WordChecked::Words(list) => {
list.insert(word.into());
false
}
}
None=>true
},
None => true,
};
if insert {
self.indexes.lock().unwrap().insert(index, WordChecked::Words([word.to_string()].into()))
self.indexes
.lock()
.unwrap()
.insert(index, WordChecked::Words([word.to_string()].into()))
}
}

pub fn mark_all(&self, index: usize, all_words: &Vec<&str>){
let insert = match self.indexes.lock().unwrap().get(index){
Some(c)=>{
match c {
WordChecked::All=>false,
WordChecked::None=>false,
WordChecked::Words(list)=>{
all_words.iter().all(|word| list.contains(*word))
}

}
}
None=>false
pub fn mark_all(&self, index: usize, all_words: &[&str]) {
let insert = match self.indexes.lock().unwrap().get(index) {
Some(c) => match c {
WordChecked::All => false,
WordChecked::None => false,
WordChecked::Words(list) => all_words.iter().all(|word| list.contains(*word)),
},
None => false,
};

if insert {
self.indexes.lock().unwrap().insert(index, WordChecked::All);
self.indexes.lock().unwrap()[index] = WordChecked::All;
self.save();
}
}
}

// pub fn mark_clear(&mut self, index: usize){
// self.indexes.insert(index, WordChecked::None);
// self.indexes.insert(index, WordChecked::None);
// }

pub fn save(&self){
write_json(&self.file, self).map_err(|err|{
println!("write_json failed : {:?}", err)
}).ok();
pub fn save(&self) {
write_json(&self.file, self)
.map_err(|err| println!("write_json failed : {:?}", err))
.ok();
}

}


// Read from JSON file
fn read_json(file_path: &str) -> Result<Cache, Box<dyn std::error::Error>> {
let json_str = fs::read_to_string(file_path)?;
Expand All @@ -128,4 +123,4 @@ fn write_json(file_path: &str, cache: &Cache) -> Result<(), Box<dyn std::error::
let mut buf_writer = BufWriter::new(file);
buf_writer.write_all(json_str.as_bytes())?;
Ok(())
}
}
Loading

0 comments on commit f813141

Please sign in to comment.