Skip to content

Commit

Permalink
Add support for contract chain object (v3)
Browse files Browse the repository at this point in the history
  • Loading branch information
dincho committed Sep 28, 2023
1 parent 8204ae3 commit ea3e985
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/ChainObjects/ChainObjectTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,19 @@ export default Object.freeze({
info: 'uint_32'
}
},
COMPILER_SOPHIA: {
3: {
sourceHash: 'hex',
typeInfo: [{
hash: 'binary',
name: 'binary',
payable: 'bool',
argType: 'binary',
returnType: 'binary'
}],
byteCode: 'binary',
compilerVersion: 'string',
payable: 'bool'
}
}
})
18 changes: 18 additions & 0 deletions src/PrimitivesEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ const ByteArray2Int = (data) => {
return BigInt('0x' + hex.join(''))
}

const ByteArray2Hex = (data) => {
return ByteArrayToHexArray(data).join('')
}

const HexStringToByteArray = (str) => {
const match = str.match(/^(0x)?([a-f0-9]*)$/i)
if (!match) {
throw new Error(`Invalid hex string: ${str}`)
}

return new Uint8Array(match[2]
.split(/(.{1,2})/)
.filter(el => el)
.map(el => parseInt(el, 16)))
}

// this is overlaping with general calldata Serializer with some extras: uint_* and id
export default class PrimitivesEncoder {
constructor() {
Expand All @@ -35,6 +51,7 @@ export default class PrimitivesEncoder {
bool: this.decodeBool,
string: this.decodeString.bind(this),
binary: this.decodeBinary,
hex: (value) => ByteArray2Hex(value),
}

this.encoders = {
Expand All @@ -45,6 +62,7 @@ export default class PrimitivesEncoder {
bool: this.encodeBool,
string: this.encodeString.bind(this),
binary: this.encodeBinary,
hex: (value) => HexStringToByteArray(value),
}
}

Expand Down
37 changes: 37 additions & 0 deletions tests/ChainObjectSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,40 @@ test('Deserialize Light MicroBlock', t => {
lightMicroBlock
)
})

test('Deserialize contract', t => {
t.plan(1)

const contractData = new Uint8Array([
248, 114, 70, 3, 160, 144, 2, 203, 30, 239, 26, 171,
141, 199, 191, 152, 59, 237, 121, 38, 75, 206, 160, 74,
96, 79, 9, 108, 232, 235, 229, 249, 64, 98, 98, 163,
200, 192, 184, 69, 169, 254, 68, 214, 68, 31, 0, 55,
0, 55, 0, 26, 14, 130, 17, 116, 101, 115, 116, 26,
14, 132, 17, 101, 99, 104, 111, 1, 3, 63, 254, 98,
168, 180, 140, 4, 55, 0, 7, 1, 3, 4, 151, 47,
2, 17, 68, 214, 68, 31, 17, 105, 110, 105, 116, 17,
98, 168, 180, 140, 21, 116, 101, 115, 116, 50, 130, 47,
0, 133, 54, 46, 49, 46, 48, 1
])
const contract = new ChainObject('compiler_sophia', {
vsn: 3n,
sourceHash: '9002cb1eef1aab8dc7bf983bed79264bcea04a604f096ce8ebe5f9406262a3c8',
compilerVersion: '6.1.0',
payable: true,
typeInfo: [],
byteCode: new Uint8Array([
169, 254, 68, 214, 68, 31, 0, 55, 0, 55, 0, 26,
14, 130, 17, 116, 101, 115, 116, 26, 14, 132, 17, 101,
99, 104, 111, 1, 3, 63, 254, 98, 168, 180, 140, 4,
55, 0, 7, 1, 3, 4, 151, 47, 2, 17, 68, 214,
68, 31, 17, 105, 110, 105, 116, 17, 98, 168, 180, 140,
21, 116, 101, 115, 116, 50, 130, 47, 0
])
})

t.deepEqual(
serializer.deserialize(contractData),
contract
)
})

0 comments on commit ea3e985

Please sign in to comment.