Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desafio criando uma carteira de criptomoedas #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
//importando as dependencias
//bip32 - significa bitcoin improvement proposal número 32, 39, etc
const bip32 = require('bip32')
const bip39 = require('bip39')
const bitcoin = require('bitcoinjs-lib')

//definir a rede
//bitcoin - rede principal - mainnet
//testnet - rede de teste - tesnet
//testnet - rede de teste - testnet
const network = bitcoin.networks.testnet

//derivação de carteiras HD
const path = `m/49'/1'/0'/0`
//derivação de carteiras HD (Hierarquica ou Deterministica)
const path = "m/49'/1'/0'/0"

//criando o mnemonic para a seed (palavras de senha)
let mnemonic = bip39.generateMnemonic()
const seed = bip39.mnemonicToSeedSync(mnemonic)
const seed = bip39.mnemonicToSeedSync()

//criando a raiz da cartiera HD
//criando a raiz da carteira HD
let root = bip32.fromSeed(seed, network)

//criando uma conta - par pvt-pub keys
let account = root.derivePath(path)
let node = account.derive(0).derive(0)

let btcAddress = bitcoin.payments.p2pkh({
let btcAddress = bitcoin.payments.p2wpkh({

pubkey: node.publicKey,

network: network,

}).address

console.log("Carteira gerada")
console.log("Endereço: ", btcAddress)
console.log("Chave privada:", node.toWIF())
console.log("Seed:", mnemonic)
console.log("Seed", mnemonic)

//após gerar a chave privada, é preciso importar a chave gerada para dentro de um sofware regenciador de carteiras

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,65 +1,78 @@
pragma solidity ^0.8.0;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.8; //compilador

interface IERC20{
interface IERC20 { //criando interface no padrão IERC20

//getters
function totalSupply() external view returns(uint256);
//CRIANDO MÉTODOS/FUNÇÕES

//suprimento total de tokens no meu contato
function totalSupply() external view returns (uint256);

//checa o saldo de um endereço específico
function balanceOf(address account) external view returns (uint256);

//limite de saldo disponibilizado para que outro endereço(usuário) possa usar (como se fosse uma procuração para alguem sacar da minha conta bancária)
function allowance(address owner, address spender) external view returns (uint256);

//functions
//tranferencia de tokens
function transfer(address recipient, uint256 amount) external returns (bool);

//aprovando uma transação de alguém que recebeu permissão de gastar o saldo que disponibilizei
function approve(address spender, uint256 amount) external returns (bool);

//informando o que estou gastanto do allowance disponibilizado
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

//EVENTOS DISPARADOS
event Transfer(address indexed from, address indexed to, uint256 value);

event Approval(address indexed owner, address indexed spender, uint256);

}

contract DIOToken is IERC20{
contract DioCoin is IERC20{

string public constant name = "DIO Token";
string public constant name = "DIO Coin";
string public constant symbol = "DIO";
uint8 public constant decimals = 18;

mapping (address => uint256) balances;

mapping(address => mapping(address=>uint256)) allowed;
mapping (address => uint256) balances;
mapping (address =>mapping (address=>uint256)) allowed;

uint256 totalSupply_ = 10 ether;

constructor(){
balances[msg.sender] = totalSupply_;
}

function totalSupply() public override view returns (uint256) {
function totalSupply() public override view returns (uint256){
return totalSupply_;
}

function balanceOf(address tokenOwner) public override view returns (uint256){
function balanceOf(address tokenOwner) public override view returns(uint256){
return balances[tokenOwner];
}


function transfer(address receiver, uint256 numTokens) public override returns (bool) {
require(numTokens <= balances[msg.sender]);
balances[msg.sender] = balances[msg.sender]-numTokens;
balances[receiver] = balances[receiver]+numTokens;
emit Transfer(msg.sender, receiver, numTokens);
return true;
require(numTokens <= balances[msg.sender]); //exige que a quantidade do salddo que esta enviando
balances[msg.sender] = balances[msg.sender]-numTokens; //o emitente decrementa o valor do saldo
balances[receiver] = balances[receiver]+numTokens; //o recebedor incrementa o valor no saldo
emit Transfer(msg.sender, receiver, numTokens); //dispara um evento dos enderecos e a quantidade
return true;
}

function approve(address delegate, uint256 numTokens) public override returns (bool) {
function approve(address delegate, uint256 numTokens) public override returns (bool){
allowed[msg.sender][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;
}

function allowance(address owner, address delegate) public override view returns (uint) {
function allowance(address owner, address delegate) public override view returns (uint){
return allowed[owner][delegate];
}

function transferFrom(address owner, address buyer, uint256 numTokens) public override returns (bool) {
function transferFrom(address owner, address buyer, uint256 numTokens) public override returns (bool){
require(numTokens <= balances[owner]);
require(numTokens <= allowed[owner][msg.sender]);

Expand All @@ -70,4 +83,4 @@ contract DIOToken is IERC20{
return true;
}

}
}