Last active
May 11, 2021 13:21
-
-
Save Anderson-Juhasc/e6975cc51116742b650d1d9cefffa4af to your computer and use it in GitHub Desktop.
dogecoin js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
NETWORKS: { | |
dogecoin: { | |
messagePrefix: '\x19Dogecoin Signed Message:\n', | |
bip32: { | |
public: 0x02facafd, | |
private: 0x02fac398 | |
}, | |
pubKeyHash: 0x1e, | |
scriptHash: 0x16, | |
wif: 0x9e | |
}, | |
dogecointestnet: { | |
messagePrefix: '\x19Dogecoin Signed Message:\n', | |
bip32: { | |
public: 0x043587cf, | |
private: 0x04358394 | |
}, | |
pubKeyHash: 0x71, | |
scriptHash: 0xc4, | |
wif: 0xf1 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bjs = require('bitcoinjs-lib') | |
, bip39 = require('bip39') | |
, Dogecore = require('bitcore-lib-doge') | |
, Unit = Dogecore.Unit | |
, axios = require('axios') | |
, { NETWORKS } = require('./constants') | |
function Dogecoin(mnemonic, password, network) { | |
this.testnet = network ? true : false | |
this.network = network ? NETWORKS.dogecointestnet : NETWORKS.dogecoin | |
this.seed = bip39.mnemonicToSeedSync(mnemonic, password ? password : '') | |
this.root = bjs.bip32.fromSeed(this.seed, this.network) | |
this.child = this.root.derivePath("m/44'/3'/0'") | |
} | |
Dogecoin.prototype.getPrivateKey = function(index, isChange) { | |
let privateKey = this.child.derive(isChange === true ? 1 : 0).derive(index).toWIF() | |
return privateKey | |
} | |
Dogecoin.prototype.getAddress = function(index, isChange) { | |
let address = this.child.derive(isChange === true ? 1 : 0).derive(index) | |
return bjs.payments.p2pkh({ pubkey: address.publicKey, network: this.network }).address | |
} | |
Dogecoin.prototype.getUnspentTxs = async function(address) { | |
try { | |
const network = this.testnet ? 'DOGETEST' : 'DOGE' | |
, res = await axios.get(`https://chain.so/api/v2/get_tx_unspent/${network}/${address}`, { timeout: 10000 }) | |
, utxos = res.data.data.txs | |
return utxos | |
} catch (e) { | |
return e.response | |
} | |
} | |
Dogecoin.prototype.estimateFee = async function(from, to, amount) { | |
const utxos = await this.getUnspentTxs(from) | |
, txs = [] | |
for (let i = 0, len = utxos.length; i < len; i++) { | |
txs.push({ | |
txId: utxos[i].txid, | |
outputIndex: utxos[i].output_no, | |
address: from, | |
script: utxos[i].script_hex, | |
satoshis: Unit.fromBTC(utxos[i].value).toSatoshis() | |
}) | |
} | |
const transaction = new Dogecore.Transaction() | |
.from(txs) | |
.to(to, Unit.fromBTC(amount).toSatoshis()) | |
.change(from) | |
return transaction._estimateFee() | |
} | |
Dogecoin.prototype.createTx = async function(from, privateKey, to, amount, fee) { | |
const prvKey = new Dogecore.PrivateKey(privateKey) | |
, utxos = await this.getUnspentTxs(from) | |
, txs = [] | |
for (let i = 0, len = utxos.length; i < len; i++) { | |
txs.push({ | |
txId: utxos[i].txid, | |
outputIndex: utxos[i].output_no, | |
address: from, | |
script: utxos[i].script_hex, | |
satoshis: Unit.fromBTC(utxos[i].value).toSatoshis() | |
}) | |
} | |
const transaction = new Dogecore.Transaction() | |
.from(txs) | |
.to(to, Unit.fromBTC(amount).toSatoshis()) | |
.change(from) | |
.fee(fee) | |
.sign(prvKey) | |
return transaction.toString() | |
} | |
Dogecoin.prototype.sendTx = async function(txHex) { | |
try { | |
const network = this.testnet ? 'DOGETEST' : 'DOGE' | |
const post = await axios({ | |
method: 'POST', | |
url: `https://chain.so/api/v2/send_tx/${network}/`, | |
data: { | |
tx_hex: txHex | |
} | |
}) | |
return post.data.data | |
} catch (e) { | |
return e.response | |
} | |
} | |
Dogecoin.prototype.getBalance = async function(address) { | |
try { | |
const network = this.testnet ? 'DOGETEST' : 'DOGE' | |
, res = await axios.get(`https://chain.so/api/v2/get_address_balance/${network}/${address}/`, { timeout: 10000 }) | |
return res.data.data | |
} catch (e) { | |
return e.response | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment