Skip to content

Commit

Permalink
Merge pull request #189 from MickWang/new_ontid
Browse files Browse the repository at this point in the history
ontid 2.0 api update
  • Loading branch information
MickWang authored May 27, 2020
2 parents a589b36 + 36dfba8 commit 25c0890
Show file tree
Hide file tree
Showing 9 changed files with 1,963 additions and 406 deletions.
37 changes: 29 additions & 8 deletions src/claim/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import * as b64 from 'base64-url';
import * as uuid from 'uuid';
import { PrivateKey, PublicKey, PublicKeyStatus, Signature, SignatureScheme } from '../crypto';
import RestClient from '../network/rest/restClient';
import { buildGetDocumentTx } from '../smartcontract/nativevm/ontidContractTxBuilder';
import { buildGetDDOTx, buildGetPublicKeyStateTx } from '../smartcontract/nativevm/ontidContractTxBuilder';
import { DDO } from '../transaction/ddo';
import { now, str2hexstr } from '../utils';
import { hexstr2str, now, str2hexstr } from '../utils';

/**
* Factory method type used for creating concrete instances of Message.
Expand Down Expand Up @@ -342,21 +343,41 @@ export async function retrievePublicKey(publicKeyId: string, url: string): Promi
const keyId = extractKeyId(publicKeyId);

const client = new RestClient(url);
const tx = buildGetDDOTx(ontId);
const tx = buildGetDocumentTx(ontId);
const response = await client.sendRawTransaction(tx.serialize(), true);

if (response.Result && response.Result.Result) {
const ddo = DDO.deserialize(response.Result.Result);
// const ddo = DDO.deserialize(response.Result.Result);
try {
const obj = JSON.parse(hexstr2str(response.Result.Result));
const publicKey = obj.publicKey.find((pk: any) => pk.id.split('#')[0] === ontId);

const publicKey = ddo.publicKeys.find((pk) => pk.id === keyId);
if (publicKey === undefined) {
throw new Error('Not found');
}

if (publicKey === undefined) {
throw new Error('Not found');
return new PublicKey(publicKey.pk);
} catch (err) {
throw new Error(err);
}

return publicKey.pk;
} else {
throw new Error('Not found');
const tx2 = buildGetDDOTx(ontId);
const response2 = await client.sendRawTransaction(tx2.serialize(), true);

if (response2.Result && response2.Result.Result) {
const ddo = DDO.deserialize(response2.Result.Result);

const publicKey = ddo.publicKeys.find((pk) => pk.id === keyId);

if (publicKey === undefined) {
throw new Error('Not found');
}

return publicKey.pk;
} else {
throw new Error('Not found');
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/smartcontract/abi/nativeVmParamsBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ export function createCodeParamScript(obj: any): string {
} else if (obj instanceof Address) {
result += pushHexString(obj.serialize());
} else if (obj instanceof Struct) {
result += pushInt(0);
result += num2hexstring(opcode.NEWSTRUCT);
result += num2hexstring(opcode.TOALTSTACK);
for (const v of obj.list) {
result += createCodeParamScript(v);
result += num2hexstring(opcode.DUPFROMALTSTACK);
result += num2hexstring(opcode.SWAP);
result += num2hexstring(opcode.APPEND);
}
result += num2hexstring(opcode.FROMALTSTACK);
}
return result;
}
Expand Down
37 changes: 37 additions & 0 deletions src/smartcontract/nativevm/ontid/group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as Long from 'long';
import { writeVarBytes } from '../../../transaction/scriptBuilder';
import { bigIntToBytes, str2hexstr } from '../../../utils';

type GroupType = string | Group;

export default class Group {
members: GroupType[];
/**
*
* Define the threshold number of signatures
* @type {number}
* @memberof Group
*/
threshold: number;

public constructor(members: GroupType[], threshold: number) {
this.members = members;
this.threshold = threshold;
}

public serialize(): string {
let result = '';
const length = Long.fromNumber(this.members.length);
result += writeVarBytes(bigIntToBytes(length));
for (const obj of this.members) {
if (typeof obj === 'string') {
result += writeVarBytes(str2hexstr(obj));
} else if (obj instanceof Group) {
result += writeVarBytes(obj.serialize());
}
}
const th = bigIntToBytes(Long.fromNumber(this.threshold));
result += writeVarBytes(th);
return result;
}
}
31 changes: 31 additions & 0 deletions src/smartcontract/nativevm/ontid/signer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as Long from 'long';
import { writeVarBytes } from '../../../transaction/scriptBuilder';
import { bigIntToBytes, str2hexstr } from '../../../utils';

export class Signer {
id: string;
index: number;

public constructor(id: string, index: number) {
this.id = id;
this.index = index;
}

public serialize(): string {
let result = '';
result += writeVarBytes(str2hexstr(this.id));
const bi = bigIntToBytes(Long.fromNumber(this.index));
result += writeVarBytes(bi);
return result;
}
}

export function serializeSigners(signers: Signer[]): string {
let result = '';
const length = bigIntToBytes(Long.fromNumber(signers.length));
result += writeVarBytes(length);
for (const s of signers) {
result += s.serialize();
}
return result;
}
Loading

0 comments on commit 25c0890

Please sign in to comment.