本文整理匯總了TypeScript中jQuery.Deferred類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Deferred類的具體用法?TypeScript Deferred怎麽用?TypeScript Deferred使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Deferred類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: 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()
}
示例2: generateWalletJson
/**
* create a keystore json (wallet file) for an account containing the encrypted private key
* @param {string} passphrase - the passphrase to be used to encrypt the wallet
* @returns {string} string of the keystore json
*/
generateWalletJson(passphrase) {
var deferred = new $.Deferred()
var that = this
let privateKey = new Buffer(this.userWallet.privateKey, 'hex')
let address = this.userWallet.address
let salt = randomBytes(32);
let iv = randomBytes(16);
// key derivation function used is scrypt along with standard params
scryptAsync(passphrase, salt, {N: 262144, r: 1, p: 8, dkLen: 32}, function(derivedKey) {
derivedKey = new Buffer(derivedKey)
// ciphertext is private key encrypted with aes-128-ctr
let aesctr = new aesjs.ModeOfOperation.ctr(derivedKey.slice(0, 16), new aesjs.Counter(iv))
let ciphertext = aesctr.encrypt(privateKey)
var result = {
address: address,
crypto: {
cipher: "aes-128-ctr",
cipherparams: {
iv: iv.toString('hex'),
},
ciphertext: new Buffer(ciphertext).toString('hex'),
kdf: "scrypt",
kdfparams: {
dklen: 32,
n: 262144,
r: 1,
p: 8,
salt: salt.toString('hex'),
},
mac: sha3.digest(Buffer.concat([derivedKey.slice(16, 32), new Buffer(ciphertext)])).toString('hex'),
},
id: uuid.v4({random: randomBytes(16)}),
version: 3,
}
deferred.resolve({
result: JSON.stringify(result)
})
})
return deferred.promise()
}
示例3: getInitData
/**
* fetch miscellaneous data like networkId/latest Tx blocknum
* @returns {Promise} Promise object containing the required data
*/
getInitData(): Promise<any> {
var deferred = new $.Deferred();
var that = this;
that.node.getNetworkId(function(err, data1) {
if (err || !data1.result) {
deferred.reject(err)
} else {
that.node.getLatestTxBlock(function(err, data2) {
if (err || !data2.result) {
deferred.reject(err)
} else {
deferred.resolve({
networkId: data1.result,
latestTxBlock: data2.result.header.BlockNum
})
}
})
}
})
return deferred.promise();
}
示例4: refreshBalance
/**
* get the account details of an account using its public address
* @param {string} address - the public address of the account
* @returns {Promise} Promise object containing address, balance and nonce of the account
*/
refreshBalance(): Promise<any> {
var deferred = new $.Deferred()
let that = this
this.node.getBalance({address: this.userWallet.address}, function(err, data) {
if (err || data.error) {
deferred.reject({error: err})
} else {
let newUserWallet = {
address: that.userWallet.address,
balance: data.result.balance,
nonce: data.result.nonce,
privateKey: that.userWallet.privateKey
}
that.userWallet = newUserWallet
deferred.resolve({
result: true
})
}
})
return deferred.promise()
}
示例5:
that.node.getLatestTxBlock(function(err, data2) {
if (err || !data2.result) {
deferred.reject(err)
} else {
deferred.resolve({
networkId: data1.result,
latestTxBlock: data2.result.header.BlockNum
})
}
})
示例6: function
this.node.createTransaction(txn, function(err, data) {
if (err || data.error) {
deferred.reject(err)
} else {
deferred.resolve({
txId: data.result
})
}
that.endLoading()
})
示例7: scryptAsync
scryptAsync(passphrase, salt, {N: 262144, r: 1, p: 8, dkLen: 32}, function(derivedKey) {
derivedKey = new Buffer(derivedKey)
// ciphertext is private key encrypted with aes-128-ctr
let aesctr = new aesjs.ModeOfOperation.ctr(derivedKey.slice(0, 16), new aesjs.Counter(iv))
let ciphertext = aesctr.encrypt(privateKey)
var result = {
address: address,
crypto: {
cipher: "aes-128-ctr",
cipherparams: {
iv: iv.toString('hex'),
},
ciphertext: new Buffer(ciphertext).toString('hex'),
kdf: "scrypt",
kdfparams: {
dklen: 32,
n: 262144,
r: 1,
p: 8,
salt: salt.toString('hex'),
},
mac: sha3.digest(Buffer.concat([derivedKey.slice(16, 32), new Buffer(ciphertext)])).toString('hex'),
},
id: uuid.v4({random: randomBytes(16)}),
version: 3,
}
deferred.resolve({
result: JSON.stringify(result)
})
})