u
This commit is contained in:
		
							parent
							
								
									07a88d9bc7
								
							
						
					
					
						commit
						0bc2108351
					
				
							
								
								
									
										24
									
								
								btc.js
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								btc.js
									
									
									
									
									
								
							@ -3,15 +3,15 @@
 | 
				
			|||||||
const axios = require('axios')
 | 
					const axios = require('axios')
 | 
				
			||||||
const HDNode = require('./utils/hdnode')
 | 
					const HDNode = require('./utils/hdnode')
 | 
				
			||||||
const bitcoinjs = require('bitcoinjs-lib')
 | 
					const bitcoinjs = require('bitcoinjs-lib')
 | 
				
			||||||
const Ticrypto = require('tic-crypto')
 | 
					const ticc = require('tic-crypto')
 | 
				
			||||||
const BTC_NODE = require('./netConfig').BTC_NODE
 | 
					const BTC_NODE = require('./netConfig').BTC_NODE
 | 
				
			||||||
const BTC_NODE2 = require('./netConfig').BTC_NODE2
 | 
					const BTC_NODE2 = require('./netConfig').BTC_NODE2
 | 
				
			||||||
const BTC_TXFEE = 30
 | 
					const BTC_TXFEE = 30
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BTC {
 | 
					class BTC {
 | 
				
			||||||
  constructor (privateKey) {
 | 
					  constructor (privateKey) {
 | 
				
			||||||
    if (!Ticrypto.isSeckey(privateKey)) throw new Error('Invalid PrivateKey')
 | 
					    if (!ticc.is_seckey(privateKey)) throw new Error('Invalid PrivateKey')
 | 
				
			||||||
    var publicKey = Ticrypto.seckey2pubkey(privateKey)
 | 
					    var publicKey = ticc.seckey_to_pubkey(privateKey)
 | 
				
			||||||
    Object.defineProperties(this, {
 | 
					    Object.defineProperties(this, {
 | 
				
			||||||
      privateKey: {
 | 
					      privateKey: {
 | 
				
			||||||
        enumerable: true,
 | 
					        enumerable: true,
 | 
				
			||||||
@ -21,12 +21,12 @@ class BTC {
 | 
				
			|||||||
      publicKey: {
 | 
					      publicKey: {
 | 
				
			||||||
        enumerable: true,
 | 
					        enumerable: true,
 | 
				
			||||||
        writable: false,
 | 
					        writable: false,
 | 
				
			||||||
        value: Ticrypto.seckey2pubkey(privateKey, { coin: 'BTC' })
 | 
					        value: ticc.seckey_to_pubkey({ seckey: privateKey, coin: 'BTC' })
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
      address: {
 | 
					      address: {
 | 
				
			||||||
        enumerable: true,
 | 
					        enumerable: true,
 | 
				
			||||||
        writable: false,
 | 
					        writable: false,
 | 
				
			||||||
        value: Ticrypto.pubkey2address(publicKey, { coin: 'BTC' })
 | 
					        value: ticc.pubkey_to_address({ pubkey: publicKey, coin: 'BTC' })
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
      url: {
 | 
					      url: {
 | 
				
			||||||
        enumerable: true,
 | 
					        enumerable: true,
 | 
				
			||||||
@ -57,16 +57,20 @@ class BTC {
 | 
				
			|||||||
    this._defaultGasFee = BTC_TXFEE
 | 
					    this._defaultGasFee = BTC_TXFEE
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  static generateNewAccount () {
 | 
					  static generateNewAccount () {
 | 
				
			||||||
    var mnemonic = Ticrypto.randomSecword()
 | 
					    var mnemonic = ticc.randomize_secword()
 | 
				
			||||||
    return Object.assign(
 | 
					    return Object.assign(
 | 
				
			||||||
      new BTC(Ticrypto.secword2keypair(mnemonic, { coin: 'BTC' }).seckey),
 | 
					      new BTC(
 | 
				
			||||||
 | 
					        ticc.secword_to_keypair({ secword: mnemonic, coin: 'BTC' }).seckey
 | 
				
			||||||
 | 
					      ),
 | 
				
			||||||
      { mnemonic: mnemonic }
 | 
					      { mnemonic: mnemonic }
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  static fromMnemonic (mnemonic) {
 | 
					  static fromMnemonic (mnemonic) {
 | 
				
			||||||
    HDNode.isValidMnemonic(mnemonic)
 | 
					    HDNode.isValidMnemonic(mnemonic)
 | 
				
			||||||
    return Object.assign(
 | 
					    return Object.assign(
 | 
				
			||||||
      new BTC(Ticrypto.secword2keypair(mnemonic, { coin: 'BTC' }).seckey),
 | 
					      new BTC(
 | 
				
			||||||
 | 
					        ticc.secword_to_keypair({ secword: mnemonic, coin: 'BTC' }).seckey
 | 
				
			||||||
 | 
					      ),
 | 
				
			||||||
      { mnemonic: mnemonic }
 | 
					      { mnemonic: mnemonic }
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -88,10 +92,10 @@ class BTC {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  static encrypt (data, key) {
 | 
					  static encrypt (data, key) {
 | 
				
			||||||
    if (!data || !key) throw new Error('Required Params Missing')
 | 
					    if (!data || !key) throw new Error('Required Params Missing')
 | 
				
			||||||
    return Ticrypto.encrypt(data, key)
 | 
					    return ticc.encrypt(data, key)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  static decrypt (data, key) {
 | 
					  static decrypt (data, key) {
 | 
				
			||||||
    return Ticrypto.decrypt(data, key, { format: 'json' }) //return null for wrong key
 | 
					    return ticc.decrypt(data, key, { format: 'json' }) //return null for wrong key
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  static isValidAddress (address) {
 | 
					  static isValidAddress (address) {
 | 
				
			||||||
    return address.length == 34 && address[0] == '1'
 | 
					    return address.length == 34 && address[0] == '1'
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										8
									
								
								eth.js
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								eth.js
									
									
									
									
									
								
							@ -2,7 +2,7 @@
 | 
				
			|||||||
const eth = require('etherscan-api').init('E3ZFFAEMNN33KX4HHVUZ4KF8XY1FXMR4BI')
 | 
					const eth = require('etherscan-api').init('E3ZFFAEMNN33KX4HHVUZ4KF8XY1FXMR4BI')
 | 
				
			||||||
const secretStorage = require('./utils/secret-storage')
 | 
					const secretStorage = require('./utils/secret-storage')
 | 
				
			||||||
const SigningKey = require('./utils/signing-key.js')
 | 
					const SigningKey = require('./utils/signing-key.js')
 | 
				
			||||||
const Ticrypto = require('tic-crypto')
 | 
					const ticc = require('tic-crypto')
 | 
				
			||||||
const HDNode = require('./utils/hdnode')
 | 
					const HDNode = require('./utils/hdnode')
 | 
				
			||||||
const utils = require('./util.js')
 | 
					const utils = require('./util.js')
 | 
				
			||||||
const axios = require('axios')
 | 
					const axios = require('axios')
 | 
				
			||||||
@ -79,7 +79,7 @@ class ETH {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  static generateNewAccount (option = { path: defaultPath }) {
 | 
					  static generateNewAccount (option = { path: defaultPath }) {
 | 
				
			||||||
    //major path as default path >/0'/0/0
 | 
					    //major path as default path >/0'/0/0
 | 
				
			||||||
    var mnemonic = Ticrypto.randomSecword()
 | 
					    var mnemonic = ticc.randomize_secword()
 | 
				
			||||||
    return Object.assign(ETH.fromMnemonic(mnemonic, option), {
 | 
					    return Object.assign(ETH.fromMnemonic(mnemonic, option), {
 | 
				
			||||||
      mnemonic,
 | 
					      mnemonic,
 | 
				
			||||||
      mnemonic
 | 
					      mnemonic
 | 
				
			||||||
@ -233,10 +233,10 @@ class ETH {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  static encrypt (data, key) {
 | 
					  static encrypt (data, key) {
 | 
				
			||||||
    if (!data || !key) throw new Error('Required Params Missing')
 | 
					    if (!data || !key) throw new Error('Required Params Missing')
 | 
				
			||||||
    return Ticrypto.encrypt(data, key)
 | 
					    return ticc.encrypt(data, key)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  static decrypt (data, key) {
 | 
					  static decrypt (data, key) {
 | 
				
			||||||
    return Ticrypto.decrypt(data, key, { format: 'json' }) //return null for wrong key
 | 
					    return ticc.decrypt(data, key, { format: 'json' }) //return null for wrong key
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  static async estimateGasPrice () {
 | 
					  static async estimateGasPrice () {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										28
									
								
								tic.js
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								tic.js
									
									
									
									
									
								
							@ -1,6 +1,6 @@
 | 
				
			|||||||
'use strict'
 | 
					'use strict'
 | 
				
			||||||
const axios = require('axios')
 | 
					const axios = require('axios')
 | 
				
			||||||
const ticrypto = require('tic-crypto')
 | 
					const ticc = require('tic-crypto')
 | 
				
			||||||
const ticActionTransfer = require('tic.action').ActionTransfer
 | 
					const ticActionTransfer = require('tic.action').ActionTransfer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const TIC_TXFEE = 10
 | 
					const TIC_TXFEE = 10
 | 
				
			||||||
@ -8,7 +8,7 @@ const TIC_NODE = require('./netConfig').TIC_NODE
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class TIC {
 | 
					class TIC {
 | 
				
			||||||
  constructor (seckey, option = {}) {
 | 
					  constructor (seckey, option = {}) {
 | 
				
			||||||
    if (!seckey || !ticrypto.isSeckey(seckey)) throw 'ERROR:Invalid Seckey'
 | 
					    if (!seckey || !ticc.is_seckey(seckey)) throw 'ERROR:Invalid Seckey'
 | 
				
			||||||
    Object.defineProperties(this, {
 | 
					    Object.defineProperties(this, {
 | 
				
			||||||
      seckey: {
 | 
					      seckey: {
 | 
				
			||||||
        value: seckey,
 | 
					        value: seckey,
 | 
				
			||||||
@ -16,12 +16,14 @@ class TIC {
 | 
				
			|||||||
        writable: false
 | 
					        writable: false
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
      pubkey: {
 | 
					      pubkey: {
 | 
				
			||||||
        value: ticrypto.seckey2pubkey(seckey),
 | 
					        value: ticc.seckey_to_pubkey({ seckey }),
 | 
				
			||||||
        enumerable: true,
 | 
					        enumerable: true,
 | 
				
			||||||
        writable: false
 | 
					        writable: false
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
      address: {
 | 
					      address: {
 | 
				
			||||||
        value: ticrypto.pubkey2address(ticrypto.seckey2pubkey(seckey)),
 | 
					        value: ticc.pubkey_to_address({
 | 
				
			||||||
 | 
					          pubkey: ticc.seckey_to_pubkey(seckey)
 | 
				
			||||||
 | 
					        }),
 | 
				
			||||||
        enumerable: true,
 | 
					        enumerable: true,
 | 
				
			||||||
        writable: false
 | 
					        writable: false
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@ -45,14 +47,14 @@ class TIC {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static generateNewAccount () {
 | 
					  static generateNewAccount () {
 | 
				
			||||||
    var secword = ticrypto.randomSecword()
 | 
					    var secword = ticc.randomize_secword()
 | 
				
			||||||
    return Object.assign(new TIC(ticrypto.secword2keypair(secword).seckey), {
 | 
					    return Object.assign(new TIC(ticc.secword_to_keypair({ secword }).seckey), {
 | 
				
			||||||
      secword: secword
 | 
					      secword: secword
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  static fromMnemonic (secword) {
 | 
					  static fromMnemonic (secword) {
 | 
				
			||||||
    if (!secword || !ticrypto.isSecword(secword)) throw 'ERROR:Invalid Secword'
 | 
					    if (!secword || !ticc.is_secword(secword)) throw 'ERROR:Invalid Secword'
 | 
				
			||||||
    return new TIC(ticrypto.secword2keypair(secword).seckey)
 | 
					    return new TIC(ticc.secword_to_keypair({ secword }).seckey)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  static async getBalance (address) {
 | 
					  static async getBalance (address) {
 | 
				
			||||||
    if (!address) {
 | 
					    if (!address) {
 | 
				
			||||||
@ -84,14 +86,14 @@ class TIC {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  static encrypt (data, key) {
 | 
					  static encrypt (data, key) {
 | 
				
			||||||
    if (!data || !key) throw new Error('Required Params Missing')
 | 
					    if (!data || !key) throw new Error('Required Params Missing')
 | 
				
			||||||
    return ticrypto.encrypt(data, key)
 | 
					    return ticc.encrypt(data, key)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  static decrypt (data, key) {
 | 
					  static decrypt (data, key) {
 | 
				
			||||||
    return ticrypto.decrypt(data, key, { format: 'json' }) //return null for wrong key
 | 
					    return ticc.decrypt(data, key, { format: 'json' }) //return null for wrong key
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static isValidAddress (address) {
 | 
					  static isValidAddress (address) {
 | 
				
			||||||
    return ticrypto.isAddress(address)
 | 
					    return ticc.is_chain_address({ address })
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async sendTransaction (toAddress, amount, option = { gasFee: TIC_TXFEE }) {
 | 
					  async sendTransaction (toAddress, amount, option = { gasFee: TIC_TXFEE }) {
 | 
				
			||||||
@ -142,10 +144,10 @@ class TIC {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  //default key for sign&encrypt is account's seckey,other keys are optional.
 | 
					  //default key for sign&encrypt is account's seckey,other keys are optional.
 | 
				
			||||||
  sign (message, key = this.seckey) {
 | 
					  sign (message, key = this.seckey) {
 | 
				
			||||||
    return ticrypto.sign({ data: message, seckey: key })
 | 
					    return ticc.sign({ data: message, seckey: key })
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  verify (message, signature) {
 | 
					  verify (message, signature) {
 | 
				
			||||||
    return ticrypto.sign({ data: message, signature, seckey: this.seckey })
 | 
					    return ticc.sign({ data: message, signature, seckey: this.seckey })
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  encrypt (key) {
 | 
					  encrypt (key) {
 | 
				
			||||||
    return TIC.encrypt(this, key)
 | 
					    return TIC.encrypt(this, key)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user