@alephium/web3 0.2.0-rc.7 → 0.2.0-rc.8
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/.eslintignore +2 -2
- package/README.md +2 -135
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/scripts/create-project.js +1 -1
- package/dist/src/api/api-alephium.d.ts +19 -6
- package/dist/src/api/api-explorer.d.ts +16 -16
- package/dist/src/api/index.js +5 -1
- package/dist/src/contract/contract.d.ts +27 -10
- package/dist/src/contract/contract.js +105 -64
- package/dist/src/contract/index.js +5 -1
- package/dist/src/global.d.ts +3 -0
- package/{src/utils/djb2.test.ts → dist/src/global.js} +20 -17
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +20 -1
- package/dist/src/signer/index.d.ts +0 -1
- package/dist/src/signer/index.js +5 -2
- package/dist/src/signer/signer.d.ts +1 -1
- package/dist/src/signer/signer.js +8 -3
- package/dist/src/transaction/index.d.ts +0 -1
- package/dist/src/transaction/index.js +5 -2
- package/dist/src/utils/index.d.ts +0 -1
- package/dist/src/utils/index.js +5 -2
- package/dist/src/utils/subscription.d.ts +0 -1
- package/dist/src/utils/subscription.js +2 -1
- package/dist/src/utils/utils.d.ts +3 -2
- package/dist/src/utils/utils.js +16 -15
- package/jest-config.json +11 -0
- package/package.json +6 -32
- package/scripts/create-project.ts +1 -1
- package/src/api/api-alephium.ts +14 -0
- package/src/contract/contract.ts +157 -69
- package/src/{test/index.ts → global.ts} +14 -10
- package/src/index.ts +3 -0
- package/src/signer/index.ts +0 -1
- package/src/signer/signer.ts +3 -2
- package/src/transaction/index.ts +0 -1
- package/src/utils/index.ts +0 -1
- package/src/utils/subscription.ts +2 -2
- package/src/utils/utils.ts +7 -7
- package/templates/base/package.json +1 -1
- package/templates/base/src/greeter.ts +8 -7
- package/templates/react/package.json +1 -1
- package/templates/react/src/App.tsx +2 -2
- package/.eslintrc.json +0 -21
- package/LICENSE +0 -165
- package/contracts/add/add.ral +0 -13
- package/contracts/main.ral +0 -4
- package/contracts/sub/sub.ral +0 -10
- package/contracts/test/metadata.ral +0 -18
- package/contracts/test/warnings.ral +0 -8
- package/dist/src/signer/node-wallet.d.ts +0 -13
- package/dist/src/signer/node-wallet.js +0 -60
- package/dist/src/test/index.d.ts +0 -7
- package/dist/src/test/index.js +0 -41
- package/dist/src/test/privatekey-wallet.d.ts +0 -12
- package/dist/src/test/privatekey-wallet.js +0 -68
- package/dist/src/transaction/sign-verify.d.ts +0 -2
- package/dist/src/transaction/sign-verify.js +0 -58
- package/dist/src/utils/password-crypto.d.ts +0 -2
- package/dist/src/utils/password-crypto.js +0 -69
- package/gitignore +0 -9
- package/src/contract/ralph.test.ts +0 -178
- package/src/fixtures/address.json +0 -36
- package/src/fixtures/balance.json +0 -9
- package/src/fixtures/self-clique.json +0 -19
- package/src/fixtures/transaction.json +0 -13
- package/src/fixtures/transactions.json +0 -179
- package/src/signer/fixtures/genesis.json +0 -26
- package/src/signer/fixtures/wallets.json +0 -26
- package/src/signer/node-wallet.ts +0 -74
- package/src/test/privatekey-wallet.ts +0 -58
- package/src/transaction/sign-verify.test.ts +0 -50
- package/src/transaction/sign-verify.ts +0 -39
- package/src/utils/address.test.ts +0 -47
- package/src/utils/password-crypto.test.ts +0 -27
- package/src/utils/password-crypto.ts +0 -77
- package/src/utils/utils.test.ts +0 -161
- package/test/contract.test.ts +0 -213
- package/test/events.test.ts +0 -143
- package/test/transaction.test.ts +0 -74
|
@@ -1,50 +0,0 @@
|
|
|
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 EC from 'elliptic'
|
|
20
|
-
import assert from 'assert'
|
|
21
|
-
|
|
22
|
-
import { transactionSign, transactionVerifySignature } from './sign-verify'
|
|
23
|
-
|
|
24
|
-
describe('transaction', function () {
|
|
25
|
-
it('should verify signature', () => {
|
|
26
|
-
const txHash = '8fc5f0d120b730f97f6cea5f02ae4a6ee7bf451d9261c623ea69d85e870201d2'
|
|
27
|
-
const pubKey = '02625b26ae1c5f7986475009e4037b3e6fe6320fde3c3f3332bea11ecadc35dd13'
|
|
28
|
-
const txSig =
|
|
29
|
-
'78471e7c97e558c98ac307ef699ed535ece319102fc69ea416dbb44fbb3cbf9c42dbfbf4ce73eb68c5e0d66122eb25d2ebe1cf9e37ef4c4f4e7a2ed35de141bc'
|
|
30
|
-
const unnormalizedSig =
|
|
31
|
-
'78471e7c97e558c98ac307ef699ed535ece319102fc69ea416dbb44fbb3cbf9cbd24040b318c14973a1f299edd14da2bcecd0d48775953ec71582fb97254ff85'
|
|
32
|
-
const wrongSig =
|
|
33
|
-
'88471e7c97e558c98ac307ef699ed535ece319102fc69ea416dbb44fbb3cbf9c42dbfbf4ce73eb68c5e0d66122eb25d2ebe1cf9e37ef4c4f4e7a2ed35de141bc'
|
|
34
|
-
|
|
35
|
-
assert.strictEqual(transactionVerifySignature(txHash, pubKey, txSig), true)
|
|
36
|
-
assert.strictEqual(transactionVerifySignature(txHash, pubKey, unnormalizedSig), false)
|
|
37
|
-
assert.strictEqual(transactionVerifySignature(txHash, pubKey, wrongSig), false)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('should sign and verify signature', () => {
|
|
41
|
-
const ec = new EC.ec('secp256k1')
|
|
42
|
-
const key = ec.genKeyPair()
|
|
43
|
-
const privateKey = key.getPrivate().toString('hex')
|
|
44
|
-
const publicKey = key.getPublic().encode('hex', true)
|
|
45
|
-
|
|
46
|
-
const txHash = '8fc5f0d120b730f97f6cea5f02ae4a6ee7bf451d9261c623ea69d85e870201d2'
|
|
47
|
-
const signature = transactionSign(txHash, privateKey)
|
|
48
|
-
assert.strictEqual(transactionVerifySignature(txHash, publicKey, signature), true)
|
|
49
|
-
})
|
|
50
|
-
})
|
|
@@ -1,39 +0,0 @@
|
|
|
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 { ec as EC } from 'elliptic'
|
|
20
|
-
|
|
21
|
-
import * as utils from '../utils'
|
|
22
|
-
|
|
23
|
-
const ec = new EC('secp256k1')
|
|
24
|
-
|
|
25
|
-
export function transactionSign(txHash: string, privateKey: string): string {
|
|
26
|
-
const keyPair = ec.keyFromPrivate(privateKey)
|
|
27
|
-
const signature = keyPair.sign(txHash)
|
|
28
|
-
|
|
29
|
-
return utils.signatureEncode(signature)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function transactionVerifySignature(txHash: string, publicKey: string, signature: string): boolean {
|
|
33
|
-
try {
|
|
34
|
-
const key = ec.keyFromPublic(publicKey, 'hex')
|
|
35
|
-
return key.verify(txHash, utils.signatureDecode(ec, signature))
|
|
36
|
-
} catch (error) {
|
|
37
|
-
return false
|
|
38
|
-
}
|
|
39
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
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 { addressToGroup } from './address'
|
|
20
|
-
|
|
21
|
-
describe('address', function () {
|
|
22
|
-
it('should derive group', async () => {
|
|
23
|
-
function check(address: string, expected: number) {
|
|
24
|
-
const group = addressToGroup(address, 4)
|
|
25
|
-
expect(group).toEqual(expected)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
check('12psscGPMgdqctaeCA37HYAkpVBFX1LbN4dSvjyxbDyKk', 1)
|
|
29
|
-
check('16TGLiD3fqyuGFRFHffh58BC3okYCbjRH1WHPzeSp39Wi', 2)
|
|
30
|
-
check('15aTcpJfCX9akQqYuRMMgun6Mv6ek8bigB98VTUFMwKYA', 1)
|
|
31
|
-
check('164ejvnxGYRPUt3tYwJrMxBLLmeag2WACH4GfcsRUN3W7', 2)
|
|
32
|
-
check('15p5vK921GnxFSQgXZo8Wceg6EvwcXZ9rCQxE2SeXc1s5', 1)
|
|
33
|
-
check('1HSLAetSuMTKPHukvXYg6yaDuUJ67vxGashzFLEuu6qV6', 1)
|
|
34
|
-
check('1HbU1TDiUMAj33Cp5cA2xc9uTqp1bjWa5UvkeGLm4bDbE', 2)
|
|
35
|
-
check('13zn4s3fb5Q9d8rtszYdmYVpQ3MM9VM2xLBe9rMhcNxjd', 0)
|
|
36
|
-
check('1DdQwce5ZzFrEYyz1H5KU9v8hRpoTbq5i7zE5Nu5k4ope', 2)
|
|
37
|
-
check('19hpcUVGzdpWRD8yVUyP9pJwwxD6P5ixGUgSmVPbhBAVX', 2)
|
|
38
|
-
check('1DpNeY8uutS1FRW7D565WjUgu5HcKSYAMqZNWgUJWggWZ', 0)
|
|
39
|
-
check('1G1gjpt4mxij7JwP5SpX6ScwQHisWN3V5WBtFfWKtc3vo', 3)
|
|
40
|
-
check('19XyGb6f1upjvQAG4vexB1EmY3pU8G2VN5gM267Pdhogg', 3)
|
|
41
|
-
check('1H5YniQrUqxJY9ShTjMeX6DMgjPpYfdFqDvTgBYv6h1iz', 1)
|
|
42
|
-
check('18Ca9jZDqRcxTfdNr2hq5KBJjf7PJLA5PXMjRoeB83JNv', 3)
|
|
43
|
-
check('15XyPNJuZ85wyUMs4mwn98LLPMjiwUSCuTmR74NuxpwXT', 0)
|
|
44
|
-
check('1F6ssQRwH1p1omaoQR3eirFrycHC3mUr3Vw9pLcgEe33W', 1)
|
|
45
|
-
check('19JtVnQ4YLcA9mWPafnLmtWarAjdaLR3d7R5RAUjxHbe1', 3)
|
|
46
|
-
})
|
|
47
|
-
})
|
|
@@ -1,27 +0,0 @@
|
|
|
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 { decrypt } from './password-crypto'
|
|
20
|
-
|
|
21
|
-
describe('password-crypto', () => {
|
|
22
|
-
it('should raise an error if payload version is not 1', () => {
|
|
23
|
-
const password = 'passw0rd'
|
|
24
|
-
const payloadRaw = '{"version":2}'
|
|
25
|
-
expect(() => decrypt(password, payloadRaw)).toThrow('Invalid version: got 2, expected: 1')
|
|
26
|
-
})
|
|
27
|
-
})
|
|
@@ -1,77 +0,0 @@
|
|
|
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 { randomBytes, createCipheriv, createDecipheriv, pbkdf2Sync, CipherKey, BinaryLike } from 'crypto'
|
|
21
|
-
|
|
22
|
-
const saltByteLength = 64
|
|
23
|
-
const ivByteLength = 64
|
|
24
|
-
const authTagLength = 16
|
|
25
|
-
|
|
26
|
-
export const encrypt = (password: string, dataRaw: string): string => {
|
|
27
|
-
const data = Buffer.from(dataRaw, 'utf8')
|
|
28
|
-
|
|
29
|
-
const salt = randomBytes(saltByteLength)
|
|
30
|
-
const derivedKey = keyFromPassword(password, salt)
|
|
31
|
-
const iv = randomBytes(ivByteLength)
|
|
32
|
-
const cipher = createCipher(derivedKey, iv)
|
|
33
|
-
const encrypted = Buffer.concat([cipher.update(data), cipher.final()])
|
|
34
|
-
const authTag = cipher.getAuthTag()
|
|
35
|
-
const payload = {
|
|
36
|
-
salt: salt.toString('hex'),
|
|
37
|
-
iv: iv.toString('hex'),
|
|
38
|
-
encrypted: Buffer.concat([encrypted, authTag]).toString('hex'),
|
|
39
|
-
version: 1
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return JSON.stringify(payload)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const decrypt = (password: string, payloadRaw: string): string => {
|
|
46
|
-
const payload = JSON.parse(payloadRaw)
|
|
47
|
-
|
|
48
|
-
const version = payload.version
|
|
49
|
-
if (version !== 1) {
|
|
50
|
-
throw new Error(`Invalid version: got ${version}, expected: 1`)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const salt = Buffer.from(payload.salt, 'hex')
|
|
54
|
-
const iv = Buffer.from(payload.iv, 'hex')
|
|
55
|
-
const encrypted = Buffer.from(payload.encrypted, 'hex')
|
|
56
|
-
|
|
57
|
-
const derivedKey = keyFromPassword(password, salt)
|
|
58
|
-
const decipher = createDecipher(derivedKey, iv)
|
|
59
|
-
const data = encrypted.slice(0, encrypted.length - authTagLength)
|
|
60
|
-
const authTag = encrypted.slice(encrypted.length - authTagLength, encrypted.length)
|
|
61
|
-
decipher.setAuthTag(authTag)
|
|
62
|
-
const decrypted = Buffer.concat([decipher.update(data), decipher.final()])
|
|
63
|
-
|
|
64
|
-
return decrypted.toString('utf8')
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const createCipher = (key: CipherKey, iv: BinaryLike | null) => {
|
|
68
|
-
return createCipheriv('aes-256-gcm', key, iv)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const createDecipher = (key: CipherKey, iv: BinaryLike | null) => {
|
|
72
|
-
return createDecipheriv('aes-256-gcm', key, iv)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const keyFromPassword = (password: BinaryLike, salt: BinaryLike) => {
|
|
76
|
-
return pbkdf2Sync(password, salt, 10000, 32, 'sha256')
|
|
77
|
-
}
|
package/src/utils/utils.test.ts
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
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 EC from 'elliptic'
|
|
20
|
-
import assert from 'assert'
|
|
21
|
-
|
|
22
|
-
import * as utils from './utils'
|
|
23
|
-
|
|
24
|
-
describe('utils', function () {
|
|
25
|
-
it('should throw an error when decoding invalid signature', () => {
|
|
26
|
-
const ec = new EC.ec('secp256k1')
|
|
27
|
-
const signature = 'signature-with-wrong-length'
|
|
28
|
-
|
|
29
|
-
expect(() => utils.signatureDecode(ec, signature)).toThrowError('Invalid signature length')
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
it('should compress signature', () => {
|
|
33
|
-
const vectors = [
|
|
34
|
-
[
|
|
35
|
-
'Satoshi Nakamoto',
|
|
36
|
-
'0000000000000000000000000000000000000000000000000000000000000001',
|
|
37
|
-
'934b1ea10a4b3c1757e2b0c017d0b6143ce3c9a7e6a4a49860d7a6ab210ee3d82442ce9d2b916064108014783e923ec36b49743e2ffa1c4496f01a512aafd9e5'
|
|
38
|
-
],
|
|
39
|
-
[
|
|
40
|
-
'Everything should be made as simple as possible, but not simpler.',
|
|
41
|
-
'0000000000000000000000000000000000000000000000000000000000000001',
|
|
42
|
-
'33a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c96f807982866f785d3f6418d24163ddae117b7db4d5fdf0071de069fa54342262'
|
|
43
|
-
],
|
|
44
|
-
[
|
|
45
|
-
'All those moments will be lost in time, like tears in rain. Time to die...',
|
|
46
|
-
'0000000000000000000000000000000000000000000000000000000000000001',
|
|
47
|
-
'8600dbd41e348fe5c9465ab92d23e3db8b98b873beecd930736488696438cb6b547fe64427496db33bf66019dacbf0039c04199abb0122918601db38a72cfc21'
|
|
48
|
-
],
|
|
49
|
-
[
|
|
50
|
-
'Satoshi Nakamoto',
|
|
51
|
-
'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140',
|
|
52
|
-
'fd567d121db66e382991534ada77a6bd3106f0a1098c231e47993447cd6af2d06b39cd0eb1bc8603e159ef5c20a5c8ad685a45b06ce9bebed3f153d10d93bed5'
|
|
53
|
-
],
|
|
54
|
-
[
|
|
55
|
-
'Alan Turing',
|
|
56
|
-
'f8b8af8ce3c7cca5e300d33939540c10d45ce001b8f252bfbc57ba0342904181',
|
|
57
|
-
'7063ae83e7f62bbb171798131b4a0564b956930092b33b07b395615d9ec7e15c58dfcc1e00a35e1572f366ffe34ba0fc47db1e7189759b9fb233c5b05ab388ea'
|
|
58
|
-
],
|
|
59
|
-
[
|
|
60
|
-
"There is a computer disease that anybody who works with computers knows about. It's a very serious disease and it interferes completely with the work. The trouble with computers is that you 'play' with them!",
|
|
61
|
-
'e91671c46231f833a6406ccbea0e3e392c76c167bac1cb013f6f1013980455c2',
|
|
62
|
-
'b552edd27580141f3b2a5463048cb7cd3e047b97c9f98076c32dbdf85a68718b279fa72dd19bfae05577e06c7c0c1900c371fcd5893f7e1d56a37d30174671f6'
|
|
63
|
-
]
|
|
64
|
-
]
|
|
65
|
-
|
|
66
|
-
const ec = new EC.ec('secp256k1')
|
|
67
|
-
|
|
68
|
-
vectors.forEach((vector) => {
|
|
69
|
-
const privateKey = vector[1]
|
|
70
|
-
const signatureExpected = vector[2]
|
|
71
|
-
const keyPair = ec.keyFromPrivate(privateKey)
|
|
72
|
-
|
|
73
|
-
const message = vector[0]
|
|
74
|
-
const sha256 = ec.hash().update(message).digest()
|
|
75
|
-
const signature = utils.signatureEncode(keyPair.sign(sha256))
|
|
76
|
-
assert.deepStrictEqual(signature, signatureExpected)
|
|
77
|
-
})
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
it('should calculate the group of addresses', () => {
|
|
81
|
-
expect(utils.groupOfAddress('15EM5rGtt7dPRZScE4Z9oL2EDfj84JnoSgq3NNgdcGFyu')).toBe(0),
|
|
82
|
-
expect(utils.groupOfAddress('1D59jXR9NpD9ZQqZTRVcVbKVh6ko5TUMt89WvkA8P9P7w')).toBe(1),
|
|
83
|
-
expect(utils.groupOfAddress('14tAT3nm7UqVP7gZ35icSdT3AEffv1kaUUMbWQK5PFygr')).toBe(2),
|
|
84
|
-
expect(utils.groupOfAddress('12F5aVQoQ7cNrgsVN2YPciwYvwmtJp4ohLa2x4R5KgLbG')).toBe(3),
|
|
85
|
-
expect(
|
|
86
|
-
utils.groupOfAddress(
|
|
87
|
-
'2jW1n2icPtc55Cdm8TF9FjGH681cWthsaZW3gaUFekFZepJoeyY3ZbY7y5SCtAjyCjLL24c4L2Vnfv3KDdAypCddfAY'
|
|
88
|
-
)
|
|
89
|
-
).toBe(0),
|
|
90
|
-
expect(
|
|
91
|
-
utils.groupOfAddress(
|
|
92
|
-
'2jXboVD9p66wrAHkPHx2AQocAzYXUWeppmRT3PuVT3ccxX9u8puTnwLeQ2VbTd4sNkgSEgk1cLbyVGLFshGweJCk1Mr'
|
|
93
|
-
)
|
|
94
|
-
).toBe(1),
|
|
95
|
-
expect(
|
|
96
|
-
utils.groupOfAddress(
|
|
97
|
-
'2je1yvQHpg8bKCDmvr1koELSNbty5DHrHYRkXomiRNvP5VcsZTK3WisBco2sCtCULM2YbxRxPd7QwhdP2hz9PEQwB1S'
|
|
98
|
-
)
|
|
99
|
-
).toBe(2),
|
|
100
|
-
expect(
|
|
101
|
-
utils.groupOfAddress(
|
|
102
|
-
'2jWukVCejM4Zifz9LvMG4dfR6SEecHLX8VqbswhGwnu61d28B861UhLu3ZmTHu4N14m1kk9rbxreBYzcxta1WPawKzG'
|
|
103
|
-
)
|
|
104
|
-
).toBe(3),
|
|
105
|
-
expect(utils.groupOfAddress('eBrjfQNeyUCuxE4zpbfMZcbS3PuvbMJDQBCyk4HRHtX4')).toBe(0),
|
|
106
|
-
expect(utils.groupOfAddress('euWxyF55nGTxavL6mgGeMrFdvSRzHor8AmhgPXm8Lm9D')).toBe(1),
|
|
107
|
-
expect(utils.groupOfAddress('n2pYTzmA27tkp7UNFPhMJpjz3jr5vgessxqJ7kwomBMF')).toBe(2),
|
|
108
|
-
expect(utils.groupOfAddress('tLf6hDfrUugmxZhKxGoZMpAUBt3NcZ2hrTspTCmZ6JdQ')).toBe(3)
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
it('should extract token id from addresses', () => {
|
|
112
|
-
expect(utils.binToHex(utils.tokenIdFromAddress('wCTeteGBeSEC54GpkS8jWBzYiYNTBUuTW3WzxGd9yExT'))).toBe(
|
|
113
|
-
'25469eb0d0d0a55deea832924547b7b166c70a3554fe321e81886d3c18f19d64'
|
|
114
|
-
),
|
|
115
|
-
expect(utils.binToHex(utils.tokenIdFromAddress('xrY8dxgVm38QCQXhiUFcivFutLFUNMoo8qu8vYf7wJps'))).toBe(
|
|
116
|
-
'3de370f893cb1383c828c0eb22c89aceb13fa56ddced1848db27ce7fa419c80c'
|
|
117
|
-
),
|
|
118
|
-
expect(() => utils.tokenIdFromAddress('eBrjfQNeyUCuxE4zpbfMZcbS3PuvbMJDQBCyk4HRHtX4')).toThrow(
|
|
119
|
-
'Invalid contract address type: 2'
|
|
120
|
-
),
|
|
121
|
-
expect(() => utils.tokenIdFromAddress('..')).toThrow('Non-base58 character')
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
it('should compute public key from private key', () => {
|
|
125
|
-
expect(utils.publicKeyFromPrivateKey('91411e484289ec7e8b3058697f53f9b26fa7305158b4ef1a81adfbabcf090e45')).toBe(
|
|
126
|
-
'030f9f042a9410969f1886f85fa20f6e43176ae23fc5e64db15b3767c84c5db2dc'
|
|
127
|
-
)
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
it('should compute address from public key', () => {
|
|
131
|
-
expect(utils.publicKeyFromPrivateKey('91411e484289ec7e8b3058697f53f9b26fa7305158b4ef1a81adfbabcf090e45')).toBe(
|
|
132
|
-
'030f9f042a9410969f1886f85fa20f6e43176ae23fc5e64db15b3767c84c5db2dc'
|
|
133
|
-
)
|
|
134
|
-
expect(utils.addressFromPublicKey('030f9f042a9410969f1886f85fa20f6e43176ae23fc5e64db15b3767c84c5db2dc')).toBe(
|
|
135
|
-
'1ACCkgFfmTif46T3qK12znuWjb5Bk9jXpqaeWt2DXx8oc'
|
|
136
|
-
)
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
it('should convert between contract id and address', () => {
|
|
140
|
-
expect(utils.addressFromContractId('1f6b937b935d7fac894fb22ffe2b974cae9c8c166501372f1b9155144e0ff4ae')).toBe(
|
|
141
|
-
'vobthYg1e9tPKhmF96rpkv3akCj7vhvgPpsP4qwZqDw3'
|
|
142
|
-
)
|
|
143
|
-
expect(utils.binToHex(utils.contractIdFromAddress('vobthYg1e9tPKhmF96rpkv3akCj7vhvgPpsP4qwZqDw3'))).toBe(
|
|
144
|
-
'1f6b937b935d7fac894fb22ffe2b974cae9c8c166501372f1b9155144e0ff4ae'
|
|
145
|
-
)
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
it('should compute id of the sub contract', () => {
|
|
149
|
-
expect(
|
|
150
|
-
utils.subContractId(
|
|
151
|
-
'0a38bc48fbb4300f1e305b201cd6129372d867122efb814d871d18c0bfe43b56',
|
|
152
|
-
'4f51cd1f0af97cf5ec9c7a3397eaeea549d55a93c216e54f2ab4a8cf29f6f865'
|
|
153
|
-
)
|
|
154
|
-
).toBe('0e28f15ca290002c31d691aa008aa56ac12356b0380efb6c88fff929b6a268a9')
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
it('should convert from string to hex and back', () => {
|
|
158
|
-
expect(utils.stringToHex('Hello Alephium!')).toBe('48656c6c6f20416c65706869756d21')
|
|
159
|
-
expect(utils.hexToString('48656c6c6f20416c65706869756d21')).toBe('Hello Alephium!')
|
|
160
|
-
})
|
|
161
|
-
})
|
package/test/contract.test.ts
DELETED
|
@@ -1,213 +0,0 @@
|
|
|
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 * as fs from 'fs'
|
|
20
|
-
import * as path from 'path'
|
|
21
|
-
|
|
22
|
-
import { NodeProvider } from '../src/api'
|
|
23
|
-
import { Contract, Project, Script, TestContractParams } from '../src/contract'
|
|
24
|
-
import { testWallet } from '../src/test'
|
|
25
|
-
import { addressFromContractId } from '../src/utils'
|
|
26
|
-
|
|
27
|
-
describe('contract', function () {
|
|
28
|
-
async function testSuite1() {
|
|
29
|
-
const provider = new NodeProvider('http://127.0.0.1:22973')
|
|
30
|
-
await Project.build(provider)
|
|
31
|
-
|
|
32
|
-
// ignore unused private function warnings
|
|
33
|
-
const add = Project.contract('add/add.ral', { errorOnWarnings: false })
|
|
34
|
-
const sub = Project.contract('sub/sub.ral')
|
|
35
|
-
|
|
36
|
-
const subState = sub.toState({ result: 0 }, { alphAmount: BigInt('1000000000000000000') })
|
|
37
|
-
const testParams: TestContractParams = {
|
|
38
|
-
initialFields: { sub: subState.contractId, result: 0 },
|
|
39
|
-
testArgs: { array: [2, 1] },
|
|
40
|
-
existingContracts: [subState]
|
|
41
|
-
}
|
|
42
|
-
const testResult = await add.testPublicMethod('add', testParams)
|
|
43
|
-
expect(testResult.returns).toEqual([[3, 1]])
|
|
44
|
-
expect(testResult.contracts[0].codeHash).toEqual(sub.codeHash)
|
|
45
|
-
expect(testResult.contracts[0].fields.result).toEqual(1)
|
|
46
|
-
expect(testResult.contracts[1].codeHash).toEqual(add.codeHash)
|
|
47
|
-
expect(testResult.contracts[1].fields.sub).toEqual(subState.contractId)
|
|
48
|
-
expect(testResult.contracts[1].fields.result).toEqual(3)
|
|
49
|
-
const events = testResult.events.sort((a, b) => a.name.localeCompare(b.name))
|
|
50
|
-
expect(events[0].name).toEqual('Add')
|
|
51
|
-
expect(events[0].fields.x).toEqual(2)
|
|
52
|
-
expect(events[0].fields.y).toEqual(1)
|
|
53
|
-
expect(events[1].name).toEqual('Sub')
|
|
54
|
-
expect(events[1].fields.x).toEqual(2)
|
|
55
|
-
expect(events[1].fields.y).toEqual(1)
|
|
56
|
-
|
|
57
|
-
const testResultPrivate = await add.testPrivateMethod('addPrivate', testParams)
|
|
58
|
-
expect(testResultPrivate.returns).toEqual([[3, 1]])
|
|
59
|
-
|
|
60
|
-
const signer = await testWallet(provider)
|
|
61
|
-
|
|
62
|
-
const subDeployTx = await sub.transactionForDeployment(signer, {
|
|
63
|
-
initialFields: { result: 0 },
|
|
64
|
-
initialTokenAmounts: []
|
|
65
|
-
})
|
|
66
|
-
const subContractId = subDeployTx.contractId
|
|
67
|
-
const subContractAddress = addressFromContractId(subContractId)
|
|
68
|
-
expect(subDeployTx.fromGroup).toEqual(0)
|
|
69
|
-
expect(subDeployTx.toGroup).toEqual(0)
|
|
70
|
-
const subSubmitResult = await signer.submitTransaction(subDeployTx.unsignedTx, subDeployTx.txId)
|
|
71
|
-
expect(subSubmitResult.fromGroup).toEqual(0)
|
|
72
|
-
expect(subSubmitResult.toGroup).toEqual(0)
|
|
73
|
-
expect(subSubmitResult.txId).toEqual(subDeployTx.txId)
|
|
74
|
-
|
|
75
|
-
const addDeployTx = await add.transactionForDeployment(signer, {
|
|
76
|
-
initialFields: { sub: subContractId, result: 0 },
|
|
77
|
-
initialTokenAmounts: []
|
|
78
|
-
})
|
|
79
|
-
expect(addDeployTx.fromGroup).toEqual(0)
|
|
80
|
-
expect(addDeployTx.toGroup).toEqual(0)
|
|
81
|
-
const addSubmitResult = await signer.submitTransaction(addDeployTx.unsignedTx, addDeployTx.txId)
|
|
82
|
-
expect(addSubmitResult.fromGroup).toEqual(0)
|
|
83
|
-
expect(addSubmitResult.toGroup).toEqual(0)
|
|
84
|
-
expect(addSubmitResult.txId).toEqual(addDeployTx.txId)
|
|
85
|
-
|
|
86
|
-
const addContractId = addDeployTx.contractId
|
|
87
|
-
const addContractAddress = addressFromContractId(addContractId)
|
|
88
|
-
|
|
89
|
-
// Check state for add/sub before main script is executed
|
|
90
|
-
let fetchedSubState = await sub.fetchState(subContractAddress, 0)
|
|
91
|
-
expect(fetchedSubState.fields.result).toEqual(0)
|
|
92
|
-
let fetchedAddState = await add.fetchState(addContractAddress, 0)
|
|
93
|
-
expect(fetchedAddState.fields.sub).toEqual(subContractId)
|
|
94
|
-
expect(fetchedAddState.fields.result).toEqual(0)
|
|
95
|
-
|
|
96
|
-
const main = Project.script('main.ral')
|
|
97
|
-
const mainScriptTx = await main.transactionForDeployment(signer, {
|
|
98
|
-
initialFields: { addContractId: addContractId }
|
|
99
|
-
})
|
|
100
|
-
expect(mainScriptTx.fromGroup).toEqual(0)
|
|
101
|
-
expect(mainScriptTx.toGroup).toEqual(0)
|
|
102
|
-
const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx, mainScriptTx.txId)
|
|
103
|
-
expect(mainSubmitResult.fromGroup).toEqual(0)
|
|
104
|
-
expect(mainSubmitResult.toGroup).toEqual(0)
|
|
105
|
-
|
|
106
|
-
// Check state for add/sub after main script is executed
|
|
107
|
-
fetchedSubState = await sub.fetchState(subContractAddress, 0)
|
|
108
|
-
expect(fetchedSubState.fields.result).toEqual(1)
|
|
109
|
-
fetchedAddState = await add.fetchState(addContractAddress, 0)
|
|
110
|
-
expect(fetchedAddState.fields.sub).toEqual(subContractId)
|
|
111
|
-
expect(fetchedAddState.fields.result).toEqual(3)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async function testSuite2() {
|
|
115
|
-
const provider = new NodeProvider('http://127.0.0.1:22973')
|
|
116
|
-
await Project.build(provider)
|
|
117
|
-
|
|
118
|
-
const greeter = Project.contract('greeter/greeter.ral')
|
|
119
|
-
|
|
120
|
-
const testParams: TestContractParams = {
|
|
121
|
-
initialFields: { btcPrice: 1 }
|
|
122
|
-
}
|
|
123
|
-
const testResult = await greeter.testPublicMethod('greet', testParams)
|
|
124
|
-
expect(testResult.returns).toEqual([1])
|
|
125
|
-
expect(testResult.contracts[0].codeHash).toEqual(greeter.codeHash)
|
|
126
|
-
expect(testResult.contracts[0].fields.btcPrice).toEqual(1)
|
|
127
|
-
|
|
128
|
-
const signer = await testWallet(provider)
|
|
129
|
-
|
|
130
|
-
const deployTx = await greeter.transactionForDeployment(signer, {
|
|
131
|
-
initialFields: { btcPrice: 1 },
|
|
132
|
-
initialTokenAmounts: []
|
|
133
|
-
})
|
|
134
|
-
expect(deployTx.fromGroup).toEqual(0)
|
|
135
|
-
expect(deployTx.toGroup).toEqual(0)
|
|
136
|
-
const submitResult = await signer.submitTransaction(deployTx.unsignedTx, deployTx.txId)
|
|
137
|
-
expect(submitResult.fromGroup).toEqual(0)
|
|
138
|
-
expect(submitResult.toGroup).toEqual(0)
|
|
139
|
-
expect(submitResult.txId).toEqual(deployTx.txId)
|
|
140
|
-
|
|
141
|
-
const greeterContractId = deployTx.contractId
|
|
142
|
-
const main = Project.script('greeter_main.ral')
|
|
143
|
-
|
|
144
|
-
const mainScriptTx = await main.transactionForDeployment(signer, {
|
|
145
|
-
initialFields: { greeterContractId: greeterContractId }
|
|
146
|
-
})
|
|
147
|
-
expect(mainScriptTx.fromGroup).toEqual(0)
|
|
148
|
-
expect(mainScriptTx.toGroup).toEqual(0)
|
|
149
|
-
const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx, mainScriptTx.txId)
|
|
150
|
-
expect(mainSubmitResult.fromGroup).toEqual(0)
|
|
151
|
-
expect(mainSubmitResult.toGroup).toEqual(0)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
it('should test contracts', async () => {
|
|
155
|
-
await testSuite1()
|
|
156
|
-
await testSuite2()
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
function loadJson(fileName: string) {
|
|
160
|
-
const filePath = path.resolve(process.cwd() + path.sep + fileName)
|
|
161
|
-
const rawData = fs.readFileSync(filePath).toString()
|
|
162
|
-
return JSON.parse(rawData)
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function loadContract(fileName: string) {
|
|
166
|
-
Contract.fromJson(loadJson(fileName))
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function loadScript(fileName: string) {
|
|
170
|
-
Script.fromJson(loadJson(fileName))
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
it('should load source files by order', async () => {
|
|
174
|
-
const sourceFiles = await Project['loadSourceFiles']('./contracts') // `loadSourceFiles` is a private method
|
|
175
|
-
expect(sourceFiles.length).toEqual(8)
|
|
176
|
-
sourceFiles.slice(0, 5).forEach((c) => expect(c.type).toEqual(0)) // contracts
|
|
177
|
-
sourceFiles.slice(5, 7).forEach((s) => expect(s.type).toEqual(1)) // scripts
|
|
178
|
-
sourceFiles.slice(7).forEach((i) => expect(i.type).toEqual(3)) // interfaces
|
|
179
|
-
})
|
|
180
|
-
|
|
181
|
-
it('should load contract from json', async () => {
|
|
182
|
-
loadContract('./artifacts/add/add.ral.json')
|
|
183
|
-
loadContract('./artifacts/sub/sub.ral.json')
|
|
184
|
-
loadScript('./artifacts/main.ral.json')
|
|
185
|
-
|
|
186
|
-
loadContract('./artifacts/greeter/greeter.ral.json')
|
|
187
|
-
loadScript('./artifacts/greeter_main.ral.json')
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
it('should extract metadata of contracts', async () => {
|
|
191
|
-
const provider = new NodeProvider('http://127.0.0.1:22973')
|
|
192
|
-
await Project.build(provider)
|
|
193
|
-
const contract = Project.contract('test/metadata.ral', { errorOnWarnings: false })
|
|
194
|
-
expect(contract.functions.map((func) => func.name)).toEqual(['foo', 'bar', 'baz'])
|
|
195
|
-
expect(contract.publicFunctions()).toEqual(['foo'])
|
|
196
|
-
expect(contract.usingPreapprovedAssetsFunctions()).toEqual(['foo'])
|
|
197
|
-
expect(contract.usingAssetsInContractFunctions()).toEqual(['bar'])
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
it('should handle compiler warnings', async () => {
|
|
201
|
-
const provider = new NodeProvider('http://127.0.0.1:22973')
|
|
202
|
-
await Project.build(provider)
|
|
203
|
-
const contract = Project.contract('test/warnings.ral', { errorOnWarnings: false })
|
|
204
|
-
expect(contract.publicFunctions()).toEqual(['foo'])
|
|
205
|
-
|
|
206
|
-
expect(() => Project.contract('test/warnings.ral')).toThrowError(
|
|
207
|
-
'Compilation warnings:\n - Found unused variables in Warnings: foo.y\n - Found unused fields in Warnings: b'
|
|
208
|
-
)
|
|
209
|
-
expect(() => Project.contract('test/warnings.ral', { ignoreUnusedConstantsWarnings: false })).toThrowError(
|
|
210
|
-
'Compilation warnings:\n - Found unused variables in Warnings: foo.y\n - Found unused constants in Warnings: C\n - Found unused fields in Warnings: b'
|
|
211
|
-
)
|
|
212
|
-
})
|
|
213
|
-
})
|