Skip to content

Instantly share code, notes, and snippets.

View harshvishu's full-sized avatar
🦀
Learning

हर्ष • Harsh • ハラシャ harshvishu

🦀
Learning
View GitHub Profile
@harshvishu
harshvishu / server.rs
Last active March 9, 2021 08:01
server.rs file for rust blockchain blog
use crate::blockchain::Chain;
use rocket::*;
use rocket_contrib::json::{Json, JsonValue};
use serde::{Deserialize, Serialize};
use std::{sync::Mutex};
use rocket::config::{Config, Environment};
use rocket::response::{Redirect, Response};
use rocket::http::{Status, ContentType};
use std::io::Cursor;
/**
Creates a SHA-256 hash of a Block
- Parameter block: Block
- returns: String
*/
pub fn hash(block: &Block) -> String {
let json = serde_json::to_string(block).unwrap();
let readable_string = Cursor::new(&json);
/**
Creates a new transaction to go into the next mined Block
- Parameter sender: Address of the Sender
- Parameter recipient: Address of the Recipient
- Parameter amount: Amount
- returns: The index of the Block that will hold this transaction
*/
pub fn new_transaction(&mut self, sender: String, recipient: String, amount: u64) -> u64 {
@harshvishu
harshvishu / Block.rs
Last active March 4, 2021 16:03
Block & Chain skeleton
pub struct Block {
index: u64,
timestamp: DateTime<Utc>,
transactions: Vec<Transaction>,
proof: u64,
previous_hash: String,
}
@harshvishu
harshvishu / Cargo.toml
Last active March 9, 2021 08:42
'Blockchain in Rust' snippets
[package]
name = "blockchain"
version = "0.1.0"
authors = ["Harsh Vishwakarma"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = { version = " 0.4", features = ["serde"] }
//
// ConnectorView.swift
// Helper
//
// Created by Harsh 20/08/20.
// Copyright © 2020 Harsh. All rights reserved.
//
import UIKit
@harshvishu
harshvishu / PercentLayoutConstraint.swift
Created August 20, 2020 07:25
PercentLayoutConstraint
// PercentLayoutConstraint.swift
import Foundation
import UIKit
@IBDesignable
final class PercentLayoutConstraint: NSLayoutConstraint {
@IBInspectable var marginPercent: CGFloat = 0
var screenSize: (width: CGFloat, height: CGFloat) {
@harshvishu
harshvishu / UIViewController+ShorthandAlerts.swift
Created August 1, 2020 06:55 — forked from aydenp/UIViewController+ShorthandAlerts.swift
Creating alerts on iOS is annoying. This extension allows you to easily present alerts on view controllers. It also improves UIAlertAction by adding a quicker more Swift-like shorthand for making actions.
//
// UIViewController+ShorthandAlerts.swift
//
// Created by Ayden Panhuyzen on 2017-07-31.
// Copyright © 2017-2018 Ayden Panhuyzen. All rights reserved.
// https://gist.github.com/aydenp
//
import UIKit
public typealias CompletionHandler = () -> Void
class LiftOffViewController : UIViewController {
var countDownTimer: CountDown?
@IBOutlet weak var labelTimeLeft: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//
// CountDownTimer.swift
//
// Created by Harsh Vishwakarma.
//
import Foundation
import UIKit
final class CountDownTimer: NSObject, CountDown {