@alephium/web3 0.5.0-rc.7 → 0.5.0-rc.9
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.LICENSE.txt +2 -0
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +57 -20
- package/dist/src/api/api-alephium.js +57 -15
- package/dist/src/api/index.d.ts +1 -0
- package/dist/src/api/index.js +1 -0
- package/dist/src/contract/contract.js +6 -2
- package/dist/src/signer/signer.d.ts +29 -30
- package/dist/src/signer/signer.js +34 -25
- package/dist/src/signer/tx-builder.d.ts +2 -7
- package/dist/src/signer/tx-builder.js +10 -7
- package/dist/src/signer/types.d.ts +8 -0
- package/dist/src/transaction/sign-verify.d.ts +3 -2
- package/dist/src/transaction/sign-verify.js +4 -14
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/sign.d.ts +3 -0
- package/dist/src/utils/sign.js +89 -0
- package/dist/src/utils/utils.d.ts +4 -3
- package/dist/src/utils/utils.js +25 -10
- package/package.json +2 -1
- package/src/api/api-alephium.ts +88 -32
- package/src/api/index.ts +2 -0
- package/src/contract/contract.ts +7 -3
- package/src/signer/signer.ts +69 -55
- package/src/signer/tx-builder.ts +13 -7
- package/src/signer/types.ts +10 -2
- package/src/transaction/sign-verify.ts +10 -15
- package/src/utils/index.ts +1 -0
- package/src/utils/sign.ts +66 -0
- package/src/utils/utils.ts +26 -10
package/src/signer/tx-builder.ts
CHANGED
|
@@ -21,6 +21,7 @@ import { fromApiNumber256, node, NodeProvider, toApiNumber256Optional, toApiToke
|
|
|
21
21
|
import { addressFromPublicKey } from '../utils'
|
|
22
22
|
import { toApiDestinations } from './signer'
|
|
23
23
|
import {
|
|
24
|
+
KeyType,
|
|
24
25
|
SignDeployContractTxParams,
|
|
25
26
|
SignDeployContractTxResult,
|
|
26
27
|
SignerAddress,
|
|
@@ -35,8 +36,10 @@ import {
|
|
|
35
36
|
export abstract class TransactionBuilder {
|
|
36
37
|
abstract get nodeProvider(): NodeProvider
|
|
37
38
|
|
|
38
|
-
static
|
|
39
|
-
|
|
39
|
+
static from(nodeProvider: NodeProvider): TransactionBuilder
|
|
40
|
+
static from(baseUrl: string, apiKey?: string): TransactionBuilder
|
|
41
|
+
static from(param0: string | NodeProvider, param1?: string): TransactionBuilder {
|
|
42
|
+
const nodeProvider = typeof param0 === 'string' ? new NodeProvider(param0, param1) : (param0 as NodeProvider)
|
|
40
43
|
return new (class extends TransactionBuilder {
|
|
41
44
|
get nodeProvider(): NodeProvider {
|
|
42
45
|
return nodeProvider
|
|
@@ -44,8 +47,8 @@ export abstract class TransactionBuilder {
|
|
|
44
47
|
})()
|
|
45
48
|
}
|
|
46
49
|
|
|
47
|
-
private static validatePublicKey(params: SignerAddress, publicKey: string) {
|
|
48
|
-
const address = addressFromPublicKey(publicKey)
|
|
50
|
+
private static validatePublicKey(params: SignerAddress, publicKey: string, keyType?: KeyType) {
|
|
51
|
+
const address = addressFromPublicKey(publicKey, keyType)
|
|
49
52
|
if (address !== params.signerAddress) {
|
|
50
53
|
throw new Error('Unmatched public key')
|
|
51
54
|
}
|
|
@@ -55,11 +58,12 @@ export abstract class TransactionBuilder {
|
|
|
55
58
|
params: SignTransferTxParams,
|
|
56
59
|
publicKey: string
|
|
57
60
|
): Promise<Omit<SignTransferTxResult, 'signature'>> {
|
|
58
|
-
TransactionBuilder.validatePublicKey(params, publicKey)
|
|
61
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType)
|
|
59
62
|
|
|
60
63
|
const { destinations, gasPrice, ...rest } = params
|
|
61
64
|
const data: node.BuildTransaction = {
|
|
62
65
|
fromPublicKey: publicKey,
|
|
66
|
+
fromPublicKeyType: params.signerKeyType,
|
|
63
67
|
destinations: toApiDestinations(destinations),
|
|
64
68
|
gasPrice: toApiNumber256Optional(gasPrice),
|
|
65
69
|
...rest
|
|
@@ -72,11 +76,12 @@ export abstract class TransactionBuilder {
|
|
|
72
76
|
params: SignDeployContractTxParams,
|
|
73
77
|
publicKey: string
|
|
74
78
|
): Promise<Omit<SignDeployContractTxResult, 'signature'>> {
|
|
75
|
-
TransactionBuilder.validatePublicKey(params, publicKey)
|
|
79
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType)
|
|
76
80
|
|
|
77
81
|
const { initialAttoAlphAmount, initialTokenAmounts, issueTokenAmount, gasPrice, ...rest } = params
|
|
78
82
|
const data: node.BuildDeployContractTx = {
|
|
79
83
|
fromPublicKey: publicKey,
|
|
84
|
+
fromPublicKeyType: params.signerKeyType,
|
|
80
85
|
initialAttoAlphAmount: toApiNumber256Optional(initialAttoAlphAmount),
|
|
81
86
|
initialTokenAmounts: toApiTokens(initialTokenAmounts),
|
|
82
87
|
issueTokenAmount: toApiNumber256Optional(issueTokenAmount),
|
|
@@ -92,11 +97,12 @@ export abstract class TransactionBuilder {
|
|
|
92
97
|
params: SignExecuteScriptTxParams,
|
|
93
98
|
publicKey: string
|
|
94
99
|
): Promise<Omit<SignExecuteScriptTxResult, 'signature'>> {
|
|
95
|
-
TransactionBuilder.validatePublicKey(params, publicKey)
|
|
100
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType)
|
|
96
101
|
|
|
97
102
|
const { attoAlphAmount, tokens, gasPrice, ...rest } = params
|
|
98
103
|
const data: node.BuildExecuteScriptTx = {
|
|
99
104
|
fromPublicKey: publicKey,
|
|
105
|
+
fromPublicKeyType: params.signerKeyType,
|
|
100
106
|
attoAlphAmount: toApiNumber256Optional(attoAlphAmount),
|
|
101
107
|
tokens: toApiTokens(tokens),
|
|
102
108
|
gasPrice: toApiNumber256Optional(gasPrice),
|
package/src/signer/types.ts
CHANGED
|
@@ -33,18 +33,22 @@ export interface Destination {
|
|
|
33
33
|
}
|
|
34
34
|
assertType<Eq<keyof Destination, keyof node.Destination>>
|
|
35
35
|
|
|
36
|
+
export type KeyType = 'default' | 'bip340-schnorr'
|
|
37
|
+
|
|
36
38
|
export interface Account {
|
|
39
|
+
keyType: KeyType
|
|
37
40
|
address: string
|
|
38
41
|
group: number
|
|
39
42
|
publicKey: string
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
export type SignerAddress = { signerAddress: string }
|
|
43
|
-
type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'targetBlockHash'> & SignerAddress
|
|
45
|
+
export type SignerAddress = { signerAddress: string; signerKeyType?: KeyType }
|
|
46
|
+
type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'fromPublicKeyType' | 'targetBlockHash'> & SignerAddress
|
|
44
47
|
type SignResult<T> = Omit<T, 'gasPrice'> & { signature: string; gasPrice: Number256 }
|
|
45
48
|
|
|
46
49
|
export interface SignTransferTxParams {
|
|
47
50
|
signerAddress: string
|
|
51
|
+
signerKeyType?: KeyType
|
|
48
52
|
destinations: Destination[]
|
|
49
53
|
utxos?: OutputRef[]
|
|
50
54
|
gasAmount?: number
|
|
@@ -64,6 +68,7 @@ assertType<Eq<SignTransferTxResult, SignResult<node.BuildTransactionResult>>>()
|
|
|
64
68
|
|
|
65
69
|
export interface SignDeployContractTxParams {
|
|
66
70
|
signerAddress: string
|
|
71
|
+
signerKeyType?: KeyType
|
|
67
72
|
bytecode: string
|
|
68
73
|
initialAttoAlphAmount?: Number256
|
|
69
74
|
initialTokenAmounts?: Token[]
|
|
@@ -91,6 +96,7 @@ assertType<
|
|
|
91
96
|
|
|
92
97
|
export interface SignExecuteScriptTxParams {
|
|
93
98
|
signerAddress: string
|
|
99
|
+
signerKeyType?: KeyType
|
|
94
100
|
bytecode: string
|
|
95
101
|
attoAlphAmount?: Number256
|
|
96
102
|
tokens?: Token[]
|
|
@@ -115,6 +121,7 @@ assertType<
|
|
|
115
121
|
|
|
116
122
|
export interface SignUnsignedTxParams {
|
|
117
123
|
signerAddress: string
|
|
124
|
+
signerKeyType?: KeyType
|
|
118
125
|
unsignedTx: string
|
|
119
126
|
}
|
|
120
127
|
assertType<Eq<SignUnsignedTxParams, { unsignedTx: string } & SignerAddress>>()
|
|
@@ -131,6 +138,7 @@ assertType<Eq<SignUnsignedTxResult, SignTransferTxResult>>
|
|
|
131
138
|
|
|
132
139
|
export interface SignMessageParams {
|
|
133
140
|
signerAddress: string
|
|
141
|
+
signerKeyType?: KeyType
|
|
134
142
|
message: string
|
|
135
143
|
}
|
|
136
144
|
assertType<Eq<SignMessageParams, { message: string } & SignerAddress>>()
|
|
@@ -17,22 +17,17 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import * as utils from '../utils'
|
|
20
|
-
import {
|
|
20
|
+
import { KeyType } from '../signer'
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export function transactionSign(txHash: string, privateKey: string): string {
|
|
25
|
-
const keyPair = ec.keyFromPrivate(privateKey)
|
|
26
|
-
const signature = keyPair.sign(txHash)
|
|
27
|
-
|
|
28
|
-
return utils.encodeSignature(signature)
|
|
22
|
+
export function transactionSign(txId: string, privateKey: string, keyType?: KeyType): string {
|
|
23
|
+
return utils.sign(txId, privateKey, keyType)
|
|
29
24
|
}
|
|
30
25
|
|
|
31
|
-
export function transactionVerifySignature(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
26
|
+
export function transactionVerifySignature(
|
|
27
|
+
txId: string,
|
|
28
|
+
publicKey: string,
|
|
29
|
+
signature: string,
|
|
30
|
+
keyType?: KeyType
|
|
31
|
+
): boolean {
|
|
32
|
+
return utils.verifySignature(txId, publicKey, signature, keyType)
|
|
38
33
|
}
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
import { binToHex, encodeSignature, hexToBinUnsafe, signatureDecode } from '..'
|
|
21
|
+
import { KeyType } from '../signer'
|
|
22
|
+
import * as necc from '@noble/secp256k1'
|
|
23
|
+
import { createHash, createHmac } from 'crypto'
|
|
24
|
+
|
|
25
|
+
const ec = new EC('secp256k1')
|
|
26
|
+
|
|
27
|
+
necc.utils.sha256Sync = (...messages: Uint8Array[]): Uint8Array => {
|
|
28
|
+
const sha256 = createHash('sha256')
|
|
29
|
+
for (const message of messages) sha256.update(message)
|
|
30
|
+
return sha256.digest()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
necc.utils.hmacSha256Sync = (key: Uint8Array, ...messages: Uint8Array[]): Uint8Array => {
|
|
34
|
+
const hash = createHmac('sha256', Buffer.from(key))
|
|
35
|
+
messages.forEach((m) => hash.update(m))
|
|
36
|
+
return Uint8Array.from(hash.digest())
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// hash has to be 32 bytes
|
|
40
|
+
export function sign(hash: string, privateKey: string, _keyType?: KeyType): string {
|
|
41
|
+
const keyType = _keyType ?? 'default'
|
|
42
|
+
|
|
43
|
+
if (keyType === 'default') {
|
|
44
|
+
const key = ec.keyFromPrivate(privateKey)
|
|
45
|
+
const signature = key.sign(hash)
|
|
46
|
+
return encodeSignature(signature)
|
|
47
|
+
} else {
|
|
48
|
+
const signature = necc.schnorr.signSync(hexToBinUnsafe(hash), hexToBinUnsafe(privateKey))
|
|
49
|
+
return binToHex(signature)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function verifySignature(hash: string, publicKey: string, signature: string, _keyType?: KeyType): boolean {
|
|
54
|
+
const keyType = _keyType ?? 'default'
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
if (keyType === 'default') {
|
|
58
|
+
const key = ec.keyFromPublic(publicKey, 'hex')
|
|
59
|
+
return key.verify(hash, signatureDecode(ec, signature))
|
|
60
|
+
} else {
|
|
61
|
+
return necc.schnorr.verifySync(hexToBinUnsafe(signature), hexToBinUnsafe(hash), hexToBinUnsafe(publicKey))
|
|
62
|
+
}
|
|
63
|
+
} catch (error) {
|
|
64
|
+
return false
|
|
65
|
+
}
|
|
66
|
+
}
|
package/src/utils/utils.ts
CHANGED
|
@@ -24,6 +24,7 @@ import { Buffer } from 'buffer/'
|
|
|
24
24
|
|
|
25
25
|
import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
|
|
26
26
|
import djb2 from './djb2'
|
|
27
|
+
import { KeyType } from '../signer'
|
|
27
28
|
|
|
28
29
|
const ec = new EC('secp256k1')
|
|
29
30
|
|
|
@@ -157,20 +158,35 @@ export function binToHex(bin: Uint8Array): string {
|
|
|
157
158
|
return Buffer.from(bin).toString('hex')
|
|
158
159
|
}
|
|
159
160
|
|
|
160
|
-
export function groupOfPrivateKey(privateKey: string): number {
|
|
161
|
-
return groupOfAddress(addressFromPublicKey(publicKeyFromPrivateKey(privateKey)))
|
|
161
|
+
export function groupOfPrivateKey(privateKey: string, keyType?: KeyType): number {
|
|
162
|
+
return groupOfAddress(addressFromPublicKey(publicKeyFromPrivateKey(privateKey, keyType), keyType))
|
|
162
163
|
}
|
|
163
164
|
|
|
164
|
-
export function publicKeyFromPrivateKey(privateKey: string): string {
|
|
165
|
-
const
|
|
166
|
-
|
|
165
|
+
export function publicKeyFromPrivateKey(privateKey: string, _keyType?: KeyType): string {
|
|
166
|
+
const keyType = _keyType ?? 'default'
|
|
167
|
+
|
|
168
|
+
if (keyType === 'default') {
|
|
169
|
+
const key = ec.keyFromPrivate(privateKey)
|
|
170
|
+
return key.getPublic(true, 'hex')
|
|
171
|
+
} else {
|
|
172
|
+
return ec.g.mul(new BN(privateKey, 16)).encode('hex', true).slice(2)
|
|
173
|
+
}
|
|
167
174
|
}
|
|
168
175
|
|
|
169
|
-
export function addressFromPublicKey(publicKey: string): string {
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
176
|
+
export function addressFromPublicKey(publicKey: string, _keyType?: KeyType): string {
|
|
177
|
+
const keyType = _keyType ?? 'default'
|
|
178
|
+
|
|
179
|
+
if (keyType === 'default') {
|
|
180
|
+
const addressType = Buffer.from([AddressType.P2PKH])
|
|
181
|
+
const hash = Buffer.from(blake.blake2b(Buffer.from(publicKey, 'hex'), undefined, 32))
|
|
182
|
+
const bytes = Buffer.concat([addressType, hash])
|
|
183
|
+
return bs58.encode(bytes)
|
|
184
|
+
} else {
|
|
185
|
+
const lockupScript = Buffer.from(`0101000000000458144020${publicKey}8685`, 'hex')
|
|
186
|
+
const lockupScriptHash = blake.blake2b(lockupScript, undefined, 32)
|
|
187
|
+
const addressType = Buffer.from([AddressType.P2SH])
|
|
188
|
+
return bs58.encode(Buffer.concat([addressType, lockupScriptHash]))
|
|
189
|
+
}
|
|
174
190
|
}
|
|
175
191
|
|
|
176
192
|
export function addressFromContractId(contractId: string): string {
|