Skip to content

Instantly share code, notes, and snippets.

@ender503

ender503/main.js

Last active Apr 5, 2020
Embed
What would you like to do?
TangleID Javascript Example
/**
* Versions
* node.js: v10.16.0
* @tangleid/core package: ^1.2.0
*/
const { composeAPI } = require('@tangleid/core');
const publicKeyPem = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAol+0LrzQ4+Z128AsTIsE
k+7+NRttp97tYP5jF1oqMShYfuqmYkde2p2V99Wp4c1/m0SX31t6/XkokXAClC/F
3LDQj4cYkR16wW/ff5hi4J19KxQN35ANeRMcNLphCjZqTm6YmCOVfptjIIM3a7fx
Y+cVacnjNMf9KKmQYaq6dUNv4aOM6m+7kyVZlCf+ZoQWQ3290vXi70Mq9RHQeoKL
2lW1acAKfhGkolvyAJmPbgWIy/IJBfQJ0inaJV+fbzqujsGY41s8RaZ9clkRQcJm
p5Boc+etklaFEd4wdB5PPUyW5EQw1lq2dUIyDi4ZmgfBdGuzMT5mfq1v+nGU930Q
OwIDAQAB
-----END PUBLIC KEY-----
`;
const privateKeyPem = `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAol+0LrzQ4+Z128AsTIsEk+7+NRttp97tYP5jF1oqMShYfuqm
Ykde2p2V99Wp4c1/m0SX31t6/XkokXAClC/F3LDQj4cYkR16wW/ff5hi4J19KxQN
35ANeRMcNLphCjZqTm6YmCOVfptjIIM3a7fxY+cVacnjNMf9KKmQYaq6dUNv4aOM
6m+7kyVZlCf+ZoQWQ3290vXi70Mq9RHQeoKL2lW1acAKfhGkolvyAJmPbgWIy/IJ
BfQJ0inaJV+fbzqujsGY41s8RaZ9clkRQcJmp5Boc+etklaFEd4wdB5PPUyW5EQw
1lq2dUIyDi4ZmgfBdGuzMT5mfq1v+nGU930QOwIDAQABAoIBAQCfruThLdl0txlj
ImuuXu0Cmv+utKmzdL4YLDTBK2/g5h+ZuSQl9LBo6J8qmxq1sMX0qZF+HFYo3rUx
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DQpCBDviKYlHdp0qk3ZOFlWzm2oEgp9B5R+DiFaw7P
n0dhBJHaUkwDRjJ2KPbeOUSSI/KEokBJ7vIqQfkC2dZZuvFHi2aV1A==
-----END RSA PRIVATE KEY-----
`;
const generateSeed = (length = 81) => {
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ9';
let buffer = '';
for (let i = 0; i < length; i++) {
buffer += charset.charAt(Math.floor(Math.random() * charset.length));
}
return buffer;
};
const main = async () => {
const tid = composeAPI({
providers: {
'0x1': 'https://nodes.devnet.iota.org:443',
},
});
const seed = generateSeed();
console.log('seed', seed);
const { document } = await tid.registerIdentifier('0x1', seed, [publicKeyPem]);
// @ts-ignore
const publicKey = document.publicKey[0];
console.log(document);
const credential = {
'@context': ['https://www.w3.org/2018/credentials/v1', 'http://schema.org'],
id: 'http://example.edu/credentials/58473',
type: ['VerifiableCredential', 'AlumniCredential'],
credentialSubject: {
id: 'did:tangle:5i1WeTSiRnsUBwj3DGLxHzENfryPxKUzkMES98S57LLAauY2NF5dx17yPke9PaoVYY9Qhh47J',
alumniOf: 'Example University',
},
};
const credentialSigned = await tid.signRsaSignature(credential, publicKey, privateKeyPem);
console.log(credentialSigned);
const verified = await tid.verifyRsaSignature(credentialSigned);
console.log('verified', verified);
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.