Last active
April 15, 2022 05:40
-
-
Save GiambiHuang/2155fdee0711caaef7022656323b9119 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const findoraWasm = await import('findora-wasm'); | |
export async function abarToBar(data: FindoraWallet.IWalletWrap, randomizerSource: string, randomizerFee: string) { | |
const { walletEnd, anonWallet } = data; | |
const { axfrSecretKey, axfrPublicKey, decKey } = anonWallet; | |
let transactionBuilder = await getTransactionBuilder(); | |
const receiverXfrPublicKey = findoraWasm.public_key_from_base64(walletEnd.publickey); | |
const aXfrKeyPairSender = findoraWasm.axfr_keypair_from_string(axfrSecretKey); | |
const secretDecKeySender = findoraWasm.x_secretkey_from_string(decKey); | |
// randomizerSource: randomizer for abar to be sent | |
// randomizerFee: randomizer to pay fee | |
let ownedAbarToUseAsSource = null; | |
let ownedAbarToUseAsFee = null; | |
try { | |
[[ownedAbarToUseAsSource], [ownedAbarToUseAsFee]] = await Promise.all([ | |
getOwnedAbars(axfrPublicKey, randomizerSource), | |
getOwnedAbars(axfrPublicKey, randomizerFee), | |
]); | |
console.log('🚀 ~ abarToBar ~ ownedAbarToUseAsSource', ownedAbarToUseAsSource); | |
console.log('🚀 ~ abarToBar ~ ownedAbarToUseAsFee', ownedAbarToUseAsFee); | |
} catch (error) { | |
throw new Error(`Could not get owned abars, Error - ${(error as Error).message}`); | |
} | |
// add_operation_abar_to_bar | |
try { | |
console.time('add_operation_abar_to_bar'); | |
const { | |
abarData: { atxoSid, ownedAbar }, | |
} = ownedAbarToUseAsSource; | |
const myOwnedAbar = findoraWasm.abar_from_json(ownedAbar); | |
const [myMemoData, mTLeafInfo] = await Promise.all([ | |
txnServer.getOwnerAbarMemo(atxoSid), | |
txnServer.getMTLeafInfo(atxoSid), | |
]); | |
const abarOwnerMemo = findoraWasm.OwnerMemo.from_json(myMemoData); | |
const myMTLeafInfo = findoraWasm.MTLeafInfo.from_json(mTLeafInfo); | |
transactionBuilder = transactionBuilder.add_operation_abar_to_bar( | |
myOwnedAbar, | |
abarOwnerMemo, | |
myMTLeafInfo, | |
aXfrKeyPairSender, | |
secretDecKeySender, | |
receiverXfrPublicKey, | |
false, | |
false, | |
); | |
console.timeEnd('add_operation_abar_to_bar'); | |
} catch (error) { | |
throw new Error(`Could not add abar to bar operation", Error - ${(error as Error).message}`); | |
} | |
// add_operation_anon_fee | |
try { | |
console.time('add_operation_anon_fee'); | |
const { | |
abarData: { atxoSid, ownedAbar }, | |
} = ownedAbarToUseAsFee; | |
const myOwnedAbar = findoraWasm.abar_from_json(ownedAbar); | |
const [myMemoData, mTLeafInfo] = await Promise.all([ | |
txnServer.getOwnerAbarMemo(atxoSid), | |
txnServer.getMTLeafInfo(atxoSid), | |
]); | |
const abarOwnerMemo = findoraWasm.OwnerMemo.from_json(myMemoData); | |
const myMTLeafInfo = findoraWasm.MTLeafInfo.from_json(mTLeafInfo); | |
transactionBuilder = transactionBuilder.add_operation_anon_fee( | |
myOwnedAbar, | |
abarOwnerMemo, | |
myMTLeafInfo, | |
aXfrKeyPairSender, | |
secretDecKeySender, | |
); | |
console.timeEnd('add_operation_anon_fee'); | |
} catch (error) { | |
throw new Error(`Could not anon fee operation", Error - ${(error as Error).message}`); | |
} | |
let randomizers: { randomizers: string[] }; | |
try { | |
randomizers = transactionBuilder?.get_randomizers(); | |
console.log('🚀 ~ randomizers', randomizers); | |
} catch (error) { | |
throw new Error(`could not get a list of randomizers strings "${(error as Error).message}" `); | |
} | |
const submitHandle = await services.txnServer.submitTransaction(transactionBuilder.transaction()); | |
console.log(submitHandle); | |
await sleep(3000); | |
const txnHash = await services.txnServer.getHashSwap(submitHandle); | |
console.log('txnHash', txnHash); | |
return txnHash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment