@alephium/web3 0.36.1 → 0.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +6 -1
- package/dist/src/api/api-alephium.js +1 -1
- package/dist/src/api/types.d.ts +1 -1
- package/dist/src/codec/array-codec.d.ts +17 -0
- package/dist/src/codec/array-codec.js +59 -0
- package/dist/src/codec/asset-output-codec.d.ts +27 -0
- package/dist/src/codec/asset-output-codec.js +135 -0
- package/dist/src/codec/bigint-codec.d.ts +5 -0
- package/dist/src/codec/bigint-codec.js +86 -0
- package/dist/src/codec/bytestring-codec.d.ts +16 -0
- package/dist/src/codec/bytestring-codec.js +50 -0
- package/dist/src/codec/codec.d.ts +8 -0
- package/dist/src/codec/codec.js +9 -0
- package/dist/src/codec/compact-int-codec.d.ts +51 -0
- package/dist/src/codec/compact-int-codec.js +300 -0
- package/dist/src/codec/contract-codec.d.ts +23 -0
- package/dist/src/codec/contract-codec.js +81 -0
- package/dist/src/codec/contract-output-codec.d.ts +21 -0
- package/dist/src/codec/contract-output-codec.js +82 -0
- package/dist/src/codec/contract-output-ref-codec.d.ts +15 -0
- package/dist/src/codec/contract-output-ref-codec.js +38 -0
- package/dist/src/codec/either-codec.d.ts +17 -0
- package/dist/src/codec/either-codec.js +67 -0
- package/dist/src/codec/hash.d.ts +4 -0
- package/dist/src/codec/hash.js +23 -0
- package/dist/src/codec/index.d.ts +23 -0
- package/dist/src/codec/index.js +69 -0
- package/dist/src/codec/input-codec.d.ts +22 -0
- package/dist/src/codec/input-codec.js +71 -0
- package/dist/src/codec/instr-codec.d.ts +230 -0
- package/dist/src/codec/instr-codec.js +471 -0
- package/dist/src/codec/lockup-script-codec.d.ts +28 -0
- package/dist/src/codec/lockup-script-codec.js +80 -0
- package/dist/src/codec/long-codec.d.ts +9 -0
- package/dist/src/codec/long-codec.js +56 -0
- package/dist/src/codec/method-codec.d.ts +31 -0
- package/dist/src/codec/method-codec.js +78 -0
- package/dist/src/codec/option-codec.d.ts +15 -0
- package/dist/src/codec/option-codec.js +55 -0
- package/dist/src/codec/output-codec.d.ts +7 -0
- package/dist/src/codec/output-codec.js +26 -0
- package/dist/src/codec/script-codec.d.ts +16 -0
- package/dist/src/codec/script-codec.js +41 -0
- package/dist/src/codec/signature-codec.d.ts +14 -0
- package/dist/src/codec/signature-codec.js +19 -0
- package/dist/src/codec/signed-int-codec.d.ts +9 -0
- package/dist/src/codec/signed-int-codec.js +39 -0
- package/dist/src/codec/token-codec.d.ts +16 -0
- package/dist/src/codec/token-codec.js +46 -0
- package/dist/src/codec/transaction-codec.d.ts +27 -0
- package/dist/src/codec/transaction-codec.js +128 -0
- package/dist/src/codec/unlock-script-codec.d.ts +40 -0
- package/dist/src/codec/unlock-script-codec.js +170 -0
- package/dist/src/codec/unsigned-tx-codec.d.ts +30 -0
- package/dist/src/codec/unsigned-tx-codec.js +103 -0
- package/dist/src/contract/contract.d.ts +14 -8
- package/dist/src/contract/contract.js +205 -22
- package/dist/src/contract/ralph.d.ts +16 -0
- package/dist/src/contract/ralph.js +127 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +2 -1
- package/package.json +5 -4
- package/src/api/api-alephium.ts +7 -1
- package/src/api/types.ts +1 -1
- package/src/codec/array-codec.ts +63 -0
- package/src/codec/asset-output-codec.ts +149 -0
- package/src/codec/bigint-codec.ts +92 -0
- package/src/codec/bytestring-codec.ts +56 -0
- package/src/codec/codec.ts +31 -0
- package/src/codec/compact-int-codec.ts +316 -0
- package/src/codec/contract-codec.ts +95 -0
- package/src/codec/contract-output-codec.ts +95 -0
- package/src/codec/contract-output-ref-codec.ts +42 -0
- package/src/codec/either-codec.ts +74 -0
- package/src/codec/hash.ts +35 -0
- package/src/codec/index.ts +41 -0
- package/src/codec/input-codec.ts +81 -0
- package/src/codec/instr-codec.ts +479 -0
- package/src/codec/lockup-script-codec.ts +99 -0
- package/src/codec/long-codec.ts +59 -0
- package/src/codec/method-codec.ts +97 -0
- package/src/codec/option-codec.ts +60 -0
- package/src/codec/output-codec.ts +26 -0
- package/src/codec/script-codec.ts +45 -0
- package/src/codec/signature-codec.ts +40 -0
- package/src/codec/signed-int-codec.ts +37 -0
- package/src/codec/token-codec.ts +51 -0
- package/src/codec/transaction-codec.ts +147 -0
- package/src/codec/unlock-script-codec.ts +194 -0
- package/src/codec/unsigned-tx-codec.ts +124 -0
- package/src/contract/contract.ts +299 -23
- package/src/contract/ralph.ts +140 -2
- package/src/index.ts +1 -1
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { Buffer } from 'buffer/'
|
|
20
|
+
import { Parser } from 'binary-parser'
|
|
21
|
+
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
22
|
+
import { Codec } from './codec'
|
|
23
|
+
import { compactSignedIntCodec, compactUnsignedIntCodec, DecodedCompactInt } from './compact-int-codec'
|
|
24
|
+
import { Method, MethodCodec, methodCodec } from './method-codec'
|
|
25
|
+
|
|
26
|
+
const compactSignedIntsCodec = new ArrayCodec(compactSignedIntCodec)
|
|
27
|
+
|
|
28
|
+
export interface HalfDecodedContract {
|
|
29
|
+
fieldLength: DecodedCompactInt
|
|
30
|
+
methodIndexes: DecodedArray<DecodedCompactInt>
|
|
31
|
+
methods: Buffer
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface Contract {
|
|
35
|
+
fieldLength: number
|
|
36
|
+
methods: Method[]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function toHalfDecoded(contract: Contract): HalfDecodedContract {
|
|
40
|
+
const fieldLength = compactSignedIntCodec.fromI32(contract.fieldLength)
|
|
41
|
+
const methods = contract.methods.map((m) => methodCodec.encode(MethodCodec.fromMethod(m)))
|
|
42
|
+
let count = 0
|
|
43
|
+
const methodIndexes = Array.from(Array(methods.length).keys()).map((index) => {
|
|
44
|
+
count += methods[`${index}`].length
|
|
45
|
+
return count
|
|
46
|
+
})
|
|
47
|
+
return {
|
|
48
|
+
fieldLength,
|
|
49
|
+
methodIndexes: {
|
|
50
|
+
length: compactSignedIntCodec.fromI32(methodIndexes.length),
|
|
51
|
+
value: methodIndexes.map((value) => compactSignedIntCodec.fromI32(value))
|
|
52
|
+
},
|
|
53
|
+
methods: methods.reduce((acc, buffer) => Buffer.concat([acc, buffer]))
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class ContractCodec implements Codec<HalfDecodedContract> {
|
|
58
|
+
parser = Parser.start()
|
|
59
|
+
.nest('fieldLength', {
|
|
60
|
+
type: compactSignedIntCodec.parser
|
|
61
|
+
})
|
|
62
|
+
.nest('methodIndexes', {
|
|
63
|
+
type: compactSignedIntsCodec.parser
|
|
64
|
+
})
|
|
65
|
+
.buffer('methods', { readUntil: 'eof' })
|
|
66
|
+
|
|
67
|
+
encode(input: HalfDecodedContract): Buffer {
|
|
68
|
+
return Buffer.from([
|
|
69
|
+
...compactSignedIntCodec.encode(input.fieldLength),
|
|
70
|
+
...compactSignedIntsCodec.encode(input.methodIndexes.value),
|
|
71
|
+
...input.methods
|
|
72
|
+
])
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
decode(input: Buffer): HalfDecodedContract {
|
|
76
|
+
return this.parser.parse(input)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
decodeContract(input: Buffer): Contract {
|
|
80
|
+
const halfDecoded = this.decode(input)
|
|
81
|
+
const fieldLength = compactUnsignedIntCodec.toU32(halfDecoded.fieldLength)
|
|
82
|
+
const methodIndexes = halfDecoded.methodIndexes.value.map((v) => compactUnsignedIntCodec.toU32(v))
|
|
83
|
+
const methods: Method[] = []
|
|
84
|
+
for (let i = 0, start = 0; i < methodIndexes.length; i++) {
|
|
85
|
+
const end = methodIndexes[i]
|
|
86
|
+
const method = MethodCodec.toMethod(methodCodec.decode(halfDecoded.methods.slice(start, end)))
|
|
87
|
+
methods.push(method)
|
|
88
|
+
start = end
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { fieldLength, methods }
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export const contractCodec = new ContractCodec()
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
import { Buffer } from 'buffer/'
|
|
19
|
+
import { Parser } from 'binary-parser'
|
|
20
|
+
import { DecodedArray } from './array-codec'
|
|
21
|
+
import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
|
|
22
|
+
import { P2C } from './lockup-script-codec'
|
|
23
|
+
import { Codec } from './codec'
|
|
24
|
+
import { Token, tokensCodec } from './token-codec'
|
|
25
|
+
import { ContractOutput as ApiContractOutput } from '../api/api-alephium'
|
|
26
|
+
import { blakeHash, createHint } from './hash'
|
|
27
|
+
import { binToHex, bs58 } from '..'
|
|
28
|
+
import { signedIntCodec } from './signed-int-codec'
|
|
29
|
+
import { lockupScriptCodec } from './lockup-script-codec'
|
|
30
|
+
|
|
31
|
+
export interface ContractOutput {
|
|
32
|
+
amount: DecodedCompactInt
|
|
33
|
+
lockupScript: P2C
|
|
34
|
+
tokens: DecodedArray<Token>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class ContractOutputCodec implements Codec<ContractOutput> {
|
|
38
|
+
parser = Parser.start()
|
|
39
|
+
.nest('amount', {
|
|
40
|
+
type: compactUnsignedIntCodec.parser
|
|
41
|
+
})
|
|
42
|
+
.nest('lockupScript', {
|
|
43
|
+
type: Parser.start().buffer('contractId', { length: 32 })
|
|
44
|
+
})
|
|
45
|
+
.nest('tokens', {
|
|
46
|
+
type: tokensCodec.parser
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
encode(input: ContractOutput): Buffer {
|
|
50
|
+
const amount = Buffer.from(compactUnsignedIntCodec.encode(input.amount))
|
|
51
|
+
const lockupScript = input.lockupScript.contractId
|
|
52
|
+
const tokens = Buffer.from(tokensCodec.encode(input.tokens.value))
|
|
53
|
+
|
|
54
|
+
return Buffer.concat([amount, lockupScript, tokens])
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
decode(input: Buffer): ContractOutput {
|
|
58
|
+
return this.parser.parse(input)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static convertToApiContractOutput(txIdBytes: Uint8Array, output: ContractOutput, index: number): ApiContractOutput {
|
|
62
|
+
const hint = createHint(output.lockupScript.contractId)
|
|
63
|
+
const key = binToHex(blakeHash(Buffer.concat([txIdBytes, signedIntCodec.encode(index)])))
|
|
64
|
+
const attoAlphAmount = compactUnsignedIntCodec.toU256(output.amount).toString()
|
|
65
|
+
const address = bs58.encode(Buffer.concat([Buffer.from([0x03]), output.lockupScript.contractId]))
|
|
66
|
+
const tokens = output.tokens.value.map((token) => {
|
|
67
|
+
return {
|
|
68
|
+
id: token.tokenId.toString('hex'),
|
|
69
|
+
amount: compactUnsignedIntCodec.toU256(token.amount).toString()
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
return { hint, key, attoAlphAmount, address, tokens, type: 'ContractOutput' }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static convertToOutput(apiContractOutput: ApiContractOutput): ContractOutput {
|
|
76
|
+
const amount: DecodedCompactInt = compactUnsignedIntCodec.fromU256(BigInt(apiContractOutput.attoAlphAmount))
|
|
77
|
+
const lockupScript: P2C = lockupScriptCodec.decode(Buffer.from(bs58.decode(apiContractOutput.address)))
|
|
78
|
+
.script as P2C
|
|
79
|
+
|
|
80
|
+
const tokensValue = apiContractOutput.tokens.map((token) => {
|
|
81
|
+
return {
|
|
82
|
+
tokenId: Buffer.from(token.id, 'hex'),
|
|
83
|
+
amount: compactUnsignedIntCodec.fromU256(BigInt(token.amount))
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
const tokens: DecodedArray<Token> = {
|
|
87
|
+
length: compactUnsignedIntCodec.fromU32(tokensValue.length),
|
|
88
|
+
value: tokensValue
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { amount, lockupScript, tokens }
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export const contractOutputCodec = new ContractOutputCodec()
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
import { Buffer } from 'buffer/'
|
|
19
|
+
import { Parser } from 'binary-parser'
|
|
20
|
+
import { ArrayCodec } from './array-codec'
|
|
21
|
+
import { Codec } from './codec'
|
|
22
|
+
import { signedIntCodec } from './signed-int-codec'
|
|
23
|
+
|
|
24
|
+
export interface ContractOutputRef {
|
|
25
|
+
hint: number
|
|
26
|
+
key: Buffer
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class ContractOutputRefCodec implements Codec<ContractOutputRef> {
|
|
30
|
+
parser = Parser.start().int32('hint').buffer('key', { length: 32 })
|
|
31
|
+
|
|
32
|
+
encode(input: ContractOutputRef): Buffer {
|
|
33
|
+
return Buffer.concat([Buffer.from([...signedIntCodec.encode(input.hint), ...input.key])])
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
decode(input: Buffer): ContractOutputRef {
|
|
37
|
+
return this.parser.parse(input)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const contractOutputRefCodec = new ContractOutputRefCodec()
|
|
42
|
+
export const contractOutputRefsCodec = new ArrayCodec(contractOutputRefCodec)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
import { Buffer } from 'buffer/'
|
|
19
|
+
import { Parser } from 'binary-parser'
|
|
20
|
+
import { Codec } from './codec'
|
|
21
|
+
|
|
22
|
+
export interface Either<L, R> {
|
|
23
|
+
either: number
|
|
24
|
+
value: L | R
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class EitherCodec<L, R> implements Codec<Either<L, R>> {
|
|
28
|
+
constructor(
|
|
29
|
+
private leftCodec: Codec<L>,
|
|
30
|
+
private rightCodec: Codec<R>,
|
|
31
|
+
public parser = Parser.start()
|
|
32
|
+
.uint8('either')
|
|
33
|
+
.choice('value', {
|
|
34
|
+
tag: 'either',
|
|
35
|
+
choices: {
|
|
36
|
+
0: leftCodec.parser,
|
|
37
|
+
1: rightCodec.parser
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
) {}
|
|
41
|
+
|
|
42
|
+
encode(input: Either<L, R>): Buffer {
|
|
43
|
+
const result = [input.either]
|
|
44
|
+
if (input.either === 0) {
|
|
45
|
+
result.push(...this.leftCodec.encode(input.value as L))
|
|
46
|
+
} else {
|
|
47
|
+
result.push(...this.rightCodec.encode(input.value as R))
|
|
48
|
+
}
|
|
49
|
+
return Buffer.from(result)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
decode(input: Buffer): Either<L, R> {
|
|
53
|
+
const result = this.parser.parse(input)
|
|
54
|
+
return {
|
|
55
|
+
...result,
|
|
56
|
+
value:
|
|
57
|
+
result.either === 0 ? this.leftCodec.decode(result.value.value) : this.rightCodec.decode(result.value.value)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
fromLeft(left: L): Either<L, R> {
|
|
62
|
+
return {
|
|
63
|
+
either: 0,
|
|
64
|
+
value: left
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fromRight(right: R): Either<L, R> {
|
|
69
|
+
return {
|
|
70
|
+
either: 1,
|
|
71
|
+
value: right
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
import { Buffer } from 'buffer/'
|
|
19
|
+
import blake from 'blakejs'
|
|
20
|
+
|
|
21
|
+
export function blakeHash(raw: Uint8Array) {
|
|
22
|
+
return blake.blake2b(raw, undefined, 32)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function djbIntHash(bytes: Buffer): number {
|
|
26
|
+
let hash = 5381
|
|
27
|
+
bytes.forEach((byte) => {
|
|
28
|
+
hash = (hash << 5) + hash + (byte & 0xff)
|
|
29
|
+
})
|
|
30
|
+
return hash
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function createHint(input: Buffer): number {
|
|
34
|
+
return djbIntHash(input) | 1
|
|
35
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export * from './array-codec'
|
|
20
|
+
export * as assetOutput from './asset-output-codec'
|
|
21
|
+
export * from './bigint-codec'
|
|
22
|
+
export * from './bytestring-codec'
|
|
23
|
+
export * from './codec'
|
|
24
|
+
export * from './compact-int-codec'
|
|
25
|
+
export * as contractOutput from './contract-output-codec'
|
|
26
|
+
export * from './contract-output-ref-codec'
|
|
27
|
+
export * from './either-codec'
|
|
28
|
+
export * from './input-codec'
|
|
29
|
+
export * from './instr-codec'
|
|
30
|
+
export * as lockupScript from './lockup-script-codec'
|
|
31
|
+
export * as unlockScript from './unlock-script-codec'
|
|
32
|
+
export * from './long-codec'
|
|
33
|
+
export * from './method-codec'
|
|
34
|
+
export * from './option-codec'
|
|
35
|
+
export * as script from './script-codec'
|
|
36
|
+
export * from './signature-codec'
|
|
37
|
+
export * from './signed-int-codec'
|
|
38
|
+
export * as token from './token-codec'
|
|
39
|
+
export * from './transaction-codec'
|
|
40
|
+
export * from './unsigned-tx-codec'
|
|
41
|
+
export * as contract from './contract-codec'
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
import { Buffer } from 'buffer/'
|
|
19
|
+
import { Parser } from 'binary-parser'
|
|
20
|
+
import { AssetInput } from '../api/api-alephium'
|
|
21
|
+
import { binToHex } from '../utils'
|
|
22
|
+
import { UnlockScript, unlockScriptCodec } from './unlock-script-codec'
|
|
23
|
+
import { Codec } from './codec'
|
|
24
|
+
import { signedIntCodec } from './signed-int-codec'
|
|
25
|
+
import { ArrayCodec } from './array-codec'
|
|
26
|
+
|
|
27
|
+
export interface Input {
|
|
28
|
+
outputRef: {
|
|
29
|
+
hint: number
|
|
30
|
+
key: Buffer
|
|
31
|
+
}
|
|
32
|
+
unlockScript: UnlockScript
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class InputCodec implements Codec<Input> {
|
|
36
|
+
parser = Parser.start()
|
|
37
|
+
.nest('outputRef', {
|
|
38
|
+
type: Parser.start().int32('hint').buffer('key', { length: 32 })
|
|
39
|
+
})
|
|
40
|
+
.nest('unlockScript', {
|
|
41
|
+
type: unlockScriptCodec.parser
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
encode(input: Input): Buffer {
|
|
45
|
+
return Buffer.concat([
|
|
46
|
+
Buffer.from([...signedIntCodec.encode(input.outputRef.hint), ...input.outputRef.key]),
|
|
47
|
+
unlockScriptCodec.encode(input.unlockScript)
|
|
48
|
+
])
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
decode(input: Buffer): Input {
|
|
52
|
+
return this.parser.parse(input)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static toAssetInputs(inputs: Input[]): AssetInput[] {
|
|
56
|
+
return inputs.map((input) => {
|
|
57
|
+
const hint = input.outputRef.hint
|
|
58
|
+
const key = binToHex(input.outputRef.key)
|
|
59
|
+
const unlockScript = unlockScriptCodec.encode(input.unlockScript)
|
|
60
|
+
return {
|
|
61
|
+
outputRef: { hint, key },
|
|
62
|
+
unlockScript: unlockScript.toString('hex')
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static fromAssetInputs(inputs: AssetInput[]): Input[] {
|
|
68
|
+
return inputs.map((input) => {
|
|
69
|
+
const hint = input.outputRef.hint
|
|
70
|
+
const key = Buffer.from(input.outputRef.key, 'hex')
|
|
71
|
+
const unlockScript = unlockScriptCodec.decode(Buffer.from(input.unlockScript, 'hex'))
|
|
72
|
+
return {
|
|
73
|
+
outputRef: { hint, key },
|
|
74
|
+
unlockScript
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const inputCodec = new InputCodec()
|
|
81
|
+
export const inputsCodec = new ArrayCodec(inputCodec)
|