Skip to content

Commit 100d4aa

Browse files
A version of lock and claim which work when base64 version of the preimage is provided to claim
Signed-off-by: Dhinakaran Vinayagamurthy <[email protected]>
1 parent 6267279 commit 100d4aa

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

samples/besu/besu-cli/src/commands/asset/claim.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ const command: GluegunCommand = {
3535
'The index of the account of the recipient of the asset from the list obtained through web3.eth.getAccounts(). For example, we can set Alice as accounts[1] and hence value of this parameter for Alice can be 1.'
3636
},
3737
{
38-
name: '--preimage',
38+
name: '--preimage_base64',
3939
description:
40-
'The preimage of hash with which the asset was locked with.'
40+
'The preimage of hash with which the asset was locked with. Input format supported: base64.'
4141
}
4242
],
4343
command,
@@ -77,11 +77,13 @@ const command: GluegunCommand = {
7777
return
7878
}
7979
const lockContractId = options.lock_contract_id
80-
if(!options.preimage){
80+
if(!options.preimage_base64){
8181
print.error('Preimage not provided.')
8282
return
8383
}
84-
const preimage = options.preimage
84+
const preimage_base64 = options.preimage_base64
85+
const preimage = Buffer.from(preimage_base64, 'base64')
86+
console.log('Length of preimage:', preimage.length)
8587

8688
console.log('Parameters')
8789
console.log('networkConfig', networkConfig)
@@ -95,8 +97,8 @@ const command: GluegunCommand = {
9597

9698
await interopContract.claimFungibleAsset(lockContractId, preimage, {
9799
from: recipient,
98-
}).catch(function () {
99-
console.log("claimFungibleAsset threw an error");
100+
}).catch((error) => {
101+
console.log("claimFungibleAsset threw an error:", error);
100102
})
101103

102104
// Balance of the recipient after claiming

samples/besu/besu-cli/src/commands/asset/is-locked.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,19 @@ const command: GluegunCommand = {
4545
console.log('Parameters')
4646
console.log('networkConfig', networkConfig)
4747
console.log('Lock Contract ID', options.lock_contract_id)
48-
48+
4949
const provider = new Web3.providers.HttpProvider('http://'+networkConfig.networkHost+':'+networkConfig.networkPort)
50-
const interopContract = await getContractInstance(provider, networkConfig.interopContract)
50+
const web3N = new Web3(provider)
51+
const interopContract = await getContractInstance(provider, networkConfig.interopContract)
52+
const accounts = await web3N.eth.getAccounts()
53+
var sender = accounts[networkConfig.senderAccountIndex]
5154

52-
var isLocked = interopContract.isFungibleAssetLocked(options.lock_contract_id)
53-
console.log(`Is there an asset locked in ${options.lock_conntract_id} in Network ${options.network}: ${isLocked}`)
55+
var isLocked = await interopContract.isFungibleAssetLocked(options.lock_contract_id, {
56+
from: sender
57+
}).catch(function () {
58+
console.log("isFungibleAssetLocked threw an error");
59+
})
60+
console.log(`Is there an asset locked in ${options.lock_contract_id} in Network ${options.network}: ${isLocked}`) //Todo: Debug. isLocked is not printing correctly.
5461
}
5562
}
5663

samples/besu/besu-cli/src/commands/asset/lock.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const command: GluegunCommand = {
4646
'Time in seconds for which the asset will be locked. The asset will be locked till Date.now() + the timeout provided'
4747
},
4848
{
49-
name: '--hash',
49+
name: '--hash_base64',
5050
description:
5151
'The hash value with which the asset will be locked i.e., providing its pre-image will enable unlocking the asset. This is an optional parameter. If not provided, we will generate a fresh hash pair with a randomly generated pre-image and output the corresponding pre-image.'
5252
}
@@ -101,25 +101,30 @@ const command: GluegunCommand = {
101101
return
102102
}
103103
const timeLock = Math.floor(Date.now() / 1000) + options.timeout
104+
// The hash input has to be dealt with care. The smart contracts take in bytes as input. But the cli takes in strings as input. So to handle that, we take in the hash in its base64 version as input and then obtain the byte array from this. If a hash is not provided, we generate a base 64 encoding and then generate its corresponding byte array from it. This byte array will be input to generate the hash.
104105
var hash
105106
var preimage
106-
if(options.hash){
107-
hash = options.hash
107+
var hash_base64
108+
var preimage_base64
109+
if(options.hash_base64){
110+
hash_base64 = options.hash_base64
108111
}
109112
else{
110113
// Generate a hash pair if not provided as an input parameter
111-
preimage = crypto.randomBytes(32)
112-
hash = crypto.createHash('sha256').update(preimage).digest()
114+
preimage_base64 = crypto.randomBytes(32).toString('base64')
115+
preimage = Buffer.from(preimage_base64, 'base64')
116+
hash_base64 = crypto.createHash('sha256').update(preimage).digest('base64')
113117
}
118+
hash = Buffer.from(hash_base64, 'base64')
114119

115120
console.log('Parameters:')
116121
console.log('networkConfig', networkConfig)
117122
console.log('Sender', sender)
118123
console.log('Receiver', recipient)
119124
console.log('Amount', options.amount)
120125
console.log('Timeout', timeLock)
121-
console.log('Hash: ', hash)
122-
console.log('Preimage: ', preimage)
126+
console.log('Hash (base64): ', hash_base64)
127+
console.log('Preimage (base64): ', preimage_base64)
123128

124129
// Balances of sender and receiver before locking
125130
console.log(`Account balances before locking`)

samples/besu/besu-cli/src/commands/asset/unlock.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const command: GluegunCommand = {
3636
}
3737
],
3838
command,
39-
['asset', 'claim']
39+
['asset', 'unlock']
4040
)
4141
return
4242
}
@@ -80,7 +80,7 @@ const command: GluegunCommand = {
8080

8181
// Balance of the recipient before claiming
8282
var senderBalance = await tokenContract.balanceOf(sender)
83-
console.log(`Account balance of the sender in Network ${options.network} before claiming: ${senderBalance.toString()}`)
83+
console.log(`Account balance of the sender in Network ${options.network} before unlocking: ${senderBalance.toString()}`)
8484

8585
await interopContract.unlockFungibleAsset(lockContractId, {
8686
from: sender
@@ -90,7 +90,7 @@ const command: GluegunCommand = {
9090

9191
// Balance of the recipient after claiming
9292
var senderBalance = await tokenContract.balanceOf(sender)
93-
console.log(`Account balance of the sender in Network ${options.network} after claiming: ${senderBalance.toString()}`)
93+
console.log(`Account balance of the sender in Network ${options.network} after unlocking: ${senderBalance.toString()}`)
9494
}
9595
}
9696

0 commit comments

Comments
 (0)