本文整理汇总了TypeScript中bcrypto.secp256k1.publicKeyCreate方法的典型用法代码示例。如果您正苦于以下问题:TypeScript secp256k1.publicKeyCreate方法的具体用法?TypeScript secp256k1.publicKeyCreate怎么用?TypeScript secp256k1.publicKeyCreate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bcrypto.secp256k1
的用法示例。
在下文中一共展示了secp256k1.publicKeyCreate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getAddressFromPrivateKey
/**
* get the public address of an account using its private key
* @param {string} privateKey - the private key of the account
* @returns {string} public address of the account
*/
getAddressFromPrivateKey(privateKey): string {
if (typeof(privateKey) == 'string') privateKey = new Buffer(privateKey, 'hex')
let pubKey = secp256k1.publicKeyCreate(privateKey, true)
let pubKeyHash = sha256.digest(pubKey) // sha256 hash of the public key
let address = pubKeyHash.toString('hex', 12) // rightmost 160 bits/20 bytes of the hash
return address
}
示例2: sendPayment
/**
* create a new transaction
* @param {Object} payment - the details of the transaction
* @param {string} payment.to - address to which transaction is sent to
* @param {number} payment.amount - number of zils sent
* @param {number} payment.gasPrice - gas price for this transaction
* @param {number} payment.gasLimit - gas limit for this transaction
* @returns {Promise} Promise object containing the newly created transaction id
*/
sendPayment(payment: any): Promise<any> {
this.startLoading()
var deferred = new $.Deferred()
let pubKey = secp256k1.publicKeyCreate(new Buffer(this.userWallet.privateKey, 'hex'), true)
let txn = {
version: 0,
nonce: this.userWallet.nonce + 1,
to: payment.address,
amount: payment.amount,
pubKey: pubKey.toString('hex'),
gasPrice: payment.gasPrice,
gasLimit: payment.gasLimit
}
var msg = this.intToByteArray(txn.version, 8).join('') +
this.intToByteArray(txn.nonce, 64).join('') +
txn.to +
txn.pubKey +
this.intToByteArray(txn.amount, 64).join('')
let sig = this.zlib.schnorr.sign(new Buffer(msg, 'hex'), new Buffer(this.userWallet.privateKey, 'hex'), pubKey)
let r = sig.r.toString('hex')
let s = sig.s.toString('hex')
while (r.length < 64) {
r = '0' + r
}
while (s.length < 64) {
s = '0' + s
}
txn['signature'] = r + s
let that = this
this.node.createTransaction(txn, function(err, data) {
if (err || data.error) {
deferred.reject(err)
} else {
deferred.resolve({
txId: data.result
})
}
that.endLoading()
})
return deferred.promise()
}