Skip to content

Commit

Permalink
CU-86ducwq4y - BSNeo3 - Ledger - Fix issue where sending assets from …
Browse files Browse the repository at this point in the history
…NEON3 is being incorrectly flagged as arbitrary contract invocation
  • Loading branch information
raulduartep committed Aug 14, 2024
1 parent 4b05249 commit 1c8d1cf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-neo3",
"comment": "Fix issue where sending assets from NEON3 is being incorrectly flagged as arbitrary contract invocation",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-neo3"
}
2 changes: 1 addition & 1 deletion packages/bs-neo3/src/BSNeo3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class BSNeo3<BSCustomName extends string = string>
? u.BigInteger.fromDecimal(intent.amount, intent.tokenDecimals).toString()
: intent.amount,
},
{ type: 'Any', value: '' },
{ type: 'Any', value: null },
],
}
})
Expand Down
31 changes: 13 additions & 18 deletions packages/bs-neo3/src/NeonDappKitLedgerServiceNeo3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,17 @@ export class NeonDappKitLedgerServiceNeo3 implements LedgerService {
try {
this.emitter.emit('getSignatureStart')

const bip44Buffer = this.toBip44Buffer(addressIndex)
await transport.send(0x80, 0x02, 0, 0x80, bip44Buffer, [0x9000])
await transport.send(0x80, 0x02, 1, 0x80, Buffer.from(NeonParser.numToHex(networkMagic, 4, true), 'hex'), [
0x9000,
])
const bip44 = this.toBip44(addressIndex)
await this.sendSignatureChunk(transport, bip44, 0)
await this.sendSignatureChunk(transport, NeonParser.numToHex(networkMagic, 4, true), 1)

const chunks = serializedTransaction.match(/.{1,510}/g) || []

for (let i = 0; i < chunks.length - 1; i++) {
await transport.send(0x80, 0x02, 2 + i, 0x80, Buffer.from(chunks[i], 'hex'), [0x9000])
await this.sendSignatureChunk(transport, chunks[i], 2 + i)
}

const response = await transport.send(
0x80,
0x02,
2 + chunks.length,
0x00,
Buffer.from(chunks[chunks.length - 1], 'hex'),
[0x9000]
)
const response = await this.sendSignatureChunk(transport, chunks[chunks.length - 1], 2 + chunks.length, true)

if (response.length <= 2) {
throw new Error(`No more data but Ledger did not return signature!`)
Expand All @@ -73,20 +64,24 @@ export class NeonDappKitLedgerServiceNeo3 implements LedgerService {
}

async getPublicKey(transport: Transport, addressIndex = 0): Promise<string> {
const bip44Buffer = this.toBip44Buffer(addressIndex)
const bip44 = this.toBip44(addressIndex)

const result = await transport.send(0x80, 0x04, 0x00, 0x00, bip44Buffer, [0x9000])
const result = await transport.send(0x80, 0x04, 0x00, 0x00, Buffer.from(bip44, 'hex'), [0x9000])
const publicKey = result.toString('hex').substring(0, 130)

return publicKey
}

private toBip44Buffer(addressIndex = 0, changeIndex = 0, accountIndex = 0) {
private sendSignatureChunk(transport: Transport, chunk: string, index: number, isLast = false) {
return transport.send(0x80, 0x02, index, isLast ? 0x00 : 0x80, Buffer.from(chunk, 'hex'), [0x9000])
}

private toBip44(addressIndex = 0, changeIndex = 0, accountIndex = 0) {
const accountHex = this.to8BitHex(accountIndex + 0x80000000)
const changeHex = this.to8BitHex(changeIndex)
const addressHex = this.to8BitHex(addressIndex)

return Buffer.from('8000002C' + '80000378' + accountHex + changeHex + addressHex, 'hex')
return '8000002C' + '80000378' + accountHex + changeHex + addressHex
}

private to8BitHex(num: number): string {
Expand Down

0 comments on commit 1c8d1cf

Please sign in to comment.