@alephium/web3 0.0.3 → 0.1.0-rc.2
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/.editorconfig +13 -0
- package/.eslintignore +2 -0
- package/.eslintrc.json +21 -0
- package/README.md +2 -2
- package/contracts/add.ral +7 -3
- package/contracts/greeter.ral +3 -1
- package/contracts/greeter_interface.ral +3 -0
- package/contracts/greeter_main.ral +9 -0
- package/contracts/main.ral +3 -5
- package/dev/user.conf +8 -4
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/scripts/check-versions.d.ts +1 -0
- package/dist/scripts/check-versions.js +39 -0
- package/dist/{cli → scripts}/create-project.d.ts +0 -0
- package/dist/scripts/create-project.js +124 -0
- package/dist/scripts/header.d.ts +0 -0
- package/dist/scripts/header.js +18 -0
- package/dist/scripts/rename-gitignore.d.ts +1 -0
- package/dist/scripts/rename-gitignore.js +24 -0
- package/dist/scripts/start-devnet.d.ts +1 -0
- package/dist/scripts/start-devnet.js +131 -0
- package/dist/scripts/stop-devnet.d.ts +1 -0
- package/dist/scripts/stop-devnet.js +32 -0
- package/dist/{api → src/api}/api-alephium.d.ts +287 -168
- package/dist/{api → src/api}/api-alephium.js +497 -115
- package/dist/{api → src/api}/api-explorer.d.ts +117 -19
- package/dist/{api → src/api}/api-explorer.js +206 -46
- package/dist/src/api/index.d.ts +10 -0
- package/dist/src/api/index.js +55 -0
- package/dist/{lib → src}/constants.d.ts +0 -0
- package/dist/{lib → src}/constants.js +0 -0
- package/dist/src/contract/contract.d.ts +182 -0
- package/dist/src/contract/contract.js +760 -0
- package/dist/src/contract/events.d.ts +29 -0
- package/dist/src/contract/events.js +77 -0
- package/dist/src/contract/index.d.ts +3 -0
- package/dist/src/contract/index.js +32 -0
- package/dist/src/contract/ralph.d.ts +12 -0
- package/dist/src/contract/ralph.js +362 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +34 -0
- package/dist/src/signer/index.d.ts +2 -0
- package/dist/src/signer/index.js +31 -0
- package/dist/src/signer/node-wallet.d.ts +13 -0
- package/dist/src/signer/node-wallet.js +60 -0
- package/dist/src/signer/signer.d.ts +143 -0
- package/dist/src/signer/signer.js +184 -0
- package/dist/src/test/index.d.ts +7 -0
- package/dist/src/test/index.js +41 -0
- package/dist/src/test/privatekey-wallet.d.ts +12 -0
- package/dist/src/test/privatekey-wallet.js +68 -0
- package/dist/{lib → src/utils}/address.d.ts +0 -0
- package/dist/{lib → src/utils}/address.js +1 -1
- package/dist/{lib → src/utils}/bs58.d.ts +2 -2
- package/dist/{lib → src/utils}/bs58.js +3 -1
- package/dist/{lib → src/utils}/djb2.d.ts +0 -0
- package/dist/{lib → src/utils}/djb2.js +1 -1
- package/dist/src/utils/index.d.ts +6 -0
- package/dist/src/utils/index.js +35 -0
- package/dist/{lib → src/utils}/password-crypto.d.ts +0 -0
- package/dist/{lib → src/utils}/password-crypto.js +8 -7
- package/dist/src/utils/transaction.d.ts +2 -0
- package/dist/src/utils/transaction.js +58 -0
- package/dist/src/utils/utils.d.ts +30 -0
- package/dist/{lib → src/utils}/utils.js +83 -19
- package/gitignore +5 -4
- package/package.json +55 -41
- package/scripts/create-project.ts +136 -0
- package/scripts/header.js +17 -0
- package/scripts/start-devnet.js +3 -3
- package/src/api/api-alephium.ts +2323 -0
- package/src/api/api-explorer.ts +808 -0
- package/src/api/index.ts +35 -0
- package/src/constants.ts +20 -0
- package/src/contract/contract.ts +1014 -0
- package/src/contract/events.ts +102 -0
- package/src/contract/index.ts +21 -0
- package/src/contract/ralph.test.ts +178 -0
- package/src/contract/ralph.ts +362 -0
- package/src/fixtures/address.json +36 -0
- package/src/fixtures/balance.json +9 -0
- package/src/fixtures/self-clique.json +19 -0
- package/src/fixtures/transaction.json +13 -0
- package/src/fixtures/transactions.json +179 -0
- package/src/index.ts +24 -0
- package/src/signer/fixtures/genesis.json +26 -0
- package/src/signer/fixtures/wallets.json +26 -0
- package/src/signer/index.ts +20 -0
- package/src/signer/node-wallet.ts +74 -0
- package/src/signer/signer.ts +313 -0
- package/src/test/index.ts +32 -0
- package/src/test/privatekey-wallet.ts +58 -0
- package/src/utils/address.test.ts +47 -0
- package/src/utils/address.ts +39 -0
- package/src/utils/bs58.ts +26 -0
- package/src/utils/djb2.test.ts +35 -0
- package/src/utils/djb2.ts +25 -0
- package/src/utils/index.ts +24 -0
- package/src/utils/password-crypto.test.ts +27 -0
- package/src/utils/password-crypto.ts +77 -0
- package/src/utils/transaction.test.ts +50 -0
- package/src/utils/transaction.ts +39 -0
- package/src/utils/utils.test.ts +160 -0
- package/src/utils/utils.ts +209 -0
- package/templates/{README.md → base/README.md} +4 -4
- package/templates/base/package.json +35 -0
- package/templates/base/src/greeter.ts +41 -0
- package/templates/base/tsconfig.json +19 -0
- package/templates/react/README.md +34 -0
- package/templates/react/config-overrides.js +18 -0
- package/templates/react/package.json +66 -0
- package/templates/react/src/App.tsx +42 -0
- package/templates/react/src/artifacts/greeter.ral.json +26 -0
- package/templates/react/src/artifacts/greeter_main.ral.json +22 -0
- package/templates/shared/.eslintrc.json +12 -0
- package/templates/shared/scripts/header.js +0 -0
- package/test/contract.test.ts +161 -0
- package/test/events.test.ts +139 -0
- package/webpack.config.js +1 -1
- package/contracts/greeter-main.ral +0 -8
- package/dist/cli/create-project.js +0 -87
- package/dist/lib/clique.d.ts +0 -23
- package/dist/lib/clique.js +0 -149
- package/dist/lib/contract.d.ts +0 -152
- package/dist/lib/contract.js +0 -711
- package/dist/lib/explorer.d.ts +0 -8
- package/dist/lib/explorer.js +0 -46
- package/dist/lib/index.d.ts +0 -12
- package/dist/lib/index.js +0 -60
- package/dist/lib/node.d.ts +0 -10
- package/dist/lib/node.js +0 -64
- package/dist/lib/numbers.d.ts +0 -7
- package/dist/lib/numbers.js +0 -128
- package/dist/lib/signer.d.ts +0 -17
- package/dist/lib/signer.js +0 -70
- package/dist/lib/storage-browser.d.ts +0 -9
- package/dist/lib/storage-browser.js +0 -52
- package/dist/lib/storage-node.d.ts +0 -9
- package/dist/lib/storage-node.js +0 -65
- package/dist/lib/utils.d.ts +0 -11
- package/dist/lib/wallet.d.ts +0 -30
- package/dist/lib/wallet.js +0 -142
- package/templates/package.json +0 -29
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
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 './transaction'
|
|
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
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
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('293c948c81c5a9495178fff8b8c9f29f4f3e09e1728c40e9253c87adf5e18770')
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('should convert from string to hex', () => {
|
|
158
|
+
expect(utils.stringToHex('Hello Alephium!')).toBe('48656c6c6f20416c65706869756d21')
|
|
159
|
+
})
|
|
160
|
+
})
|
|
@@ -0,0 +1,209 @@
|
|
|
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, SignatureInput } from 'elliptic'
|
|
20
|
+
import BN from 'bn.js'
|
|
21
|
+
import blake from 'blakejs'
|
|
22
|
+
import bs58 from './bs58'
|
|
23
|
+
import { Buffer } from 'buffer/'
|
|
24
|
+
|
|
25
|
+
import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
|
|
26
|
+
import djb2 from './djb2'
|
|
27
|
+
import * as node from '../api/api-alephium'
|
|
28
|
+
import * as explorer from '../api/api-explorer'
|
|
29
|
+
|
|
30
|
+
const ec = new EC('secp256k1')
|
|
31
|
+
|
|
32
|
+
export function convertHttpResponse<T>(
|
|
33
|
+
response: node.HttpResponse<T, { detail: string }> | explorer.HttpResponse<T, { detail: string }>
|
|
34
|
+
): T {
|
|
35
|
+
if (response.error) {
|
|
36
|
+
throw new Error(response.error.detail)
|
|
37
|
+
} else {
|
|
38
|
+
return response.data
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function signatureEncode(signature: EC.Signature): string {
|
|
43
|
+
let sNormalized = signature.s
|
|
44
|
+
if (ec.n && signature.s.cmp(ec.nh) === 1) {
|
|
45
|
+
sNormalized = ec.n.sub(signature.s)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const r = signature.r.toString('hex', 66).slice(2)
|
|
49
|
+
const s = sNormalized.toString('hex', 66).slice(2)
|
|
50
|
+
return r + s
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// the signature should be in hex string format for 64 bytes
|
|
54
|
+
export const signatureDecode = (ec: EC, signature: string): SignatureInput => {
|
|
55
|
+
if (signature.length !== 128) {
|
|
56
|
+
throw new Error('Invalid signature length')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const sHex = signature.slice(64, 128)
|
|
60
|
+
const s = new BN(sHex, 'hex')
|
|
61
|
+
if (ec.n && s.cmp(ec.nh) < 1) {
|
|
62
|
+
const decoded = { r: signature.slice(0, 64), s: sHex }
|
|
63
|
+
return decoded
|
|
64
|
+
} else {
|
|
65
|
+
throw new Error('The signature is not normalized')
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const xorByte = (intValue: number) => {
|
|
70
|
+
const byte0 = (intValue >> 24) & 0xff
|
|
71
|
+
const byte1 = (intValue >> 16) & 0xff
|
|
72
|
+
const byte2 = (intValue >> 8) & 0xff
|
|
73
|
+
const byte3 = intValue & 0xff
|
|
74
|
+
return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function isHexString(input: string): boolean {
|
|
78
|
+
return input.length % 2 === 0 && /[0-9a-f]*$/.test(input)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
enum AddressType {
|
|
82
|
+
P2PKH = 0x00,
|
|
83
|
+
P2MPKH = 0x01,
|
|
84
|
+
P2SH = 0x02,
|
|
85
|
+
P2C = 0x03
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const groupOfAddress = (address: string): number => {
|
|
89
|
+
const decoded = bs58.decode(address)
|
|
90
|
+
|
|
91
|
+
if (decoded.length == 0) throw new Error('Address string is empty')
|
|
92
|
+
const addressType = decoded[0]
|
|
93
|
+
const addressBody = decoded.slice(1)
|
|
94
|
+
|
|
95
|
+
if (addressType == AddressType.P2PKH) {
|
|
96
|
+
return groupOfP2pkhAddress(addressBody)
|
|
97
|
+
} else if (addressType == AddressType.P2MPKH) {
|
|
98
|
+
return groupOfP2mpkhAddress(addressBody)
|
|
99
|
+
} else if (addressType == AddressType.P2SH) {
|
|
100
|
+
return groupOfP2shAddress(addressBody)
|
|
101
|
+
} else {
|
|
102
|
+
throw new Error(`Invalid asset address type: ${addressType}`)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const groupOfAddressBytes = (bytes: Uint8Array): number => {
|
|
107
|
+
const hint = djb2(bytes) | 1
|
|
108
|
+
const hash = xorByte(hint)
|
|
109
|
+
const group = hash % TOTAL_NUMBER_OF_GROUPS
|
|
110
|
+
return group
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Pay to public key hash address
|
|
114
|
+
const groupOfP2pkhAddress = (address: Uint8Array): number => {
|
|
115
|
+
if (address.length != 32) {
|
|
116
|
+
throw new Error(`Invalid p2pkh address length: ${address.length}`)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return groupOfAddressBytes(address)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Pay to multiple public key hash address
|
|
123
|
+
const groupOfP2mpkhAddress = (address: Uint8Array): number => {
|
|
124
|
+
if ((address.length - 2) % 32 != 0) {
|
|
125
|
+
throw new Error(`Invalid p2mpkh address length: ${address.length}`)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return groupOfAddressBytes(address.slice(1, 33))
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Pay to script hash address
|
|
132
|
+
const groupOfP2shAddress = (address: Uint8Array): number => {
|
|
133
|
+
return groupOfAddressBytes(address)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function contractIdFromAddress(address: string): Uint8Array {
|
|
137
|
+
return idFromAddress(address)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function tokenIdFromAddress(address: string): Uint8Array {
|
|
141
|
+
return idFromAddress(address)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function idFromAddress(address: string): Uint8Array {
|
|
145
|
+
const decoded = bs58.decode(address)
|
|
146
|
+
|
|
147
|
+
if (decoded.length == 0) throw new Error('Address string is empty')
|
|
148
|
+
const addressType = decoded[0]
|
|
149
|
+
const addressBody = decoded.slice(1)
|
|
150
|
+
|
|
151
|
+
if (addressType == AddressType.P2C) {
|
|
152
|
+
return addressBody
|
|
153
|
+
} else {
|
|
154
|
+
throw new Error(`Invalid contract address type: ${addressType}`)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function hexToBinUnsafe(hex: string): Uint8Array {
|
|
159
|
+
return Buffer.from(hex, 'hex')
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function binToHex(bin: Uint8Array): string {
|
|
163
|
+
return Buffer.from(bin).toString('hex')
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function publicKeyFromPrivateKey(privateKey: string): string {
|
|
167
|
+
const key = ec.keyFromPrivate(privateKey)
|
|
168
|
+
return key.getPublic(true, 'hex')
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function addressFromPublicKey(publicKey: string): string {
|
|
172
|
+
const addressType = Buffer.from([AddressType.P2PKH])
|
|
173
|
+
const hash = Buffer.from(blake.blake2b(Buffer.from(publicKey, 'hex'), undefined, 32))
|
|
174
|
+
const bytes = Buffer.concat([addressType, hash])
|
|
175
|
+
return bs58.encode(bytes)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function addressFromContractId(contractId: string): string {
|
|
179
|
+
const addressType = Buffer.from([AddressType.P2C])
|
|
180
|
+
const hash = Buffer.from(hexToBinUnsafe(contractId))
|
|
181
|
+
const bytes = Buffer.concat([addressType, hash])
|
|
182
|
+
return bs58.encode(bytes)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function contractIdFromTx(txId: string, outputIndex: number): string {
|
|
186
|
+
const txIdBin = hexToBinUnsafe(txId)
|
|
187
|
+
const data = Buffer.concat([txIdBin, Buffer.from([outputIndex])])
|
|
188
|
+
const hash = blake.blake2b(data, undefined, 32)
|
|
189
|
+
return binToHex(hash)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function subContractId(parentContractId: string, pathInHex: string): string {
|
|
193
|
+
const data = Buffer.concat([hexToBinUnsafe(pathInHex), hexToBinUnsafe(parentContractId)])
|
|
194
|
+
|
|
195
|
+
return binToHex(blake.blake2b(blake.blake2b(data, undefined, 32), undefined, 32))
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function stringToHex(str: string): string {
|
|
199
|
+
let hex = ''
|
|
200
|
+
for (let i = 0; i < str.length; i++) {
|
|
201
|
+
hex += '' + str.charCodeAt(i).toString(16)
|
|
202
|
+
}
|
|
203
|
+
return hex
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false
|
|
207
|
+
export type Eq<X, Y> = _Eq<{ [P in keyof X]: X[P] }, { [P in keyof Y]: Y[P] }>
|
|
208
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
|
|
209
|
+
export function assertType<T extends true>(): void {}
|
|
@@ -11,20 +11,20 @@ npm install
|
|
|
11
11
|
Compile the TypeScript files into JavaScript:
|
|
12
12
|
|
|
13
13
|
```
|
|
14
|
-
npm run
|
|
14
|
+
npm run build
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
## Start a devnet
|
|
18
18
|
|
|
19
19
|
```
|
|
20
|
-
npm run devnet
|
|
20
|
+
npm run start-devnet // this will start a devnet for smart contract tests
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
## Stop/restart devnet
|
|
24
24
|
|
|
25
25
|
```
|
|
26
|
-
npm run devnet
|
|
27
|
-
npm run devnet
|
|
26
|
+
npm run stop-devnet
|
|
27
|
+
npm run restart-devnet
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
## Testing
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "my-dapp-template",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "GPL",
|
|
5
|
+
"config": {
|
|
6
|
+
"alephium_version": "1.4.0-rc2"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "rm -rf dist && npx tsc --build .",
|
|
10
|
+
"start-devnet": "node scripts/start-devnet.js ${npm_package_config_alephium_version}",
|
|
11
|
+
"restart-devnet": "npm run start-devnet",
|
|
12
|
+
"stop-devnet": "node scripts/stop-devnet.js"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@alephium/web3": "0.1.0-rc.2"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/elliptic": "^6.4.13",
|
|
19
|
+
"@types/node": "^16.7.8",
|
|
20
|
+
"typescript": "^4.4.2"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=14.0.0",
|
|
24
|
+
"npm": ">=7.0.0"
|
|
25
|
+
},
|
|
26
|
+
"prettier": {
|
|
27
|
+
"printWidth": 120,
|
|
28
|
+
"tabWidth": 2,
|
|
29
|
+
"useTabs": false,
|
|
30
|
+
"semi": false,
|
|
31
|
+
"singleQuote": true,
|
|
32
|
+
"bracketSameLine": false,
|
|
33
|
+
"trailingComma": "none"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* greeter.ts
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { NodeProvider, Contract, Script, TestContractParams } from '@alephium/web3'
|
|
6
|
+
import { testWallet } from '@alephium/web3/test'
|
|
7
|
+
|
|
8
|
+
async function greet() {
|
|
9
|
+
const provider = new NodeProvider('http://127.0.0.1:22973')
|
|
10
|
+
|
|
11
|
+
const greeter = await Contract.fromSource(provider, 'greeter.ral')
|
|
12
|
+
|
|
13
|
+
const testParams: TestContractParams = {
|
|
14
|
+
initialFields: { btcPrice: 1 }
|
|
15
|
+
}
|
|
16
|
+
const testResult = await greeter.testPublicMethod(provider, 'greet', testParams)
|
|
17
|
+
console.log(testResult)
|
|
18
|
+
|
|
19
|
+
const signer = await testWallet(provider)
|
|
20
|
+
|
|
21
|
+
const deployTx = await greeter.transactionForDeployment(signer, { initialFields: testParams.initialFields })
|
|
22
|
+
const greeterContractId = deployTx.contractId
|
|
23
|
+
console.log(deployTx.fromGroup)
|
|
24
|
+
console.log(deployTx.toGroup)
|
|
25
|
+
|
|
26
|
+
const submitResult = await signer.submitTransaction(deployTx.unsignedTx, deployTx.txId)
|
|
27
|
+
console.log(submitResult)
|
|
28
|
+
|
|
29
|
+
const main = await Script.fromSource(provider, 'greeter_main.ral')
|
|
30
|
+
|
|
31
|
+
const mainScriptTx = await main.transactionForDeployment(signer, {
|
|
32
|
+
initialFields: { greeterContractId: greeterContractId }
|
|
33
|
+
})
|
|
34
|
+
console.log(mainScriptTx.fromGroup)
|
|
35
|
+
console.log(mainScriptTx.toGroup)
|
|
36
|
+
|
|
37
|
+
const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx, mainScriptTx.txId)
|
|
38
|
+
console.log(mainSubmitResult)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
greet()
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "dist/",
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"target": "ES2015",
|
|
7
|
+
"lib": ["dom", "es2016"],
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"resolveJsonModule": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"inlineSourceMap": true,
|
|
12
|
+
"inlineSources": true,
|
|
13
|
+
"declarationMap": true,
|
|
14
|
+
"noFallthroughCasesInSwitch": true,
|
|
15
|
+
"strictFunctionTypes": true,
|
|
16
|
+
"esModuleInterop": true
|
|
17
|
+
},
|
|
18
|
+
"exclude": ["node_modules", "templates"]
|
|
19
|
+
}
|