@alephium/web3 0.42.0 → 0.43.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/{utils → address}/address.js +15 -19
- package/dist/src/address/index.d.ts +1 -0
- package/dist/src/address/index.js +34 -0
- package/dist/src/api/node-provider.js +10 -9
- package/dist/src/api/types.js +1 -1
- package/dist/src/codec/array-codec.d.ts +2 -3
- package/dist/src/codec/array-codec.js +1 -2
- package/dist/src/codec/asset-output-codec.d.ts +3 -4
- package/dist/src/codec/asset-output-codec.js +10 -12
- package/dist/src/codec/bigint-codec.d.ts +2 -3
- package/dist/src/codec/bigint-codec.js +2 -3
- package/dist/src/codec/bytestring-codec.d.ts +5 -6
- package/dist/src/codec/bytestring-codec.js +5 -5
- package/dist/src/codec/codec.d.ts +2 -3
- package/dist/src/codec/compact-int-codec.d.ts +12 -13
- package/dist/src/codec/compact-int-codec.js +22 -22
- package/dist/src/codec/contract-codec.d.ts +5 -6
- package/dist/src/codec/contract-codec.js +24 -24
- package/dist/src/codec/contract-output-codec.d.ts +2 -3
- package/dist/src/codec/contract-output-codec.js +8 -10
- package/dist/src/codec/contract-output-ref-codec.d.ts +3 -4
- package/dist/src/codec/contract-output-ref-codec.js +2 -2
- package/dist/src/codec/either-codec.d.ts +2 -3
- package/dist/src/codec/either-codec.js +1 -2
- package/dist/src/codec/hash.d.ts +2 -3
- package/dist/src/codec/hash.js +17 -0
- package/dist/src/codec/input-codec.d.ts +3 -4
- package/dist/src/codec/input-codec.js +6 -6
- package/dist/src/codec/instr-codec.d.ts +2 -3
- package/dist/src/codec/instr-codec.js +1 -2
- package/dist/src/codec/lockup-script-codec.d.ts +5 -6
- package/dist/src/codec/lockup-script-codec.js +1 -2
- package/dist/src/codec/long-codec.d.ts +2 -3
- package/dist/src/codec/long-codec.js +1 -2
- package/dist/src/codec/method-codec.d.ts +2 -3
- package/dist/src/codec/method-codec.js +1 -2
- package/dist/src/codec/option-codec.d.ts +3 -4
- package/dist/src/codec/option-codec.js +2 -3
- package/dist/src/codec/script-codec.d.ts +4 -5
- package/dist/src/codec/script-codec.js +1 -3
- package/dist/src/codec/signature-codec.d.ts +3 -4
- package/dist/src/codec/signature-codec.js +17 -0
- package/dist/src/codec/signed-int-codec.d.ts +2 -3
- package/dist/src/codec/signed-int-codec.js +1 -2
- package/dist/src/codec/token-codec.d.ts +3 -4
- package/dist/src/codec/token-codec.js +3 -3
- package/dist/src/codec/transaction-codec.d.ts +4 -5
- package/dist/src/codec/transaction-codec.js +12 -13
- package/dist/src/codec/unlock-script-codec.d.ts +5 -6
- package/dist/src/codec/unlock-script-codec.js +14 -15
- package/dist/src/codec/unsigned-tx-codec.d.ts +4 -5
- package/dist/src/codec/unsigned-tx-codec.js +4 -5
- package/dist/src/contract/contract.js +20 -20
- package/dist/src/contract/ralph.d.ts +1 -1
- package/dist/src/contract/ralph.js +9 -10
- package/dist/src/{utils → exchange}/exchange.js +5 -5
- package/dist/src/exchange/index.d.ts +1 -0
- package/dist/src/exchange/index.js +25 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/signer/signer.js +4 -5
- package/dist/src/signer/tx-builder.js +3 -3
- package/dist/src/utils/index.d.ts +0 -2
- package/dist/src/utils/index.js +0 -7
- package/dist/src/utils/sign.js +1 -1
- package/dist/src/utils/utils.d.ts +1 -0
- package/dist/src/utils/utils.js +30 -11
- package/dist/src/utils/webcrypto.js +3 -4
- package/package.json +1 -2
- package/src/{utils → address}/address.ts +15 -19
- package/src/address/index.ts +19 -0
- package/src/api/node-provider.ts +2 -9
- package/src/api/types.ts +2 -2
- package/src/codec/array-codec.ts +3 -4
- package/src/codec/asset-output-codec.ts +16 -18
- package/src/codec/bigint-codec.ts +4 -5
- package/src/codec/bytestring-codec.ts +8 -8
- package/src/codec/codec.ts +2 -3
- package/src/codec/compact-int-codec.ts +36 -37
- package/src/codec/contract-codec.ts +28 -27
- package/src/codec/contract-output-codec.ts +11 -13
- package/src/codec/contract-output-ref-codec.ts +5 -5
- package/src/codec/either-codec.ts +3 -4
- package/src/codec/hash.ts +2 -3
- package/src/codec/input-codec.ts +10 -10
- package/src/codec/instr-codec.ts +3 -4
- package/src/codec/lockup-script-codec.ts +8 -9
- package/src/codec/long-codec.ts +3 -4
- package/src/codec/method-codec.ts +3 -4
- package/src/codec/option-codec.ts +4 -5
- package/src/codec/script-codec.ts +5 -7
- package/src/codec/signature-codec.ts +3 -4
- package/src/codec/signed-int-codec.ts +3 -4
- package/src/codec/token-codec.ts +6 -6
- package/src/codec/transaction-codec.ts +17 -18
- package/src/codec/unlock-script-codec.ts +26 -27
- package/src/codec/unsigned-tx-codec.ts +10 -11
- package/src/contract/contract.ts +3 -7
- package/src/contract/ralph.ts +10 -11
- package/src/{utils → exchange}/exchange.ts +3 -11
- package/src/exchange/index.ts +19 -0
- package/src/index.ts +2 -0
- package/src/signer/signer.ts +2 -3
- package/src/signer/tx-builder.ts +2 -2
- package/src/utils/index.ts +0 -2
- package/src/utils/sign.ts +1 -1
- package/src/utils/utils.ts +29 -10
- package/src/utils/webcrypto.ts +3 -4
- package/webpack.config.js +1 -5
- /package/dist/src/{utils → address}/address.d.ts +0 -0
- /package/dist/src/{utils → exchange}/exchange.d.ts +0 -0
|
@@ -15,7 +15,6 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
21
20
|
import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
|
|
@@ -95,16 +94,16 @@ export class MethodCodec implements Codec<DecodedMethod> {
|
|
|
95
94
|
type: instrsCodec.parser
|
|
96
95
|
})
|
|
97
96
|
|
|
98
|
-
encode(input: DecodedMethod):
|
|
97
|
+
encode(input: DecodedMethod): Uint8Array {
|
|
99
98
|
const result = [input.isPublic, input.assetModifier]
|
|
100
99
|
result.push(...compactUnsignedIntCodec.encode(input.argsLength))
|
|
101
100
|
result.push(...compactUnsignedIntCodec.encode(input.localsLength))
|
|
102
101
|
result.push(...compactUnsignedIntCodec.encode(input.returnLength))
|
|
103
102
|
result.push(...instrsCodec.encode(input.instrs.value))
|
|
104
|
-
return
|
|
103
|
+
return new Uint8Array(result)
|
|
105
104
|
}
|
|
106
105
|
|
|
107
|
-
decode(input:
|
|
106
|
+
decode(input: Uint8Array): DecodedMethod {
|
|
108
107
|
return this.parser.parse(input)
|
|
109
108
|
}
|
|
110
109
|
|
|
@@ -15,7 +15,6 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { Codec } from './codec'
|
|
21
20
|
|
|
@@ -35,15 +34,15 @@ export class OptionCodec<T> implements Codec<Option<T>> {
|
|
|
35
34
|
})
|
|
36
35
|
) {}
|
|
37
36
|
|
|
38
|
-
encode(input: Option<T>):
|
|
37
|
+
encode(input: Option<T>): Uint8Array {
|
|
39
38
|
const result = [input.option]
|
|
40
39
|
if (input.option === 1) {
|
|
41
40
|
result.push(...this.childCodec.encode(input.value!))
|
|
42
41
|
}
|
|
43
|
-
return
|
|
42
|
+
return new Uint8Array(result)
|
|
44
43
|
}
|
|
45
44
|
|
|
46
|
-
decode(input:
|
|
45
|
+
decode(input: Uint8Array): Option<T> {
|
|
47
46
|
const result = this.parser.parse(input)
|
|
48
47
|
return {
|
|
49
48
|
...result,
|
|
@@ -51,7 +50,7 @@ export class OptionCodec<T> implements Codec<Option<T>> {
|
|
|
51
50
|
}
|
|
52
51
|
}
|
|
53
52
|
|
|
54
|
-
|
|
53
|
+
fromBytes(input?: Uint8Array): Option<T> {
|
|
55
54
|
return {
|
|
56
55
|
option: input ? 1 : 0,
|
|
57
56
|
value: input ? this.childCodec.decode(input) : undefined
|
|
@@ -16,7 +16,6 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { Buffer } from 'buffer/'
|
|
20
19
|
import { Parser } from 'binary-parser'
|
|
21
20
|
import { DecodedArray } from './array-codec'
|
|
22
21
|
import { Codec } from './codec'
|
|
@@ -37,22 +36,21 @@ export class ScriptCodec implements Codec<DecodedScript> {
|
|
|
37
36
|
type: methodsCodec.parser
|
|
38
37
|
})
|
|
39
38
|
|
|
40
|
-
encode(input: DecodedScript):
|
|
41
|
-
|
|
42
|
-
return Buffer.from(script)
|
|
39
|
+
encode(input: DecodedScript): Uint8Array {
|
|
40
|
+
return methodsCodec.encode(input.methods.value)
|
|
43
41
|
}
|
|
44
42
|
|
|
45
|
-
decode(input:
|
|
43
|
+
decode(input: Uint8Array): DecodedScript {
|
|
46
44
|
return this.parser.parse(input)
|
|
47
45
|
}
|
|
48
46
|
|
|
49
|
-
decodeScript(input:
|
|
47
|
+
decodeScript(input: Uint8Array): Script {
|
|
50
48
|
const decodedTxScript = this.decode(input)
|
|
51
49
|
const methods = decodedTxScript.methods.value.map((decodedMethod) => MethodCodec.toMethod(decodedMethod))
|
|
52
50
|
return { methods }
|
|
53
51
|
}
|
|
54
52
|
|
|
55
|
-
encodeScript(inputTxScript: Script):
|
|
53
|
+
encodeScript(inputTxScript: Script): Uint8Array {
|
|
56
54
|
const methodLength = compactUnsignedIntCodec.fromU32(inputTxScript.methods.length)
|
|
57
55
|
const decodedMethods = inputTxScript.methods.map((method) => MethodCodec.fromMethod(method))
|
|
58
56
|
return this.encode({ methods: { value: decodedMethods, length: methodLength } })
|
|
@@ -15,23 +15,22 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { ArrayCodec } from './array-codec'
|
|
21
20
|
import { Codec } from './codec'
|
|
22
21
|
|
|
23
22
|
export interface Signature {
|
|
24
|
-
value:
|
|
23
|
+
value: Uint8Array
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
export class SignatureCodec implements Codec<Signature> {
|
|
28
27
|
parser = Parser.start().buffer('value', { length: 64 })
|
|
29
28
|
|
|
30
|
-
encode(input: Signature):
|
|
29
|
+
encode(input: Signature): Uint8Array {
|
|
31
30
|
return input.value
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
decode(input:
|
|
33
|
+
decode(input: Uint8Array): Signature {
|
|
35
34
|
return this.parser.parse(input)
|
|
36
35
|
}
|
|
37
36
|
}
|
|
@@ -18,17 +18,16 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18
18
|
import { Parser } from 'binary-parser'
|
|
19
19
|
import { Codec, assert } from './codec'
|
|
20
20
|
|
|
21
|
-
import { Buffer } from 'buffer/'
|
|
22
21
|
export class SignedIntCodec implements Codec<number> {
|
|
23
22
|
parser = Parser.start().buffer('value', {
|
|
24
23
|
length: 4
|
|
25
24
|
})
|
|
26
25
|
|
|
27
|
-
encode(value: number):
|
|
28
|
-
return
|
|
26
|
+
encode(value: number): Uint8Array {
|
|
27
|
+
return new Uint8Array([(value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff])
|
|
29
28
|
}
|
|
30
29
|
|
|
31
|
-
decode(bytes:
|
|
30
|
+
decode(bytes: Uint8Array): number {
|
|
32
31
|
assert(bytes.length === 4, 'Length should be 4')
|
|
33
32
|
return ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff)
|
|
34
33
|
}
|
package/src/codec/token-codec.ts
CHANGED
|
@@ -16,14 +16,14 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { Buffer } from 'buffer/'
|
|
20
19
|
import { Parser } from 'binary-parser'
|
|
21
20
|
import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
|
|
22
21
|
import { Codec } from './codec'
|
|
23
22
|
import { ArrayCodec } from './array-codec'
|
|
23
|
+
import { concatBytes } from '../utils'
|
|
24
24
|
|
|
25
25
|
export interface Token {
|
|
26
|
-
tokenId:
|
|
26
|
+
tokenId: Uint8Array
|
|
27
27
|
amount: DecodedCompactInt
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -36,13 +36,13 @@ export class TokenCodec implements Codec<Token> {
|
|
|
36
36
|
type: compactUnsignedIntCodec.parser
|
|
37
37
|
})
|
|
38
38
|
|
|
39
|
-
encode(input: Token):
|
|
39
|
+
encode(input: Token): Uint8Array {
|
|
40
40
|
const tokenId = input.tokenId
|
|
41
|
-
const amount =
|
|
42
|
-
return
|
|
41
|
+
const amount = compactUnsignedIntCodec.encode(input.amount)
|
|
42
|
+
return concatBytes([tokenId, amount])
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
decode(input:
|
|
45
|
+
decode(input: Uint8Array): Token {
|
|
46
46
|
return this.parser.parse(input)
|
|
47
47
|
}
|
|
48
48
|
}
|
|
@@ -16,7 +16,6 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { Buffer } from 'buffer/'
|
|
20
19
|
import { Parser } from 'binary-parser'
|
|
21
20
|
import { DecodedArray } from './array-codec'
|
|
22
21
|
|
|
@@ -27,7 +26,7 @@ import { Either } from './either-codec'
|
|
|
27
26
|
import { AssetOutput, AssetOutputCodec } from './asset-output-codec'
|
|
28
27
|
import { ContractOutput, ContractOutputCodec } from './contract-output-codec'
|
|
29
28
|
import { FixedAssetOutput, Transaction as ApiTransaction } from '../api/api-alephium'
|
|
30
|
-
import { hexToBinUnsafe } from '../utils'
|
|
29
|
+
import { binToHex, hexToBinUnsafe, concatBytes } from '../utils'
|
|
31
30
|
import { ContractOutput as ApiContractOutput } from '../api/api-alephium'
|
|
32
31
|
import { Codec } from './codec'
|
|
33
32
|
import { Output, outputCodec, outputsCodec } from './output-codec'
|
|
@@ -60,27 +59,27 @@ export class TransactionCodec implements Codec<Transaction> {
|
|
|
60
59
|
type: signaturesCodec.parser
|
|
61
60
|
})
|
|
62
61
|
|
|
63
|
-
encode(decodedTx: Transaction):
|
|
64
|
-
return
|
|
62
|
+
encode(decodedTx: Transaction): Uint8Array {
|
|
63
|
+
return concatBytes([
|
|
65
64
|
unsignedTxCodec.encode(decodedTx.unsigned),
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
65
|
+
new Uint8Array([decodedTx.scriptExecutionOk]),
|
|
66
|
+
contractOutputRefsCodec.encode(decodedTx.contractInputs.value),
|
|
67
|
+
outputsCodec.encode(decodedTx.generatedOutputs.value),
|
|
68
|
+
signaturesCodec.encode(decodedTx.inputSignatures.value),
|
|
69
|
+
signaturesCodec.encode(decodedTx.scriptSignatures.value)
|
|
71
70
|
])
|
|
72
71
|
}
|
|
73
72
|
|
|
74
|
-
decode(input:
|
|
73
|
+
decode(input: Uint8Array): Transaction {
|
|
75
74
|
return this.parser.parse(input)
|
|
76
75
|
}
|
|
77
76
|
|
|
78
|
-
encodeApiTransaction(input: ApiTransaction):
|
|
77
|
+
encodeApiTransaction(input: ApiTransaction): Uint8Array {
|
|
79
78
|
const decodedTx = TransactionCodec.fromApiTransaction(input)
|
|
80
79
|
return this.encode(decodedTx)
|
|
81
80
|
}
|
|
82
81
|
|
|
83
|
-
decodeApiTransaction(input:
|
|
82
|
+
decodeApiTransaction(input: Uint8Array): ApiTransaction {
|
|
84
83
|
const decodedTx = this.parser.parse(input)
|
|
85
84
|
return TransactionCodec.toApiTransaction(decodedTx)
|
|
86
85
|
}
|
|
@@ -91,7 +90,7 @@ export class TransactionCodec implements Codec<Transaction> {
|
|
|
91
90
|
const scriptExecutionOk = !!transaction.scriptExecutionOk
|
|
92
91
|
const contractInputs = transaction.contractInputs.value.map((contractInput) => {
|
|
93
92
|
const hint = contractInput.hint
|
|
94
|
-
const key = contractInput.key
|
|
93
|
+
const key = binToHex(contractInput.key)
|
|
95
94
|
return { hint, key }
|
|
96
95
|
})
|
|
97
96
|
const txIdBytes = hexToBinUnsafe(txId)
|
|
@@ -104,8 +103,8 @@ export class TransactionCodec implements Codec<Transaction> {
|
|
|
104
103
|
}
|
|
105
104
|
})
|
|
106
105
|
|
|
107
|
-
const inputSignatures = transaction.inputSignatures.value.map((signature) => signature.value
|
|
108
|
-
const scriptSignatures = transaction.scriptSignatures.value.map((signature) => signature.value
|
|
106
|
+
const inputSignatures = transaction.inputSignatures.value.map((signature) => binToHex(signature.value))
|
|
107
|
+
const scriptSignatures = transaction.scriptSignatures.value.map((signature) => binToHex(signature.value))
|
|
109
108
|
|
|
110
109
|
return { unsigned, scriptExecutionOk, contractInputs, generatedOutputs, inputSignatures, scriptSignatures }
|
|
111
110
|
}
|
|
@@ -114,7 +113,7 @@ export class TransactionCodec implements Codec<Transaction> {
|
|
|
114
113
|
const unsigned = UnsignedTxCodec.fromApiUnsignedTx(tx.unsigned)
|
|
115
114
|
const scriptExecutionOk = tx.scriptExecutionOk ? 1 : 0
|
|
116
115
|
const contractInputs: ContractOutputRef[] = tx.contractInputs.map((contractInput) => {
|
|
117
|
-
return { hint: contractInput.hint, key:
|
|
116
|
+
return { hint: contractInput.hint, key: hexToBinUnsafe(contractInput.key) }
|
|
118
117
|
})
|
|
119
118
|
const generatedOutputs: Either<AssetOutput, ContractOutput>[] = tx.generatedOutputs.map((output) => {
|
|
120
119
|
if (output.type === 'AssetOutput') {
|
|
@@ -127,10 +126,10 @@ export class TransactionCodec implements Codec<Transaction> {
|
|
|
127
126
|
})
|
|
128
127
|
|
|
129
128
|
const inputSignatures: Signature[] = tx.inputSignatures.map((signature) => {
|
|
130
|
-
return { value:
|
|
129
|
+
return { value: hexToBinUnsafe(signature) }
|
|
131
130
|
})
|
|
132
131
|
const scriptSignatures: Signature[] = tx.scriptSignatures.map((signature) => {
|
|
133
|
-
return { value:
|
|
132
|
+
return { value: hexToBinUnsafe(signature) }
|
|
134
133
|
})
|
|
135
134
|
|
|
136
135
|
return {
|
|
@@ -15,27 +15,27 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
21
20
|
import { compactUnsignedIntCodec, compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
|
|
22
21
|
import { Codec } from './codec'
|
|
23
|
-
import { DecodedScript,
|
|
22
|
+
import { DecodedScript, scriptCodec } from './script-codec'
|
|
24
23
|
import { ByteString, byteStringCodec } from './bytestring-codec'
|
|
25
24
|
import { LockupScript, lockupScriptCodec } from './lockup-script-codec'
|
|
25
|
+
import { concatBytes } from '../utils'
|
|
26
26
|
|
|
27
27
|
export interface P2PKH {
|
|
28
|
-
publicKey:
|
|
28
|
+
publicKey: Uint8Array
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
class P2PKHCodec implements Codec<P2PKH> {
|
|
32
32
|
parser = Parser.start().buffer('publicKey', { length: 33 })
|
|
33
33
|
|
|
34
|
-
encode(input: P2PKH):
|
|
34
|
+
encode(input: P2PKH): Uint8Array {
|
|
35
35
|
return input.publicKey
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
decode(input:
|
|
38
|
+
decode(input: Uint8Array): P2PKH {
|
|
39
39
|
return this.parser.parse(input)
|
|
40
40
|
}
|
|
41
41
|
}
|
|
@@ -57,16 +57,16 @@ class P2MPKHCodec implements Codec<P2MPKH> {
|
|
|
57
57
|
)
|
|
58
58
|
})
|
|
59
59
|
|
|
60
|
-
encode(input: P2MPKH):
|
|
61
|
-
return
|
|
62
|
-
|
|
60
|
+
encode(input: P2MPKH): Uint8Array {
|
|
61
|
+
return concatBytes([
|
|
62
|
+
compactUnsignedIntCodec.encode(input.publicKeys.length),
|
|
63
63
|
...input.publicKeys.value.map((v) => {
|
|
64
|
-
return
|
|
64
|
+
return concatBytes([v.publicKey.publicKey, compactUnsignedIntCodec.encode(v.index)])
|
|
65
65
|
})
|
|
66
66
|
])
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
decode(input:
|
|
69
|
+
decode(input: Uint8Array): any {
|
|
70
70
|
return this.parser.parse(input)
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -90,30 +90,30 @@ class ValCodec implements Codec<Val> {
|
|
|
90
90
|
}
|
|
91
91
|
})
|
|
92
92
|
|
|
93
|
-
encode(input: Val):
|
|
93
|
+
encode(input: Val): Uint8Array {
|
|
94
94
|
const valType = input.type
|
|
95
95
|
|
|
96
96
|
if (valType === 0x00) {
|
|
97
97
|
// Boolean
|
|
98
|
-
return
|
|
98
|
+
return new Uint8Array([valType, input.val as number])
|
|
99
99
|
} else if (valType === 0x01) {
|
|
100
100
|
// I256
|
|
101
|
-
return
|
|
101
|
+
return new Uint8Array([valType, ...compactUnsignedIntCodec.encode(input.val as DecodedCompactInt)])
|
|
102
102
|
} else if (valType === 0x02) {
|
|
103
103
|
// U256
|
|
104
|
-
return
|
|
104
|
+
return new Uint8Array([valType, ...compactUnsignedIntCodec.encode(input.val as DecodedCompactInt)])
|
|
105
105
|
} else if (valType === 0x03) {
|
|
106
106
|
// ByteVec
|
|
107
|
-
return
|
|
107
|
+
return new Uint8Array([valType, ...byteStringCodec.encode(input.val as ByteString)])
|
|
108
108
|
} else if (valType === 0x04) {
|
|
109
109
|
// Address
|
|
110
|
-
return
|
|
110
|
+
return new Uint8Array([valType, ...lockupScriptCodec.encode(input.val as LockupScript)])
|
|
111
111
|
} else {
|
|
112
112
|
throw new Error(`ValCodec: unsupported val type: ${valType}`)
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
decode(input:
|
|
116
|
+
decode(input: Uint8Array): Val {
|
|
117
117
|
return this.parser.parse(input)
|
|
118
118
|
}
|
|
119
119
|
}
|
|
@@ -135,11 +135,11 @@ export class P2SHCodec implements Codec<P2SH> {
|
|
|
135
135
|
type: valsCodec.parser
|
|
136
136
|
})
|
|
137
137
|
|
|
138
|
-
encode(input: P2SH):
|
|
139
|
-
return
|
|
138
|
+
encode(input: P2SH): Uint8Array {
|
|
139
|
+
return concatBytes([scriptCodec.encode(input.script), valsCodec.encode(input.params.value)])
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
decode(input:
|
|
142
|
+
decode(input: Uint8Array): P2SH {
|
|
143
143
|
return this.parser.parse(input)
|
|
144
144
|
}
|
|
145
145
|
}
|
|
@@ -164,29 +164,28 @@ export class UnlockScriptCodec implements Codec<UnlockScript> {
|
|
|
164
164
|
}
|
|
165
165
|
})
|
|
166
166
|
|
|
167
|
-
encode(input: UnlockScript):
|
|
167
|
+
encode(input: UnlockScript): Uint8Array {
|
|
168
168
|
const scriptType = input.scriptType
|
|
169
169
|
const inputUnLockScript = input.script
|
|
170
|
-
const inputUnLockScriptType = Buffer.from([scriptType])
|
|
171
170
|
|
|
172
171
|
if (scriptType === 0) {
|
|
173
172
|
// P2PKH
|
|
174
|
-
return
|
|
173
|
+
return new Uint8Array([scriptType, ...p2pkhCodec.encode(inputUnLockScript as P2PKH)])
|
|
175
174
|
} else if (scriptType === 1) {
|
|
176
175
|
// P2MPKH
|
|
177
|
-
return
|
|
176
|
+
return new Uint8Array([scriptType, ...p2mpkhCodec.encode(inputUnLockScript as P2MPKH)])
|
|
178
177
|
} else if (scriptType === 2) {
|
|
179
178
|
// P2SH
|
|
180
|
-
return
|
|
179
|
+
return new Uint8Array([scriptType, ...p2shCodec.encode(inputUnLockScript as P2SH)])
|
|
181
180
|
} else if (scriptType === 3) {
|
|
182
181
|
// SameAsPrevious
|
|
183
|
-
return
|
|
182
|
+
return new Uint8Array([scriptType])
|
|
184
183
|
} else {
|
|
185
184
|
throw new Error(`TODO: encode unlock script: ${scriptType}`)
|
|
186
185
|
}
|
|
187
186
|
}
|
|
188
187
|
|
|
189
|
-
decode(input:
|
|
188
|
+
decode(input: Uint8Array): UnlockScript {
|
|
190
189
|
return this.parser.parse(input)
|
|
191
190
|
}
|
|
192
191
|
}
|
|
@@ -15,10 +15,9 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { UnsignedTx as ApiUnsignedTx } from '../api/api-alephium'
|
|
21
|
-
import { binToHex, hexToBinUnsafe } from '../utils'
|
|
20
|
+
import { binToHex, concatBytes, hexToBinUnsafe } from '../utils'
|
|
22
21
|
import { DecodedScript, scriptCodec, statefulScriptCodecOpt } from './script-codec'
|
|
23
22
|
import { Option } from './option-codec'
|
|
24
23
|
import { DecodedCompactInt, compactSignedIntCodec, compactUnsignedIntCodec } from './compact-int-codec'
|
|
@@ -58,9 +57,9 @@ export class UnsignedTxCodec implements Codec<UnsignedTx> {
|
|
|
58
57
|
type: assetOutputsCodec.parser
|
|
59
58
|
})
|
|
60
59
|
|
|
61
|
-
encode(decodedUnsignedTx: UnsignedTx):
|
|
62
|
-
return
|
|
63
|
-
|
|
60
|
+
encode(decodedUnsignedTx: UnsignedTx): Uint8Array {
|
|
61
|
+
return concatBytes([
|
|
62
|
+
new Uint8Array([decodedUnsignedTx.version, decodedUnsignedTx.networkId]),
|
|
64
63
|
statefulScriptCodecOpt.encode(decodedUnsignedTx.statefulScript),
|
|
65
64
|
compactSignedIntCodec.encode(decodedUnsignedTx.gasAmount),
|
|
66
65
|
compactUnsignedIntCodec.encode(decodedUnsignedTx.gasPrice),
|
|
@@ -69,16 +68,16 @@ export class UnsignedTxCodec implements Codec<UnsignedTx> {
|
|
|
69
68
|
])
|
|
70
69
|
}
|
|
71
70
|
|
|
72
|
-
decode(input:
|
|
71
|
+
decode(input: Uint8Array): UnsignedTx {
|
|
73
72
|
return this.parser.parse(input)
|
|
74
73
|
}
|
|
75
74
|
|
|
76
|
-
encodeApiUnsignedTx(input: ApiUnsignedTx):
|
|
75
|
+
encodeApiUnsignedTx(input: ApiUnsignedTx): Uint8Array {
|
|
77
76
|
const decoded = UnsignedTxCodec.fromApiUnsignedTx(input)
|
|
78
77
|
return this.encode(decoded)
|
|
79
78
|
}
|
|
80
79
|
|
|
81
|
-
decodeApiUnsignedTx(input:
|
|
80
|
+
decodeApiUnsignedTx(input: Uint8Array): ApiUnsignedTx {
|
|
82
81
|
const decoded = this.parser.parse(input)
|
|
83
82
|
return UnsignedTxCodec.toApiUnsignedTx(decoded)
|
|
84
83
|
}
|
|
@@ -98,7 +97,7 @@ export class UnsignedTxCodec implements Codec<UnsignedTx> {
|
|
|
98
97
|
const fixedOutputs = AssetOutputCodec.toFixedAssetOutputs(txIdBytes, unsigned.fixedOutputs.value)
|
|
99
98
|
let scriptOpt: string | undefined = undefined
|
|
100
99
|
if (unsigned.statefulScript.option === 1) {
|
|
101
|
-
scriptOpt = scriptCodec.encode(unsigned.statefulScript.value!)
|
|
100
|
+
scriptOpt = binToHex(scriptCodec.encode(unsigned.statefulScript.value!))
|
|
102
101
|
}
|
|
103
102
|
|
|
104
103
|
return { txId, version, networkId, gasAmount, scriptOpt, gasPrice, inputs, fixedOutputs }
|
|
@@ -113,8 +112,8 @@ export class UnsignedTxCodec implements Codec<UnsignedTx> {
|
|
|
113
112
|
const inputs = inputsCodec.fromArray(inputsValue)
|
|
114
113
|
const fixedOutputsValue = AssetOutputCodec.fromFixedAssetOutputs(unsignedTx.fixedOutputs)
|
|
115
114
|
const fixedOutputs = assetOutputsCodec.fromArray(fixedOutputsValue)
|
|
116
|
-
const statefulScript = statefulScriptCodecOpt.
|
|
117
|
-
unsignedTx.scriptOpt ?
|
|
115
|
+
const statefulScript = statefulScriptCodecOpt.fromBytes(
|
|
116
|
+
unsignedTx.scriptOpt ? hexToBinUnsafe(unsignedTx.scriptOpt) : undefined
|
|
118
117
|
)
|
|
119
118
|
|
|
120
119
|
return { version, networkId, gasAmount, gasPrice, inputs, fixedOutputs, statefulScript }
|
package/src/contract/contract.ts
CHANGED
|
@@ -16,7 +16,6 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { Buffer } from 'buffer/'
|
|
20
19
|
import { promises as fsPromises } from 'fs'
|
|
21
20
|
import {
|
|
22
21
|
fromApiNumber256,
|
|
@@ -47,19 +46,16 @@ import * as ralph from './ralph'
|
|
|
47
46
|
import {
|
|
48
47
|
bs58,
|
|
49
48
|
binToHex,
|
|
50
|
-
contractIdFromAddress,
|
|
51
49
|
Subscription,
|
|
52
50
|
assertType,
|
|
53
51
|
Eq,
|
|
54
52
|
Optional,
|
|
55
|
-
groupOfAddress,
|
|
56
53
|
WebCrypto,
|
|
57
54
|
hexToBinUnsafe,
|
|
58
55
|
isDevnet,
|
|
59
|
-
addressFromContractId,
|
|
60
|
-
subContractId,
|
|
61
56
|
HexString
|
|
62
57
|
} from '../utils'
|
|
58
|
+
import { contractIdFromAddress, groupOfAddress, addressFromContractId, subContractId } from '../address'
|
|
63
59
|
import { getCurrentNodeProvider } from '../global'
|
|
64
60
|
import { EventSubscribeOptions, EventSubscription, subscribeToEvents } from './events'
|
|
65
61
|
import { ONE_ALPH, TOTAL_NUMBER_OF_GROUPS } from '../constants'
|
|
@@ -220,7 +216,7 @@ export class Contract extends Artifact {
|
|
|
220
216
|
this.bytecodeDebug = ralph.buildDebugBytecode(this.bytecode, this.bytecodeDebugPatch)
|
|
221
217
|
this.codeHashDebug = codeHashDebug
|
|
222
218
|
|
|
223
|
-
this.decodedMethods = contract.contractCodec.decodeContract(
|
|
219
|
+
this.decodedMethods = contract.contractCodec.decodeContract(hexToBinUnsafe(bytecode)).methods
|
|
224
220
|
}
|
|
225
221
|
|
|
226
222
|
publicFunctions(): FunctionSig[] {
|
|
@@ -1208,7 +1204,7 @@ function genCodeForType(type: string, structs: Struct[]): { bytecode: string; co
|
|
|
1208
1204
|
fieldLength: immFields + mutFields + 1, // parentContractId
|
|
1209
1205
|
methods: [loadImmFieldByIndex, loadMutFieldByIndex, storeMutFieldByIndex, destroy]
|
|
1210
1206
|
}
|
|
1211
|
-
const bytecode = contract.contractCodec.
|
|
1207
|
+
const bytecode = contract.contractCodec.encodeContract(c)
|
|
1212
1208
|
const codeHash = blake.blake2b(bytecode, undefined, 32)
|
|
1213
1209
|
return { bytecode: binToHex(bytecode), codeHash: binToHex(codeHash) }
|
|
1214
1210
|
}
|
package/src/contract/ralph.ts
CHANGED
|
@@ -16,9 +16,8 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { Buffer } from 'buffer/'
|
|
20
19
|
import { Val, decodeArrayType, toApiAddress, toApiBoolean, toApiByteVec, toApiNumber256 } from '../api'
|
|
21
|
-
import { HexString, binToHex, bs58, hexToBinUnsafe, isHexString } from '../utils'
|
|
20
|
+
import { HexString, binToHex, bs58, concatBytes, hexToBinUnsafe, isHexString } from '../utils'
|
|
22
21
|
import { Fields, FieldsSig, Struct } from './contract'
|
|
23
22
|
import { compactSignedIntCodec, compactUnsignedIntCodec } from '../codec'
|
|
24
23
|
|
|
@@ -151,14 +150,14 @@ export function encodeU256(u256: bigint): Uint8Array {
|
|
|
151
150
|
}
|
|
152
151
|
}
|
|
153
152
|
|
|
154
|
-
export function encodeByteVec(
|
|
155
|
-
if (!isHexString(
|
|
156
|
-
throw Error(`Given value ${
|
|
153
|
+
export function encodeByteVec(hex: string): Uint8Array {
|
|
154
|
+
if (!isHexString(hex)) {
|
|
155
|
+
throw Error(`Given value ${hex} is not a valid hex string`)
|
|
157
156
|
}
|
|
158
157
|
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
return
|
|
158
|
+
const bytes0 = hexToBinUnsafe(hex)
|
|
159
|
+
const bytes1 = encodeI256(BigInt(bytes0.length))
|
|
160
|
+
return concatBytes([bytes1, bytes0])
|
|
162
161
|
}
|
|
163
162
|
|
|
164
163
|
export function encodeAddress(address: string): Uint8Array {
|
|
@@ -254,7 +253,7 @@ function encodeScriptFieldU256(value: bigint): Uint8Array {
|
|
|
254
253
|
}
|
|
255
254
|
|
|
256
255
|
export function encodeScriptFieldAsString(tpe: string, value: Val): string {
|
|
257
|
-
return
|
|
256
|
+
return binToHex(encodeScriptField(tpe, value))
|
|
258
257
|
}
|
|
259
258
|
|
|
260
259
|
export function encodeScriptField(tpe: string, value: Val): Uint8Array {
|
|
@@ -378,9 +377,9 @@ export function decodePrimitive(value: Uint8Array, type: string): Val {
|
|
|
378
377
|
case 'Bool':
|
|
379
378
|
return decodeBool(value)
|
|
380
379
|
case 'I256':
|
|
381
|
-
return compactSignedIntCodec.decodeI256(
|
|
380
|
+
return compactSignedIntCodec.decodeI256(value)
|
|
382
381
|
case 'U256':
|
|
383
|
-
return compactUnsignedIntCodec.decodeU256(
|
|
382
|
+
return compactUnsignedIntCodec.decodeU256(value)
|
|
384
383
|
case 'ByteVec':
|
|
385
384
|
return binToHex(value)
|
|
386
385
|
case 'Address':
|
|
@@ -16,19 +16,11 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
addressFromPublicKey,
|
|
22
|
-
addressFromScript,
|
|
23
|
-
binToHex,
|
|
24
|
-
bs58,
|
|
25
|
-
hexToBinUnsafe,
|
|
26
|
-
isHexString
|
|
27
|
-
} from '../utils'
|
|
19
|
+
import { AddressType, addressFromPublicKey, addressFromScript } from '../address'
|
|
20
|
+
import { binToHex, bs58, hexToBinUnsafe, isHexString } from '../utils'
|
|
28
21
|
import { Transaction } from '../api/api-alephium'
|
|
29
22
|
import { Address } from '../signer'
|
|
30
23
|
import { P2SH, unlockScriptCodec } from '../codec/unlock-script-codec'
|
|
31
|
-
import { Buffer } from 'buffer/'
|
|
32
24
|
import { scriptCodec } from '../codec/script-codec'
|
|
33
25
|
|
|
34
26
|
export function validateExchangeAddress(address: string) {
|
|
@@ -113,7 +105,7 @@ export function getAddressFromUnlockScript(unlockScript: string): Address {
|
|
|
113
105
|
if (unlockScriptType === UnlockScriptType.P2SH) {
|
|
114
106
|
let p2sh: P2SH
|
|
115
107
|
try {
|
|
116
|
-
p2sh = unlockScriptCodec.decode(
|
|
108
|
+
p2sh = unlockScriptCodec.decode(decoded).script as P2SH
|
|
117
109
|
} catch (_) {
|
|
118
110
|
throw new Error(`Invalid p2sh unlock script: ${unlockScript}`)
|
|
119
111
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
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 { validateExchangeAddress, isALPHTransferTx, getSenderAddress, getALPHDepositInfo } from './exchange'
|