Skip to content

Commit 098d4f4

Browse files
authored
update some test;fix issue1;fix pr ontio#7 (ontio#8)
* update some test;fix issue1;fix pr ontio#7 * update tsconfig.json for build amd file
1 parent 553f3e4 commit 098d4f4

30 files changed

Lines changed: 1179 additions & 143 deletions

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"main": "./lib/index.js",
66
"types": "./lib/types/index.d.ts",
77
"scripts": {
8-
"test": "jest",
9-
"build:dev": "webpack --display-error-details --config webpack.config.js --progress --color",
10-
"build:prod": "webpack --env.prod --config webpack.config.js --progress --color"
8+
"test": "./node_modules/.bin/jest",
9+
"build:dev": "./node_modules/.bin/webpack --display-error-details --config webpack.config.js --progress --color",
10+
"build:prod": "./node_modules/.bin/webpack --env.prod --config webpack.config.js --progress --color"
1111
},
1212
"jest": {
1313
"moduleFileExtensions": [

src/SDK/index.ts

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,33 @@ import axios from 'axios'
3232
import {BigNumber} from 'bignumber.js'
3333
import {DDO} from '../transaction/ddo';
3434
export class SDK {
35+
static SERVER_NODE : string
36+
static REST_PORT : string
37+
static SOCKET_PORT : string
38+
39+
static setServerNode(node : string) {
40+
if(node) {
41+
SDK.SERVER_NODE = node
42+
return;
43+
}
44+
throw new Error('Can not set ' + node + 'as server node')
45+
}
46+
47+
static setRestPort(port: string) {
48+
if (port) {
49+
SDK.REST_PORT = port
50+
return;
51+
}
52+
throw new Error('Can not set ' + port + ' as restful port')
53+
}
54+
55+
static setSocketPort(port: string) {
56+
if (port) {
57+
SDK.SOCKET_PORT = port
58+
return;
59+
}
60+
throw new Error('Can not set ' + port + 'as socket port')
61+
}
3562

3663
static getDecryptError(err:any) {
3764
return {
@@ -80,7 +107,8 @@ export class SDK {
80107
socket.close()
81108
}
82109
}
83-
var txSender = new TxSender(ONT_NETWORK.TEST)
110+
let socket = `ws://${SDK.SERVER_NODE}:${SDK.SOCKET_PORT}`
111+
var txSender = new TxSender(socket)
84112
txSender.sendTxWithSocket(param, socketCallback)
85113
// callback && sendBackResult2Native(JSON.stringify(obj), callback)
86114
return obj
@@ -90,10 +118,8 @@ export class SDK {
90118
password : string, callback ?: string) {
91119
let identity = new Identity()
92120
let wallet = Wallet.parseJson(walletDataStr)
93-
let privateKey = ''
94121
try {
95122
//TODO check ontid
96-
privateKey = scrypt.decrypt(encryptedPrivateKey, password)
97123
identity = Identity.importIdentity(label, encryptedPrivateKey, password)
98124
} catch (err) {
99125
let obj = this.getDecryptError(err)
@@ -109,9 +135,10 @@ export class SDK {
109135
desc : ''
110136
}
111137
//check ontid on chain
112-
let tx = buildGetDDOTx(identity.ontid, privateKey)
138+
let tx = buildGetDDOTx(identity.ontid)
113139
let param = buildRestfulParam(tx)
114-
let url = sendRawTxRestfulUrl(TEST_ONT_URL.REST_URL, true)
140+
let restUrl = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}/`
141+
let url = sendRawTxRestfulUrl(restUrl, true)
115142
axios.post(url, param).then((res:any) => {
116143
if (res.data.Result && res.data.Result.length > 0 && res.data.Result[0] !== '0000000000000000') {
117144

@@ -130,9 +157,7 @@ export class SDK {
130157
static importIdentity(label : string, encryptedPrivateKey : string, password : string, callback ?: string) {
131158
let identity = new Identity()
132159
let error = {}
133-
let privateKey
134160
try {
135-
privateKey = scrypt.decrypt(encryptedPrivateKey, password)
136161
identity = Identity.importIdentity(label, encryptedPrivateKey, password)
137162
let wallet = new Wallet()
138163
wallet.create(identity.label)
@@ -145,10 +170,11 @@ export class SDK {
145170
desc: ''
146171
}
147172
//check ontid on chain
148-
let tx = buildGetDDOTx(identity.ontid, privateKey)
173+
let tx = buildGetDDOTx(identity.ontid)
149174
let param = buildRestfulParam(tx)
150-
let url = sendRawTxRestfulUrl(TEST_ONT_URL.REST_URL, true)
151-
axios.post(url, param).then((res: any) => {
175+
let restUrl = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}/`
176+
let url = sendRawTxRestfulUrl(restUrl, true)
177+
return axios.post(url, param).then((res: any) => {
152178
if (res.data.Result && res.data.Result.length > 0 && res.data.Result[0] !== '0000000000000000') {
153179

154180
} else {
@@ -163,7 +189,7 @@ export class SDK {
163189
} catch(err) {
164190
error = this.getDecryptError(err)
165191
callback && sendBackResult2Native(JSON.stringify(error), callback)
166-
return error
192+
return Promise.reject(error)
167193
}
168194
}
169195

@@ -180,8 +206,9 @@ export class SDK {
180206
//register ontid
181207
let tx = buildRegisterOntidTx(identity.ontid, privateKey)
182208
let param = buildRestfulParam(tx)
183-
const url = TEST_ONT_URL.sendRawTxByRestful
184-
axios.post(url, param).then((res: any) => {
209+
let restUrl = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}${REST_API.sendRawTx}`
210+
211+
axios.post(restUrl, param).then((res: any) => {
185212
if(res.data.Error === 0) {
186213
callback && sendBackResult2Native(JSON.stringify(obj), callback)
187214
} else {
@@ -322,7 +349,8 @@ export class SDK {
322349
// let txId = core.getHash(tx.serialize())
323350
let param = buildTxParam(tx)
324351
//通过socket能获得推送的结果
325-
var txSender = new TxSender(ONT_NETWORK.TEST)
352+
let socket = `ws://${SDK.SERVER_NODE}:${SDK.SOCKET_PORT}`
353+
var txSender = new TxSender(socket)
326354
const socketCallback = function(res : any, socket : any) {
327355
console.log('res: '+ JSON.stringify(res))
328356
if(res.Action === 'InvokeTransaction' && res.Error === 0) {
@@ -365,7 +393,7 @@ export class SDK {
365393
if(address.length === 40) {
366394
address = core.u160ToAddress(address)
367395
}
368-
let request = `http://${TEST_NODE}:${HTTP_REST_PORT}${REST_API.getBalance}/${address}`
396+
let request = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}${REST_API.getBalance}/${address}`
369397
axios.get(request).then((res : any) => {
370398
if(res.data.Error === 0) {
371399
let result = res.data.Result
@@ -423,7 +451,7 @@ export class SDK {
423451

424452
let tx = makeTransferTransaction('ONT',from, to, value, privateKey)
425453
var param = buildRestfulParam(tx)
426-
let request = `http://${TEST_NODE}:${HTTP_REST_PORT}${REST_API.sendRawTx}`
454+
let request = `http://${SDK.SERVER_NODE}:${SDK.REST_PORT}${REST_API.sendRawTx}`
427455
axios.post(request, param).then( (res:any) => {
428456
console.log('transfer response: ' + JSON.stringify(res.data))
429457
if(res.data.Error === 0) {

src/claim.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class Claim {
6262
this.Id = CryptoJS.SHA256(CryptoJS.enc.Hex.parse(JSON.stringify(body))).toString()
6363
}
6464

65-
sign( privateKey: string ) {
65+
sign( privateKey: string ) : Signature {
6666
let claimBody = {
6767
Context: this.Context,
6868
Id: this.Id,

src/common/fixed64.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2018 The ontology Authors
3+
* This file is part of The ontology library.
4+
*
5+
* The ontology 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 ontology 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 ontology. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
20+
import {str2hexstr, hexstr2str ,StringReader} from '../utils'
21+
const Fixed64Len = 8
22+
export default class Fixed64 {
23+
//8 bytes
24+
value : string
25+
constructor() {
26+
this.value = '0000000000000000'
27+
}
28+
29+
serialize() {
30+
// return str2hexstr(this.value)
31+
return this.value
32+
}
33+
34+
static deserialize(sr:StringReader) {
35+
let f = new Fixed64()
36+
const v = sr.read(8)
37+
// f.value = hexstr2str(v)
38+
f.value = v
39+
return f
40+
}
41+
}

src/consts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Algorithm {
4242
export const DEFAULT_ALGORITHM = {
4343
algorithm : "ECDSA",
4444
parameters : {
45-
"curve": "secp256r1"
45+
"curve": "secp256r1" // also called p256
4646
}
4747
}
4848

@@ -77,7 +77,7 @@ export const TEST_ONT_URL = {
7777

7878
RPC_URL: `http://${TEST_NODE}:${HTTP_JSON_PORT}/`,
7979

80-
REST_URL: `http://${TEST_NODE}:${HTTP_REST_PORT}/`,
80+
REST_URL: `http://${TEST_NODE}:${HTTP_REST_PORT}/`,
8181

8282
sendRawTxByRestful : `http://${TEST_NODE}:${HTTP_REST_PORT}${REST_API.sendRawTx}`
8383
}

src/core.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ import * as base58 from 'bs58'
2121
import * as ecurve from 'ecurve'
2222
import * as bigInteger from 'bigi'
2323
import { ab2hexstring, hexstring2ab, StringReader, hexstr2str, num2hexstring } from './utils'
24-
import { ADDR_VERSION } from './consts'
24+
import { ADDR_VERSION, TEST_ONT_URL } from './consts'
2525
import * as scrypt from './scrypt'
2626
import {ERROR_CODE} from './error'
2727
import { VmType } from './transaction/vmcode';
28+
import { buildGetDDOTx, buildRestfulParam, sendRawTxRestfulUrl} from './transaction/transactionBuilder'
29+
import axios from 'axios'
30+
import { DDO } from './transaction/ddo'
2831

2932
var ec = require('elliptic').ec
3033
var wif = require('wif')
@@ -147,12 +150,20 @@ export function signatureData(data: string, privateKey: string): string {
147150
return signatureValue.toString('hex');
148151
}
149152

150-
export function verifySignature(data : string, signature : string, publicKey : string) {
153+
154+
/*
155+
@data original value of signature
156+
@signature
157+
@publicKey the public key of the signer. is array-like or buffer
158+
*/
159+
export function verifySignature(data: string, signature: string, publicKey: any) {
151160
let msg = cryptoJS.enc.Hex.parse(data)
152161
let msgHash = cryptoJS.SHA256(msg)
153162

154163
let elliptic = new ec('p256')
155-
const result = elliptic.verify(data.toString(), signature, publicKey, null)
164+
let r = signature.substr(0, 64)
165+
let s = signature.substr(64, 64)
166+
const result = elliptic.verify(msgHash.toString(), { r, s }, publicKey, null)
156167
return result
157168
}
158169

@@ -212,11 +223,30 @@ export function checkPrivateKey(encryptedPrivateKey : string, password : string)
212223
return true
213224
}
214225

215-
226+
/*
227+
@claim claim json object
228+
*/
216229
export function verifyOntidClaim(claim : any) {
217230
if(!claim.Metadata || !claim.Metadata.Issuer) {
218231
throw new Error('Invalid claim.')
219232
}
220-
let issuer = claim.Metadata.Issuer
221-
//TODO
233+
let issuerDid = claim.Metadata.Issuer
234+
let tx = buildGetDDOTx(issuerDid)
235+
let param = buildRestfulParam(tx)
236+
let url = sendRawTxRestfulUrl(TEST_ONT_URL.REST_URL, true)
237+
return axios.post(url, param).then( (res:any) => {
238+
if (res.data.Result && res.data.Result.length > 0) {
239+
console.log('ddo hexstr: '+ res.data.Result[0])
240+
const ddo = DDO.deserialize(res.data.Result[0])
241+
console.log('ddo: ' + JSON.stringify(ddo))
242+
if(ddo.publicKeys.length > 0) {
243+
const pk = ddo.publicKeys[0].pk
244+
const signature = claim.Signature.Value
245+
claim.delete('Signature')
246+
return verifySignature(JSON.stringify(claim), signature, hexstring2ab(pk))
247+
} else {
248+
return false
249+
}
250+
}
251+
})
222252
}

src/crypto.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,26 @@ export enum SignatureSchema {
1313

1414
SHA512withEDDSA
1515
}
16+
17+
export enum CurveLabel {
18+
P224 = 1,
19+
P256 = 2,
20+
P384 = 3,
21+
P521 = 4,
22+
23+
SM2P256V1 = 20,
24+
25+
ED25519 = 25
26+
}
27+
28+
export enum KeyType {
29+
PK_ECDSA = 0x12,
30+
PK_SM2 = 0x13,
31+
PK_EDDSA = 0x14,
32+
}
33+
34+
export class PublicKey {
35+
algorithm: number
36+
curve: number
37+
pk: string
38+
}

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import * as core from './core'
3131
import * as utils from './utils'
3232
import * as CONST from './consts'
3333
import { SDK } from './SDK/index'
34-
import { CompleterResult } from 'readline';
3534

3635
class ONT {
3736
Account : any
@@ -81,7 +80,7 @@ class ONT {
8180
this.CONST.HTTP_REST_PORT = port
8281
}
8382

84-
setWsPortyarn(port: string) {
83+
setSocketPort(port: string) {
8584
this.CONST.HTTP_WS_PORT = port
8685
}
8786
}

0 commit comments

Comments
 (0)