本文整理汇总了TypeScript中bitcoinjs-lib.crypto类的典型用法代码示例。如果您正苦于以下问题:TypeScript crypto类的具体用法?TypeScript crypto怎么用?TypeScript crypto使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了crypto类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: makeLegacyAuthToken
/**
*
* @param challengeText
* @param signerKeyHex
*
* @ignore
*/
function makeLegacyAuthToken(challengeText: string, signerKeyHex: string): string {
// only sign specific legacy auth challenges.
let parsedChallenge
try {
parsedChallenge = JSON.parse(challengeText)
} catch (err) {
throw new Error('Failed in parsing legacy challenge text from the gaia hub.')
}
if (parsedChallenge[0] === 'gaiahub'
&& parsedChallenge[3] === 'blockstack_storage_please_sign') {
const signer = hexStringToECPair(signerKeyHex
+ (signerKeyHex.length === 64 ? '01' : ''))
const digest = bitcoin.crypto.sha256(Buffer.from(challengeText))
const signatureBuffer = signer.sign(digest)
const signatureWithHash = bitcoin.script.signature.encode(
signatureBuffer, bitcoin.Transaction.SIGHASH_NONE)
// We only want the DER encoding so remove the sighash version byte at the end.
// See: https://github.com/bitcoinjs/bitcoinjs-lib/issues/1241#issuecomment-428062912
const signature = signatureWithHash.toString('hex').slice(0, -2)
const publickey = getPublicKeyFromPrivate(signerKeyHex)
const token = Buffer.from(JSON.stringify(
{ publickey, signature }
)).toString('base64')
return token
} else {
throw new Error('Failed to connect to legacy gaia hub. If you operate this hub, please update.')
}
}
示例2: makeAuthPart
static makeAuthPart(secretKey: bitcoin.ECPair, challengeText: string) {
const publickey = secretKey.publicKey.toString('hex')
const digest = bitcoin.crypto.sha256(Buffer.from(challengeText))
const signatureBuffer = secretKey.sign(digest)
const signatureWithHash = bitcoin.script.signature.encode(signatureBuffer, bitcoin.Transaction.SIGHASH_NONE)
// We only want the DER encoding so remove the sighash version byte at the end.
// See: https://github.com/bitcoinjs/bitcoinjs-lib/issues/1241#issuecomment-428062912
const signature = signatureWithHash.toString('hex').slice(0, -2)
const authObj = { publickey, signature }
return Buffer.from(JSON.stringify(authObj)).toString('base64')
}
示例3: isAuthenticationValid
isAuthenticationValid(address: string, challengeTexts: Array<string>,
options? : {}) { // eslint-disable-line @typescript-eslint/no-unused-vars
if (ecPairToAddress(this.publickey) !== address) {
throw new ValidationError('Address not allowed to write on this path')
}
for (const challengeText of challengeTexts) {
const digest = bitcoin.crypto.sha256(Buffer.from(challengeText))
const valid = (this.publickey.verify(digest, Buffer.from(this.signature, 'hex')) === true)
if (valid) {
return address
}
}
logger.debug(`Failed to validate with challenge text: ${JSON.stringify(challengeTexts)}`)
throw new ValidationError('Invalid signature or expired authentication token.')
}
示例4: publicKeyToAddress
export function publicKeyToAddress(publicKey: string) {
const publicKeyBuffer = Buffer.from(publicKey, 'hex')
const publicKeyHash160 = bcrypto.hash160(publicKeyBuffer)
const address = baddress.toBase58Check(publicKeyHash160, 0x00)
return address
}
示例5: ecPairToAddress
export function ecPairToAddress(keyPair: ECPair) {
return address.toBase58Check(crypto.hash160(keyPair.publicKey), keyPair.network.pubKeyHash)
}