本文整理汇总了TypeScript中jQuery.Deferred.promise方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Deferred.promise方法的具体用法?TypeScript Deferred.promise怎么用?TypeScript Deferred.promise使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jQuery.Deferred
的用法示例。
在下文中一共展示了Deferred.promise方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: importWallet
/**
* import an account wallet from server using a private key
* @param {string} privateKey - the private key of the account being imported
* @returns {Promise} Promise object containing boolean - if imported successfully or not
*/
importWallet(privateKey): Promise<any> {
this.startLoading()
var deferred = new $.Deferred()
if (!!(privateKey.match(/[0-9a-fA-F]{64}/)) == false) {
deferred.reject({
error: 'Invalid private key.'
})
return deferred.promise()
}
if (typeof(privateKey) == 'string') privateKey = new Buffer(privateKey, 'hex')
// check if private key valid
try {
if (secp256k1.privateKeyVerify(privateKey)) {
let addr = this.getAddressFromPrivateKey(privateKey)
// get balance from API
let that = this
this.node.getBalance({address: addr}, function(err, data) {
if (err || data.error) {
deferred.reject({error: err})
} else {
that.userWallet = {
address: addr,
balance: data.result.balance,
nonce: data.result.nonce,
privateKey: privateKey.toString('hex')
}
deferred.resolve({
result: true
})
}
that.endLoading()
})
} else {
deferred.reject({
error: 'Invalid private key.'
})
this.endLoading()
}
} catch (e) {
deferred.reject({
error: e
})
this.endLoading()
}
return deferred.promise()
}
示例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()
}
示例3: 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()
}
示例4: 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();
}
示例5: 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()
}
示例6: decryptWalletFile
/**
* process a keystore json file to retrieve the private key and import the account data
* uses this.walletData to import data and this.userWallet to store data separately
* @param {string} passphrase - the passphrase of the keystore json entered by the user
* @returns {Promise} Promise object containing boolean - if imported successfully or not
*/
decryptWalletFile(passphrase): Promise<any> {
let that = this
var deferred = new $.Deferred()
// use the passphrase and keystore json file to get private key
if (passphrase && passphrase.length >= 8) {
let walletJson = JSON.parse(this.walletData.encryptedWalletFile)
let ciphertext = new Buffer(walletJson['crypto']['ciphertext'], 'hex')
let iv = new Buffer(walletJson['crypto']['cipherparams']['iv'], 'hex')
let salt = new Buffer(walletJson['crypto']['kdfparams']['salt'], 'hex')
let kdfparams = walletJson['crypto']['kdfparams']
// recreate the derived key using scrypt and the same parameters
scryptAsync(passphrase, salt, {N: kdfparams['n'], r: kdfparams['r'], p: kdfparams['p'], dkLen: kdfparams['dklen']}, function(derivedKey) {
derivedKey = new Buffer(derivedKey, 'hex')
// check passphrase using mac
let mac = sha3.digest(Buffer.concat([derivedKey.slice(16, 32), ciphertext])).toString('hex')
if (mac.toLowerCase() !== walletJson['crypto']['mac'].toLowerCase()) {
// Incorrect passphrase
deferred.resolve({result: false})
}
let aesctr = new aesjs.ModeOfOperation.ctr(derivedKey.slice(0, 16), new aesjs.Counter(iv))
let decryptedSeed = aesctr.decrypt(ciphertext);
deferred.resolve({
result: new Buffer(decryptedSeed).toString('hex')
})
})
} else {
deferred.resolve({result: false})
}
return deferred.promise()
}